t-comm
Version:
专业、稳定、纯粹的工具库
65 lines (64 loc) • 2.8 kB
TypeScript
/**
* 微信蓝牙 API 薄封装
*
* 把所有对 wx.* 的直接调用集中在这一层,外部只看到 Promise / 回调注册接口。
* 这样:
* 1. 主流程类 BluetoothBump 不直接依赖 wx,可注入 mock adapter 做单测
* 2. 这里的实现也能单独被测:mock 一个 (global as any).wx 就够了
*/
export interface OpenAdapterResult {
/** iOS 上 peripheral 模式失败时为 true,需降级为只扫描 */
skipAdvertising: boolean;
}
export interface StartAdvertisingParams {
serviceUuid: string;
characteristicUuid: string;
tempId: string;
/**
* 可选:广播 payload 字符串(mutual 模式用)。
* 为空时回退为广播 tempId(向后兼容 simple 模式)。
*/
payload?: string;
}
export interface StartDiscoveryParams {
platform: string;
serviceUuid: string;
}
/** 微信蓝牙能力适配器接口(用于依赖注入,便于测试) */
export interface WxBluetoothAdapter {
/** 当前环境是否具备最低限度的蓝牙 API */
isAvailable(): boolean;
/** 检测平台(ios / android / devtools / unknown ...) */
detectPlatform(): string;
/** 触发隐私授权弹窗并等待用户同意(不支持时直接 resolve) */
ensurePrivacyAuthorized(privacyContent: string, log: (m: string) => void): Promise<void>;
/** 打开蓝牙适配器;iOS 上分别打开 central + peripheral,并返回是否需跳过广播 */
openAdapter(platform: string, log: (m: string) => void): Promise<OpenAdapterResult>;
/** 创建外围设备并开始广播 */
startAdvertising(params: StartAdvertisingParams): Promise<{
server: any;
}>;
/**
* 更新正在广播的 payload(mutual 模式用)。
* 实现:stopAdvertising → 重新 addService(新 value)→ startAdvertising。
* 失败时 reject,调用方需要自己决定是否回退。
*/
updateAdvertisedValue(server: any, params: StartAdvertisingParams): Promise<void>;
/** 开始扫描:iOS 不传 services(系统重新包装广播包后会扫不到) */
startDiscovery(params: StartDiscoveryParams): Promise<void>;
/** 注册扫描回调 */
onDeviceFound(cb: (res: any) => void): void;
offDeviceFound(cb: (res: any) => void): void;
/** 注册适配器状态变化回调 */
onAdapterStateChange(cb: (res: any) => void): void;
offAdapterStateChange(cb: (res: any) => void): void;
/** iOS 兜底轮询用 */
getDevices(): Promise<{
devices: any[];
}>;
/** 停止扫描 / 停止广播 / 关闭适配器(任一失败都安全吞掉) */
stopDiscovery(): void;
stopAdvertising(server: any): void;
closeAdapter(): void;
}
export declare const wxBluetoothAdapter: WxBluetoothAdapter;