Initial commit
This commit is contained in:
279
uni_modules/ak-sbsrv/utssdk/app-android/bluetooth_manager.uts
Normal file
279
uni_modules/ak-sbsrv/utssdk/app-android/bluetooth_manager.uts
Normal file
@@ -0,0 +1,279 @@
|
||||
import type {
|
||||
BleDevice,
|
||||
BleConnectionState,
|
||||
BleEvent,
|
||||
BleEventCallback,
|
||||
BleEventPayload,
|
||||
BleScanResult,
|
||||
BleConnectOptionsExt,
|
||||
AutoBleInterfaces,
|
||||
BleDataPayload,
|
||||
SendDataPayload,
|
||||
BleOptions,
|
||||
MultiProtocolDevice,
|
||||
ScanHandler,
|
||||
BleProtocolType,
|
||||
ScanDevicesOptions
|
||||
} from '../interface.uts';
|
||||
import { ProtocolHandler } from '../protocol_handler.uts';
|
||||
import { BluetoothService } from '../interface.uts';
|
||||
import { DeviceManager } from './device_manager.uts';
|
||||
|
||||
// Shape used when callers register plain objects as handlers. Using a named
|
||||
// type keeps member access explicit so the code generator emits valid Kotlin
|
||||
// member references instead of trying to access properties on Any.
|
||||
type RawProtocolHandler = {
|
||||
protocol?: BleProtocolType;
|
||||
scanDevices?: (options?: ScanDevicesOptions) => Promise<void>;
|
||||
connect?: (device: BleDevice, options?: BleConnectOptionsExt) => Promise<void>;
|
||||
disconnect?: (device: BleDevice) => Promise<void>;
|
||||
sendData?: (device: BleDevice, payload?: SendDataPayload, options?: BleOptions) => Promise<void>;
|
||||
autoConnect?: (device: BleDevice, options?: BleConnectOptionsExt) => Promise<AutoBleInterfaces>;
|
||||
}
|
||||
|
||||
// 设备上下文
|
||||
class DeviceContext {
|
||||
device : BleDevice;
|
||||
protocol : BleProtocolType;
|
||||
state : BleConnectionState;
|
||||
handler : ProtocolHandler;
|
||||
constructor(device : BleDevice, protocol : BleProtocolType, handler : ProtocolHandler) {
|
||||
this.device = device;
|
||||
this.protocol = protocol;
|
||||
this.state = 0; // DISCONNECTED
|
||||
this.handler = handler;
|
||||
}
|
||||
}
|
||||
|
||||
const deviceMap = new Map<string, DeviceContext>(); // key: deviceId|protocol
|
||||
// Single active protocol handler (no multi-protocol registration)
|
||||
let activeProtocol: BleProtocolType = 'standard';
|
||||
let activeHandler: ProtocolHandler | null = null;
|
||||
// 事件监听注册表
|
||||
const eventListeners = new Map<BleEvent, Set<BleEventCallback>>();
|
||||
|
||||
function emit(event : BleEvent, payload : BleEventPayload) {
|
||||
if (event === 'connectionStateChanged') {
|
||||
console.log('[AKBLE][LOG] bluetooth_manager.uts emit connectionStateChanged', payload)
|
||||
}
|
||||
const listeners = eventListeners.get(event);
|
||||
if (listeners != null) {
|
||||
listeners.forEach(cb => {
|
||||
try { cb(payload); } catch (e) { }
|
||||
});
|
||||
}
|
||||
}
|
||||
class ProtocolHandlerWrapper extends ProtocolHandler {
|
||||
private _raw: RawProtocolHandler | null;
|
||||
constructor(raw?: RawProtocolHandler) {
|
||||
// pass a lightweight BluetoothService instance to satisfy generators
|
||||
super(new BluetoothService());
|
||||
this._raw = (raw != null) ? raw : null;
|
||||
}
|
||||
override async scanDevices(options?: ScanDevicesOptions): Promise<void> {
|
||||
const rawTyped = this._raw;
|
||||
if (rawTyped != null && typeof rawTyped.scanDevices === 'function') {
|
||||
await rawTyped.scanDevices(options);
|
||||
}
|
||||
return;
|
||||
}
|
||||
override async connect(device: BleDevice, options?: BleConnectOptionsExt): Promise<void> {
|
||||
const rawTyped = this._raw;
|
||||
if (rawTyped != null && typeof rawTyped.connect === 'function') {
|
||||
await rawTyped.connect(device, options);
|
||||
}
|
||||
return;
|
||||
}
|
||||
override async disconnect(device: BleDevice): Promise<void> {
|
||||
const rawTyped = this._raw;
|
||||
if (rawTyped != null && typeof rawTyped.disconnect === 'function') {
|
||||
await rawTyped.disconnect(device);
|
||||
}
|
||||
return;
|
||||
}
|
||||
override async sendData(device: BleDevice, payload?: SendDataPayload, options?: BleOptions): Promise<void> {
|
||||
const rawTyped = this._raw;
|
||||
if (rawTyped != null && typeof rawTyped.sendData === 'function') {
|
||||
await rawTyped.sendData(device, payload, options);
|
||||
}
|
||||
return;
|
||||
}
|
||||
override async autoConnect(device: BleDevice, options?: BleConnectOptionsExt): Promise<AutoBleInterfaces> {
|
||||
const rawTyped = this._raw;
|
||||
if (rawTyped != null && typeof rawTyped.autoConnect === 'function') {
|
||||
return await rawTyped.autoConnect(device, options);
|
||||
}
|
||||
return { serviceId: '', writeCharId: '', notifyCharId: '' };
|
||||
}
|
||||
}
|
||||
|
||||
// Strong runtime detector for plain object handlers (no Type Predicate)
|
||||
// Note: the UTS bundler doesn't support TypeScript type predicates (x is T),
|
||||
// and it doesn't accept the 'unknown' type. This returns a boolean and
|
||||
// callers must cast the value to RawProtocolHandler after the function
|
||||
// returns true.
|
||||
function isRawProtocolHandler(x: any): boolean {
|
||||
if (x == null || typeof x !== 'object') return false;
|
||||
const r = x as Record<string, unknown>;
|
||||
if (typeof r['scanDevices'] === 'function') return true;
|
||||
if (typeof r['connect'] === 'function') return true;
|
||||
if (typeof r['disconnect'] === 'function') return true;
|
||||
if (typeof r['sendData'] === 'function') return true;
|
||||
if (typeof r['autoConnect'] === 'function') return true;
|
||||
if (typeof r['protocol'] === 'string') return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
export const registerProtocolHandler = (handler : any) => {
|
||||
if (handler == null) return;
|
||||
// Determine protocol value defensively. Default to 'standard' when unknown.
|
||||
let proto: BleProtocolType = 'standard';
|
||||
if (handler instanceof ProtocolHandler) {
|
||||
try { proto = (handler as ProtocolHandler).protocol as BleProtocolType; } catch (e) { }
|
||||
activeHandler = handler as ProtocolHandler;
|
||||
} else if (isRawProtocolHandler(handler)) {
|
||||
try { proto = (handler as RawProtocolHandler).protocol as BleProtocolType; } catch (e) { }
|
||||
activeHandler = new ProtocolHandlerWrapper(handler as RawProtocolHandler);
|
||||
(activeHandler as ProtocolHandler).protocol = proto;
|
||||
} else {
|
||||
console.warn('[AKBLE] registerProtocolHandler: unsupported handler type, ignoring', handler);
|
||||
return;
|
||||
}
|
||||
activeProtocol = proto;
|
||||
}
|
||||
|
||||
|
||||
export const scanDevices = async (options ?: ScanDevicesOptions) : Promise<void> => {
|
||||
console.log('[AKBLE] start scan', options)
|
||||
// Determine which protocols to run: either user-specified or all registered
|
||||
// Single active handler flow
|
||||
if (activeHandler == null) {
|
||||
console.log('[AKBLE] no active scan handler registered')
|
||||
return
|
||||
}
|
||||
const handler = activeHandler as ProtocolHandler;
|
||||
const scanOptions : ScanDevicesOptions = {
|
||||
onDeviceFound: (device : BleDevice) => emit('deviceFound', { event: 'deviceFound', device }),
|
||||
onScanFinished: () => emit('scanFinished', { event: 'scanFinished' })
|
||||
}
|
||||
try {
|
||||
await handler.scanDevices(scanOptions)
|
||||
} catch (e) {
|
||||
console.warn('[AKBLE] scanDevices handler error', e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const connectDevice = async (deviceId : string, protocol : BleProtocolType, options ?: BleConnectOptionsExt) : Promise<void> => {
|
||||
const handler = activeHandler;
|
||||
if (handler == null) throw new Error('No protocol handler');
|
||||
const device : BleDevice = { deviceId, name: '', rssi: 0 }; // 可扩展
|
||||
await handler.connect(device, options);
|
||||
const ctx = new DeviceContext(device, protocol, handler);
|
||||
ctx.state = 2; // CONNECTED
|
||||
deviceMap.set(getDeviceKey(deviceId, protocol), ctx);
|
||||
console.log(deviceMap)
|
||||
emit('connectionStateChanged', { event: 'connectionStateChanged', device, protocol, state: 2 });
|
||||
}
|
||||
|
||||
export const disconnectDevice = async (deviceId : string, protocol : BleProtocolType) : Promise<void> => {
|
||||
const ctx = deviceMap.get(getDeviceKey(deviceId, protocol));
|
||||
if (ctx == null || ctx.handler == null) return;
|
||||
await ctx.handler.disconnect(ctx.device);
|
||||
ctx.state = 0;
|
||||
emit('connectionStateChanged', { event: 'connectionStateChanged', device: ctx.device, protocol, state: 0 });
|
||||
deviceMap.delete(getDeviceKey(deviceId, protocol));
|
||||
}
|
||||
export const sendData = async (payload : SendDataPayload, options ?: BleOptions) : Promise<void> => {
|
||||
const ctx = deviceMap.get(getDeviceKey(payload.deviceId, payload.protocol));
|
||||
if (ctx == null) throw new Error('Device not connected');
|
||||
// copy to local non-null variable so generator can smart-cast across awaits
|
||||
const deviceCtx = ctx as DeviceContext;
|
||||
if (deviceCtx.handler == null) throw new Error('sendData not supported for this protocol');
|
||||
await deviceCtx.handler.sendData(deviceCtx.device, payload, options);
|
||||
emit('dataSent', { event: 'dataSent', device: deviceCtx.device, protocol: payload.protocol, data: payload.data });
|
||||
}
|
||||
|
||||
export const getConnectedDevices = () : MultiProtocolDevice[] => {
|
||||
const result : MultiProtocolDevice[] = [];
|
||||
deviceMap.forEach((ctx : DeviceContext) => {
|
||||
const dev : MultiProtocolDevice = {
|
||||
deviceId: ctx.device.deviceId,
|
||||
name: ctx.device.name,
|
||||
rssi: ctx.device.rssi,
|
||||
protocol: ctx.protocol
|
||||
};
|
||||
result.push(dev);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export const getConnectionState = (deviceId : string, protocol : BleProtocolType) : BleConnectionState => {
|
||||
const ctx = deviceMap.get(getDeviceKey(deviceId, protocol));
|
||||
if (ctx == null) return 0;
|
||||
return ctx.state;
|
||||
}
|
||||
|
||||
export const on = (event : BleEvent, callback : BleEventCallback) => {
|
||||
if (!eventListeners.has(event)) eventListeners.set(event, new Set());
|
||||
eventListeners.get(event)!.add(callback);
|
||||
}
|
||||
|
||||
export const off = (event : BleEvent, callback ?: BleEventCallback) => {
|
||||
if (callback == null) {
|
||||
eventListeners.delete(event);
|
||||
} else {
|
||||
eventListeners.get(event)?.delete(callback as BleEventCallback);
|
||||
}
|
||||
}
|
||||
|
||||
function getDeviceKey(deviceId : string, protocol : BleProtocolType) : string {
|
||||
return `${deviceId}|${protocol}`;
|
||||
}
|
||||
|
||||
export const autoConnect = async (deviceId : string, protocol : BleProtocolType, options ?: BleConnectOptionsExt) : Promise<AutoBleInterfaces> => {
|
||||
const handler = activeHandler;
|
||||
if (handler == null) throw new Error('autoConnect not supported for this protocol');
|
||||
const device : BleDevice = { deviceId, name: '', rssi: 0 };
|
||||
// safe call - handler.autoConnect exists on ProtocolHandler
|
||||
return await handler.autoConnect(device, options) as AutoBleInterfaces;
|
||||
}
|
||||
|
||||
// Ensure there is at least one handler registered so callers can scan/connect
|
||||
// without needing to import a registry module. This creates a minimal default
|
||||
// ProtocolHandler backed by a BluetoothService instance.
|
||||
try {
|
||||
if (activeHandler == null) {
|
||||
// Create a DeviceManager-backed raw handler that delegates to native code
|
||||
const _dm = DeviceManager.getInstance();
|
||||
const _raw: RawProtocolHandler = {
|
||||
protocol: 'standard',
|
||||
scanDevices: (options?: ScanDevicesOptions) => {
|
||||
try {
|
||||
const scanOptions = options != null ? options : {} as ScanDevicesOptions;
|
||||
_dm.startScan(scanOptions);
|
||||
} catch (e) {
|
||||
console.warn('[AKBLE] DeviceManager.startScan failed', e);
|
||||
}
|
||||
return Promise.resolve();
|
||||
},
|
||||
connect: (device, options?: BleConnectOptionsExt) => {
|
||||
return _dm.connectDevice(device.deviceId, options);
|
||||
},
|
||||
disconnect: (device) => {
|
||||
return _dm.disconnectDevice(device.deviceId);
|
||||
},
|
||||
autoConnect: (device, options?: any) => {
|
||||
// DeviceManager does not provide an autoConnect helper; return default
|
||||
const result: AutoBleInterfaces = { serviceId: '', writeCharId: '', notifyCharId: '' };
|
||||
return Promise.resolve(result);
|
||||
}
|
||||
};
|
||||
const _wrapper = new ProtocolHandlerWrapper(_raw);
|
||||
activeHandler = _wrapper;
|
||||
activeProtocol = _raw.protocol as BleProtocolType;
|
||||
console.log('[AKBLE] default protocol handler (BluetoothService-backed) registered', activeProtocol);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[AKBLE] failed to register default protocol handler', e);
|
||||
}
|
||||
5
uni_modules/ak-sbsrv/utssdk/app-android/config.json
Normal file
5
uni_modules/ak-sbsrv/utssdk/app-android/config.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": [
|
||||
|
||||
]
|
||||
}
|
||||
311
uni_modules/ak-sbsrv/utssdk/app-android/device_manager.uts
Normal file
311
uni_modules/ak-sbsrv/utssdk/app-android/device_manager.uts
Normal file
@@ -0,0 +1,311 @@
|
||||
import type { BleDevice, BleOptions, BleConnectionState, BleConnectionStateChangeCallback } from '../interface.uts'
|
||||
import type { BleConnectOptionsExt } from '../interface.uts'
|
||||
import type { ScanDevicesOptions } from '../interface.uts';
|
||||
import Context from "android.content.Context";
|
||||
import BluetoothAdapter from "android.bluetooth.BluetoothAdapter";
|
||||
import BluetoothManager from "android.bluetooth.BluetoothManager";
|
||||
import BluetoothDevice from "android.bluetooth.BluetoothDevice";
|
||||
import BluetoothGatt from "android.bluetooth.BluetoothGatt";
|
||||
import BluetoothGattCallback from "android.bluetooth.BluetoothGattCallback";
|
||||
import ScanCallback from "android.bluetooth.le.ScanCallback";
|
||||
import ScanResult from "android.bluetooth.le.ScanResult";
|
||||
import ScanSettings from "android.bluetooth.le.ScanSettings";
|
||||
import Handler from "android.os.Handler";
|
||||
import Looper from "android.os.Looper";
|
||||
import ContextCompat from "androidx.core.content.ContextCompat";
|
||||
import PackageManager from "android.content.pm.PackageManager";
|
||||
// 定义 PendingConnect 类型和实现类
|
||||
interface PendingConnect {
|
||||
resolve: () => void;
|
||||
reject: (err?: any) => void; // Changed to make err optional
|
||||
timer?: number;
|
||||
}
|
||||
|
||||
class PendingConnectImpl implements PendingConnect {
|
||||
resolve: () => void;
|
||||
reject: (err?: any) => void; // Changed to make err optional
|
||||
timer?: number;
|
||||
|
||||
constructor(resolve: () => void, reject: (err?: any) => void, timer?: number) {
|
||||
this.resolve = resolve;
|
||||
this.reject = reject;
|
||||
this.timer = timer;
|
||||
}
|
||||
}
|
||||
// 引入全局回调管理
|
||||
import { gattCallback } from './service_manager.uts'
|
||||
const pendingConnects = new Map<string, PendingConnect>();
|
||||
|
||||
const STATE_DISCONNECTED = 0;
|
||||
const STATE_CONNECTING = 1;
|
||||
const STATE_CONNECTED = 2;
|
||||
const STATE_DISCONNECTING = 3;
|
||||
|
||||
export class DeviceManager {
|
||||
private static instance: DeviceManager | null = null;
|
||||
private devices = new Map<string, BleDevice>();
|
||||
private connectionStates = new Map<string, BleConnectionState>();
|
||||
private connectionStateChangeListeners: BleConnectionStateChangeCallback[] = []
|
||||
private gattMap = new Map<string, BluetoothGatt | null>();
|
||||
private scanCallback: ScanCallback | null = null
|
||||
private isScanning: boolean = false
|
||||
private constructor() {}
|
||||
static getInstance(): DeviceManager {
|
||||
if (DeviceManager.instance == null) {
|
||||
DeviceManager.instance = new DeviceManager();
|
||||
}
|
||||
return DeviceManager.instance!;
|
||||
}
|
||||
startScan(options: ScanDevicesOptions): void {
|
||||
console.log('ak startscan now')
|
||||
const adapter = this.getBluetoothAdapter();
|
||||
if (adapter == null) {
|
||||
throw new Error('未找到蓝牙适配器');
|
||||
}
|
||||
if (!adapter.isEnabled) {
|
||||
// 尝试请求用户开启蓝牙
|
||||
try {
|
||||
adapter.enable(); // 直接调用,无需可选链和括号
|
||||
} catch (e) {
|
||||
// 某些设备可能不支持 enable
|
||||
}
|
||||
setTimeout(() => {
|
||||
if (!adapter.isEnabled) {
|
||||
throw new Error('蓝牙未开启');
|
||||
}
|
||||
}, 1500);
|
||||
throw new Error('正在开启蓝牙,请重试');
|
||||
}
|
||||
const foundDevices = this.devices; // 直接用全局 devices
|
||||
|
||||
class MyScanCallback extends ScanCallback {
|
||||
private foundDevices: Map<string, BleDevice>;
|
||||
private onDeviceFound: (device: BleDevice) => void;
|
||||
constructor(foundDevices: Map<string, BleDevice>, onDeviceFound: (device: BleDevice) => void) {
|
||||
super();
|
||||
this.foundDevices = foundDevices;
|
||||
this.onDeviceFound = onDeviceFound;
|
||||
}
|
||||
override onScanResult(callbackType: Int, result: ScanResult): void {
|
||||
const device = result.getDevice();
|
||||
if (device != null) {
|
||||
const deviceId = device.getAddress();
|
||||
let bleDevice = foundDevices.get(deviceId);
|
||||
if (bleDevice == null) {
|
||||
bleDevice = {
|
||||
deviceId,
|
||||
name: device.getName() ?? 'Unknown',
|
||||
rssi: result.getRssi(),
|
||||
lastSeen: Date.now()
|
||||
};
|
||||
foundDevices.set(deviceId, bleDevice);
|
||||
this.onDeviceFound(bleDevice);
|
||||
} else {
|
||||
// 更新属性(已确保 bleDevice 非空)
|
||||
bleDevice.rssi = result.getRssi();
|
||||
bleDevice.name = device.getName() ?? bleDevice.name;
|
||||
bleDevice.lastSeen = Date.now();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override onScanFailed(errorCode: Int): void {
|
||||
console.log('ak scan fail')
|
||||
}
|
||||
}
|
||||
this.scanCallback = new MyScanCallback(foundDevices, options.onDeviceFound ?? (() => {}));
|
||||
const scanner = adapter.getBluetoothLeScanner();
|
||||
if (scanner == null) {
|
||||
throw new Error('无法获取扫描器');
|
||||
}
|
||||
const scanSettings = new ScanSettings.Builder()
|
||||
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
|
||||
.build();
|
||||
scanner.startScan(null, scanSettings, this.scanCallback);
|
||||
this.isScanning = true;
|
||||
// 默认10秒后停止扫描
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() => {
|
||||
if (this.isScanning && this.scanCallback != null) {
|
||||
scanner.stopScan(this.scanCallback);
|
||||
this.isScanning = false;
|
||||
// this.devices = foundDevices;
|
||||
if (options.onScanFinished != null) options.onScanFinished?.invoke();
|
||||
}
|
||||
}, 40000);
|
||||
}
|
||||
|
||||
async connectDevice(deviceId: string, options?: BleConnectOptionsExt): Promise<void> {
|
||||
console.log('[AKBLE] connectDevice called, deviceId:', deviceId, 'options:', options, 'connectionStates:')
|
||||
const adapter = this.getBluetoothAdapter();
|
||||
if (adapter == null) {
|
||||
console.error('[AKBLE] connectDevice failed: 蓝牙适配器不可用')
|
||||
throw new Error('蓝牙适配器不可用');
|
||||
}
|
||||
const device = adapter.getRemoteDevice(deviceId);
|
||||
if (device == null) {
|
||||
console.error('[AKBLE] connectDevice failed: 未找到设备', deviceId)
|
||||
throw new Error('未找到设备');
|
||||
}
|
||||
this.connectionStates.set(deviceId, STATE_CONNECTING);
|
||||
console.log('[AKBLE] connectDevice set STATE_CONNECTING, deviceId:', deviceId, 'connectionStates:')
|
||||
this.emitConnectionStateChange(deviceId, STATE_CONNECTING);
|
||||
const activity = UTSAndroid.getUniActivity();
|
||||
const timeout = options?.timeout ?? 15000;
|
||||
const key = `${deviceId}|connect`;
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
console.error('[AKBLE] connectDevice 超时:', deviceId)
|
||||
pendingConnects.delete(key);
|
||||
this.connectionStates.set(deviceId, STATE_DISCONNECTED);
|
||||
this.gattMap.set(deviceId, null);
|
||||
this.emitConnectionStateChange(deviceId, STATE_DISCONNECTED);
|
||||
reject(new Error('连接超时'));
|
||||
}, timeout);
|
||||
|
||||
// 创建一个适配器函数来匹配类型签名
|
||||
const resolveAdapter = () => {
|
||||
console.log('[AKBLE] connectDevice resolveAdapter:', deviceId)
|
||||
resolve();
|
||||
};
|
||||
const rejectAdapter = (err?: any) => {
|
||||
console.error('[AKBLE] connectDevice rejectAdapter:', deviceId, err)
|
||||
reject(err);
|
||||
};
|
||||
|
||||
pendingConnects.set(key, new PendingConnectImpl(resolveAdapter, rejectAdapter, timer));
|
||||
try {
|
||||
console.log('[AKBLE] connectGatt 调用前:', deviceId)
|
||||
const gatt = device.connectGatt(activity, false, gattCallback);
|
||||
this.gattMap.set(deviceId, gatt);
|
||||
console.log('[AKBLE] connectGatt 调用后:', deviceId, gatt)
|
||||
} catch (e) {
|
||||
console.error('[AKBLE] connectGatt 异常:', deviceId, e)
|
||||
clearTimeout(timer);
|
||||
pendingConnects.delete(key);
|
||||
this.connectionStates.set(deviceId, STATE_DISCONNECTED);
|
||||
this.gattMap.set(deviceId, null);
|
||||
this.emitConnectionStateChange(deviceId, STATE_DISCONNECTED);
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 统一分发连接回调(应在 gattCallback.onConnectionStateChange 内调用)
|
||||
static handleConnectionStateChange(deviceId: string, newState: number, error?: any) {
|
||||
console.log('[AKBLE] handleConnectionStateChange:', deviceId, 'newState:', newState, 'error:', error, 'pendingConnects:')
|
||||
const key = `${deviceId}|connect`;
|
||||
const cb = pendingConnects.get(key);
|
||||
if (cb != null) {
|
||||
// 修复 timer 的空安全问题,使用临时变量
|
||||
const timerValue = cb.timer;
|
||||
if (timerValue != null) {
|
||||
clearTimeout(timerValue);
|
||||
}
|
||||
|
||||
// 修复 error 处理
|
||||
if (newState === STATE_CONNECTED) {
|
||||
console.log('[AKBLE] handleConnectionStateChange: 连接成功', deviceId)
|
||||
cb.resolve();
|
||||
} else {
|
||||
// 正确处理可空值
|
||||
const errorToUse = error != null ? error : new Error('连接断开');
|
||||
console.error('[AKBLE] handleConnectionStateChange: 连接失败', deviceId, errorToUse)
|
||||
cb.reject(errorToUse);
|
||||
}
|
||||
pendingConnects.delete(key);
|
||||
} else {
|
||||
console.warn('[AKBLE] handleConnectionStateChange: 未找到 pendingConnects', deviceId, newState)
|
||||
}
|
||||
}
|
||||
|
||||
async disconnectDevice(deviceId: string, isActive: boolean = true): Promise<void> {
|
||||
console.log('[AKBLE] disconnectDevice called, deviceId:', deviceId, 'isActive:', isActive)
|
||||
let gatt = this.gattMap.get(deviceId);
|
||||
if (gatt != null) {
|
||||
gatt.disconnect();
|
||||
gatt.close();
|
||||
// gatt=null;
|
||||
this.gattMap.set(deviceId, null);
|
||||
this.connectionStates.set(deviceId, STATE_DISCONNECTED);
|
||||
console.log('[AKBLE] disconnectDevice set STATE_DISCONNECTED, deviceId:', deviceId, 'connectionStates:')
|
||||
this.emitConnectionStateChange(deviceId, STATE_DISCONNECTED);
|
||||
return;
|
||||
} else {
|
||||
console.log('[AKBLE] disconnectDevice: gatt is null, deviceId:', deviceId)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async reconnectDevice(deviceId: string, options?: BleConnectOptionsExt): Promise<void> {
|
||||
let attempts = 0;
|
||||
const maxAttempts = options?.maxAttempts ?? 3;
|
||||
const interval = options?.interval ?? 3000;
|
||||
while (attempts < maxAttempts) {
|
||||
try {
|
||||
await this.disconnectDevice(deviceId, false);
|
||||
await this.connectDevice(deviceId, options);
|
||||
return;
|
||||
} catch (e) {
|
||||
attempts++;
|
||||
if (attempts >= maxAttempts) throw new Error('重连失败');
|
||||
// 修复 setTimeout 问题,使用旧式 Promise + setTimeout 解决
|
||||
await new Promise<void>((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, interval);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getConnectedDevices(): BleDevice[] {
|
||||
// 创建一个空数组来存储结果
|
||||
const result: BleDevice[] = [];
|
||||
|
||||
// 遍历 devices Map 并检查连接状态
|
||||
this.devices.forEach((device, deviceId) => {
|
||||
if (this.connectionStates.get(deviceId) === STATE_CONNECTED) {
|
||||
result.push(device);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
onConnectionStateChange(listener: BleConnectionStateChangeCallback) {
|
||||
console.log('[AKBLE][LOG] onConnectionStateChange 注册, 当前监听数:', this.connectionStateChangeListeners.length + 1, listener)
|
||||
this.connectionStateChangeListeners.push(listener)
|
||||
}
|
||||
|
||||
protected emitConnectionStateChange(deviceId: string, state: BleConnectionState) {
|
||||
console.log('[AKBLE][LOG] emitConnectionStateChange', deviceId, state, 'listeners:', this.connectionStateChangeListeners.length, 'connectionStates:', this.connectionStates)
|
||||
for (const listener of this.connectionStateChangeListeners) {
|
||||
try {
|
||||
console.log('[AKBLE][LOG] emitConnectionStateChange 调用 listener', listener)
|
||||
listener(deviceId, state)
|
||||
} catch (e) {
|
||||
console.error('[AKBLE][LOG] emitConnectionStateChange listener error', e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getGattInstance(deviceId: string): BluetoothGatt | null {
|
||||
return this.gattMap.get(deviceId) ?? null;
|
||||
}
|
||||
|
||||
private getBluetoothAdapter(): BluetoothAdapter | null {
|
||||
const context = UTSAndroid.getAppContext();
|
||||
if (context == null) return null;
|
||||
const manager = context?.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager;
|
||||
return manager.getAdapter();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定ID的设备(如果存在)
|
||||
*/
|
||||
public getDevice(deviceId: string): BleDevice | null {
|
||||
console.log(deviceId,this.devices)
|
||||
return this.devices.get(deviceId) ?? null;
|
||||
}
|
||||
}
|
||||
734
uni_modules/ak-sbsrv/utssdk/app-android/dfu_manager.uts
Normal file
734
uni_modules/ak-sbsrv/utssdk/app-android/dfu_manager.uts
Normal file
@@ -0,0 +1,734 @@
|
||||
import { BleService } from '../interface.uts'
|
||||
import type { WriteCharacteristicOptions, DfuOptions, ControlParserResult } from '../interface.uts'
|
||||
import { DeviceManager } from './device_manager.uts'
|
||||
import { ServiceManager } from './service_manager.uts'
|
||||
import BluetoothGatt from 'android.bluetooth.BluetoothGatt'
|
||||
import BluetoothGattCharacteristic from 'android.bluetooth.BluetoothGattCharacteristic'
|
||||
import BluetoothGattDescriptor from 'android.bluetooth.BluetoothGattDescriptor'
|
||||
import UUID from 'java.util.UUID'
|
||||
|
||||
// 通用 Nordic DFU UUIDs (常见设备可能使用这些;如厂商自定义请替换)
|
||||
const DFU_SERVICE_UUID = '0000fe59-0000-1000-8000-00805f9b34fb'
|
||||
const DFU_CONTROL_POINT_UUID = '8ec90001-f315-4f60-9fb8-838830daea50'
|
||||
const DFU_PACKET_UUID = '8ec90002-f315-4f60-9fb8-838830daea50'
|
||||
|
||||
type DfuSession = {
|
||||
resolve : () => void;
|
||||
reject : (err ?: any) => void;
|
||||
onProgress ?: (p : number) => void;
|
||||
onLog ?: (s : string) => void;
|
||||
controlParser ?: (data : Uint8Array) => ControlParserResult | null;
|
||||
// Nordic 专用字段
|
||||
bytesSent ?: number;
|
||||
totalBytes ?: number;
|
||||
useNordic ?: boolean;
|
||||
// PRN (packet receipt notification) support
|
||||
prn ?: number;
|
||||
packetsSincePrn ?: number;
|
||||
prnResolve ?: () => void;
|
||||
prnReject ?: (err ?: any) => void;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export class DfuManager {
|
||||
// 会话表,用于把 control-point 通知路由到当前 DFU 流程
|
||||
private sessions : Map<string, DfuSession> = new Map();
|
||||
|
||||
// 简化:只实现最基本的 GATT-based DFU 上传逻辑,需按设备协议调整 control point 的命令/解析
|
||||
|
||||
// Emit a DFU lifecycle event for a session. name should follow Nordic listener names
|
||||
private _emitDfuEvent(deviceId : string, name : string, payload ?: any) {
|
||||
console.log('[DFU][Event]', name, deviceId, payload ?? '');
|
||||
const s = this.sessions.get(deviceId);
|
||||
if (s == null) return;
|
||||
if (typeof s.onLog == 'function') {
|
||||
try {
|
||||
const logFn = s.onLog as (msg : string) => void;
|
||||
logFn(`[${name}] ${payload != null ? JSON.stringify(payload) : ''}`);
|
||||
} catch (e) { }
|
||||
}
|
||||
if (name == 'onProgress' && typeof s.onProgress == 'function' && typeof payload == 'number') {
|
||||
try { s.onProgress(payload); } catch (e) { }
|
||||
}
|
||||
}
|
||||
|
||||
async startDfu(deviceId : string, firmwareBytes : Uint8Array, options ?: DfuOptions) : Promise<void> {
|
||||
console.log('startDfu 0')
|
||||
const deviceManager = DeviceManager.getInstance();
|
||||
const serviceManager = ServiceManager.getInstance();
|
||||
console.log('startDfu 1')
|
||||
const gatt : BluetoothGatt | null = deviceManager.getGattInstance(deviceId);
|
||||
console.log('startDfu 2')
|
||||
if (gatt == null) throw new Error('Device not connected');
|
||||
console.log('[DFU] startDfu start deviceId=', deviceId, 'firmwareBytes=', firmwareBytes != null ? firmwareBytes.length : 0, 'options=', options);
|
||||
try {
|
||||
console.log('[DFU] requesting high connection priority for', deviceId);
|
||||
gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
|
||||
} catch (e) {
|
||||
console.warn('[DFU] requestConnectionPriority failed', e);
|
||||
}
|
||||
|
||||
// 发现服务并特征
|
||||
// ensure services discovered before accessing GATT; serviceManager exposes Promise-based API
|
||||
await serviceManager.getServices(deviceId, null);
|
||||
console.log('[DFU] services ensured for', deviceId);
|
||||
const dfuService = gatt.getService(UUID.fromString(DFU_SERVICE_UUID));
|
||||
if (dfuService == null) throw new Error('DFU service not found');
|
||||
const controlChar = dfuService.getCharacteristic(UUID.fromString(DFU_CONTROL_POINT_UUID));
|
||||
const packetChar = dfuService.getCharacteristic(UUID.fromString(DFU_PACKET_UUID));
|
||||
console.log('[DFU] dfuService=', dfuService != null ? dfuService.getUuid().toString() : null, 'controlChar=', controlChar != null ? controlChar.getUuid().toString() : null, 'packetChar=', packetChar != null ? packetChar.getUuid().toString() : null);
|
||||
if (controlChar == null || packetChar == null) throw new Error('DFU characteristics missing');
|
||||
const packetProps = packetChar.getProperties();
|
||||
const supportsWriteWithResponse = (packetProps & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0;
|
||||
const supportsWriteNoResponse = (packetProps & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0;
|
||||
console.log('[DFU] packet characteristic props mask=', packetProps, 'supportsWithResponse=', supportsWriteWithResponse, 'supportsNoResponse=', supportsWriteNoResponse);
|
||||
|
||||
// Allow caller to request a desired MTU via options for higher throughput
|
||||
const desiredMtu = (options != null && typeof options.mtu == 'number') ? options.mtu : 247;
|
||||
try {
|
||||
console.log('[DFU] requesting MTU=', desiredMtu, 'for', deviceId);
|
||||
await this._requestMtu(gatt, desiredMtu, 8000);
|
||||
console.log('[DFU] requestMtu completed for', deviceId);
|
||||
} catch (e) {
|
||||
console.warn('[DFU] requestMtu failed or timed out, continue with default.', e);
|
||||
}
|
||||
const mtu = desiredMtu; // 假定成功或使用期望值
|
||||
const chunkSize = Math.max(20, mtu - 3);
|
||||
|
||||
// small helper to convert a byte (possibly signed) to a two-digit hex string
|
||||
const byteToHex = (b : number) => {
|
||||
const v = (b < 0) ? (b + 256) : b;
|
||||
let s = v.toString(16);
|
||||
if (s.length < 2) s = '0' + s;
|
||||
return s;
|
||||
};
|
||||
|
||||
// Parameterize PRN window and timeout via options early so they are available
|
||||
// for session logging. Defaults: prn = 12 packets, prnTimeoutMs = 10000 ms
|
||||
let prnWindow = 0;
|
||||
if (options != null && typeof options.prn == 'number') {
|
||||
prnWindow = Math.max(0, Math.floor(options.prn));
|
||||
}
|
||||
const prnTimeoutMs = (options != null && typeof options.prnTimeoutMs == 'number') ? Math.max(1000, Math.floor(options.prnTimeoutMs)) : 8000;
|
||||
const disablePrnOnTimeout = !(options != null && options.disablePrnOnTimeout == false);
|
||||
|
||||
// 订阅 control point 通知并将通知路由到会话处理器
|
||||
const controlHandler = (data : Uint8Array) => {
|
||||
// 交给会话处理器解析并触发事件
|
||||
try {
|
||||
const hexParts: string[] = [];
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
const v = data[i] as number;
|
||||
hexParts.push(byteToHex(v));
|
||||
}
|
||||
const hex = hexParts.join(' ');
|
||||
console.log('[DFU] control notification callback invoked for', deviceId, 'raw=', Array.from(data), 'hex=', hex);
|
||||
} catch (e) {
|
||||
console.log('[DFU] control notification callback invoked for', deviceId, 'raw=', Array.from(data));
|
||||
}
|
||||
this._handleControlNotification(deviceId, data);
|
||||
};
|
||||
console.log('[DFU] subscribing control point for', deviceId);
|
||||
await serviceManager.subscribeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID, controlHandler);
|
||||
console.log('[DFU] subscribeCharacteristic returned for', deviceId);
|
||||
|
||||
// 保存会话回调(用于 waitForControlEvent); 支持 Nordic 模式追踪已发送字节
|
||||
this.sessions.set(deviceId, {
|
||||
resolve: () => { },
|
||||
reject: (err ?: any) => {console.log(err) },
|
||||
onProgress: null,
|
||||
onLog: null,
|
||||
controlParser: (data : Uint8Array) => this._defaultControlParser(data),
|
||||
bytesSent: 0,
|
||||
totalBytes: firmwareBytes.length,
|
||||
useNordic: options != null && options.useNordic == true,
|
||||
prn: null,
|
||||
packetsSincePrn: 0,
|
||||
prnResolve: null,
|
||||
prnReject: null
|
||||
});
|
||||
console.log('[DFU] session created for', deviceId, 'totalBytes=', firmwareBytes.length);
|
||||
console.log('[DFU] DFU session details:', { deviceId: deviceId, totalBytes: firmwareBytes.length, chunkSize: chunkSize, prnWindow: prnWindow, prnTimeoutMs: prnTimeoutMs });
|
||||
|
||||
// wire options callbacks into the session (if provided)
|
||||
const sessRef = this.sessions.get(deviceId);
|
||||
if (sessRef != null) {
|
||||
sessRef.onProgress = (options != null && typeof options.onProgress == 'function') ? options.onProgress : null;
|
||||
sessRef.onLog = (options != null && typeof options.onLog == 'function') ? options.onLog : null;
|
||||
}
|
||||
|
||||
// emit initial lifecycle events (Nordic-like)
|
||||
this._emitDfuEvent(deviceId, 'onDeviceConnecting', null);
|
||||
|
||||
// 写入固件数据(非常保守的实现:逐包写入并等待短延迟)
|
||||
// --- PRN setup (optional, Nordic-style flow) ---
|
||||
// Parameterize PRN window and timeout via options: options.prn, options.prnTimeoutMs
|
||||
// Defaults were set earlier; build PRN payload using arithmetic to avoid
|
||||
// bitwise operators which don't map cleanly to generated Kotlin.
|
||||
if (prnWindow > 0) {
|
||||
try {
|
||||
// send Set PRN to device (format: [OP_CODE_SET_PRN, prn LSB, prn MSB])
|
||||
// WARNING: Ensure your device uses the same opcode/format; change if needed.
|
||||
const prnLsb = prnWindow % 256;
|
||||
const prnMsb = Math.floor(prnWindow / 256) % 256;
|
||||
const prnPayload = new Uint8Array([0x02, prnLsb, prnMsb]);
|
||||
await serviceManager.writeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID, prnPayload, null);
|
||||
const sess0 = this.sessions.get(deviceId);
|
||||
if (sess0 != null) {
|
||||
sess0.useNordic = true;
|
||||
sess0.prn = prnWindow;
|
||||
sess0.packetsSincePrn = 0;
|
||||
sess0.controlParser = (data : Uint8Array) => this._nordicControlParser(data);
|
||||
}
|
||||
console.log('[DFU] Set PRN sent (prn=', prnWindow, ') for', deviceId);
|
||||
} catch (e) {
|
||||
console.warn('[DFU] Set PRN failed (continuing without PRN):', e);
|
||||
const sessFallback = this.sessions.get(deviceId);
|
||||
if (sessFallback != null) sessFallback.prn = 0;
|
||||
}
|
||||
} else {
|
||||
console.log('[DFU] PRN disabled (prnWindow=', prnWindow, ') for', deviceId);
|
||||
}
|
||||
|
||||
// 写入固件数据(逐包写入并根据 options.waitForResponse 选择是否等待响应)
|
||||
let offset = 0;
|
||||
const total = firmwareBytes.length;
|
||||
this._emitDfuEvent(deviceId, 'onDfuProcessStarted', null);
|
||||
this._emitDfuEvent(deviceId, 'onUploadingStarted', null);
|
||||
// Track outstanding write operations when using fire-and-forget mode so we can
|
||||
// log and throttle if the Android stack becomes overwhelmed.
|
||||
let outstandingWrites = 0;
|
||||
// read tuning parameters from options in a safe, generator-friendly way
|
||||
let configuredMaxOutstanding = 2;
|
||||
let writeSleepMs = 0;
|
||||
let writeRetryDelay = 100;
|
||||
let writeMaxAttempts = 12;
|
||||
let writeGiveupTimeout = 60000;
|
||||
let drainOutstandingTimeout = 3000;
|
||||
let failureBackoffMs = 0;
|
||||
|
||||
// throughput measurement
|
||||
let throughputWindowBytes = 0;
|
||||
let lastThroughputTime = Date.now();
|
||||
function _logThroughputIfNeeded(force ?: boolean) {
|
||||
try {
|
||||
const now = Date.now();
|
||||
const elapsed = now - lastThroughputTime;
|
||||
if (force == true || elapsed >= 1000) {
|
||||
const bytes = throughputWindowBytes;
|
||||
const bps = Math.floor((bytes * 1000) / Math.max(1, elapsed));
|
||||
// reset window
|
||||
throughputWindowBytes = 0;
|
||||
lastThroughputTime = now;
|
||||
const human = `${bps} B/s`;
|
||||
console.log('[DFU] throughput:', human, 'elapsedMs=', elapsed);
|
||||
const s = this.sessions.get(deviceId);
|
||||
if (s != null && typeof s.onLog == 'function') {
|
||||
try { s.onLog?.invoke('[DFU] throughput: ' + human); } catch (e) { }
|
||||
}
|
||||
}
|
||||
} catch (e) { }
|
||||
}
|
||||
|
||||
function _safeErr(e ?: any) {
|
||||
try {
|
||||
if (e == null) return '';
|
||||
if (typeof e == 'string') return e;
|
||||
try { return JSON.stringify(e); } catch (e2) { }
|
||||
try { return (e as any).toString(); } catch (e3) { }
|
||||
return '';
|
||||
} catch (e4) { return ''; }
|
||||
}
|
||||
try {
|
||||
if (options != null) {
|
||||
try {
|
||||
if (options.maxOutstanding != null) {
|
||||
const parsed = Math.floor(options.maxOutstanding as number);
|
||||
if (!isNaN(parsed) && parsed > 0) configuredMaxOutstanding = parsed;
|
||||
}
|
||||
} catch (e) { }
|
||||
try {
|
||||
if (options.writeSleepMs != null) {
|
||||
const parsedWs = Math.floor(options.writeSleepMs as number);
|
||||
if (!isNaN(parsedWs) && parsedWs >= 0) writeSleepMs = parsedWs;
|
||||
}
|
||||
} catch (e) { }
|
||||
try {
|
||||
if (options.writeRetryDelayMs != null) {
|
||||
const parsedRetry = Math.floor(options.writeRetryDelayMs as number);
|
||||
if (!isNaN(parsedRetry) && parsedRetry >= 0) writeRetryDelay = parsedRetry;
|
||||
}
|
||||
} catch (e) { }
|
||||
try {
|
||||
if (options.writeMaxAttempts != null) {
|
||||
const parsedAttempts = Math.floor(options.writeMaxAttempts as number);
|
||||
if (!isNaN(parsedAttempts) && parsedAttempts > 0) writeMaxAttempts = parsedAttempts;
|
||||
}
|
||||
} catch (e) { }
|
||||
try {
|
||||
if (options.writeGiveupTimeoutMs != null) {
|
||||
const parsedGiveupTimeout = Math.floor(options.writeGiveupTimeoutMs as number);
|
||||
if (!isNaN(parsedGiveupTimeout) && parsedGiveupTimeout > 0) writeGiveupTimeout = parsedGiveupTimeout;
|
||||
}
|
||||
} catch (e) { }
|
||||
try {
|
||||
if (options.drainOutstandingTimeoutMs != null) {
|
||||
const parsedDrain = Math.floor(options.drainOutstandingTimeoutMs as number);
|
||||
if (!isNaN(parsedDrain) && parsedDrain >= 0) drainOutstandingTimeout = parsedDrain;
|
||||
}
|
||||
} catch (e) { }
|
||||
}
|
||||
if (configuredMaxOutstanding < 1) configuredMaxOutstanding = 1;
|
||||
if (writeSleepMs < 0) writeSleepMs = 0;
|
||||
} catch (e) { }
|
||||
if (supportsWriteWithResponse == false && supportsWriteNoResponse == true) {
|
||||
if (configuredMaxOutstanding > 1) configuredMaxOutstanding = 1;
|
||||
if (writeSleepMs < 15) writeSleepMs = 15;
|
||||
if (writeRetryDelay < 150) writeRetryDelay = 150;
|
||||
if (writeMaxAttempts < 40) writeMaxAttempts = 40;
|
||||
if (writeGiveupTimeout < 120000) writeGiveupTimeout = 120000;
|
||||
console.log('[DFU] packet char only supports WRITE_NO_RESPONSE; serializing writes with conservative pacing');
|
||||
}
|
||||
const maxOutstandingCeiling = configuredMaxOutstanding;
|
||||
let adaptiveMaxOutstanding = configuredMaxOutstanding;
|
||||
const minOutstandingWindow = 1;
|
||||
|
||||
while (offset < total) {
|
||||
const end = Math.min(offset + chunkSize, total);
|
||||
const slice = firmwareBytes.subarray(offset, end);
|
||||
// Decide whether to wait for response per-chunk. Honor characteristic support.
|
||||
// Generator-friendly: avoid 'undefined' and use explicit boolean check.
|
||||
let finalWaitForResponse = true;
|
||||
if (options != null) {
|
||||
try {
|
||||
const maybe = options.waitForResponse;
|
||||
if (maybe == true && supportsWriteWithResponse == false && supportsWriteNoResponse == true) {
|
||||
// caller requested response but characteristic cannot provide it; keep true to ensure we await the write promise.
|
||||
finalWaitForResponse = true;
|
||||
} else if (maybe == false) {
|
||||
finalWaitForResponse = false;
|
||||
} else if (maybe == true) {
|
||||
finalWaitForResponse = true;
|
||||
}
|
||||
} catch (e) { finalWaitForResponse = true; }
|
||||
}
|
||||
|
||||
const writeOpts: WriteCharacteristicOptions = {
|
||||
waitForResponse: finalWaitForResponse,
|
||||
retryDelayMs: writeRetryDelay,
|
||||
maxAttempts: writeMaxAttempts,
|
||||
giveupTimeoutMs: writeGiveupTimeout,
|
||||
forceWriteTypeNoResponse: finalWaitForResponse == false
|
||||
};
|
||||
console.log('[DFU] writing packet chunk offset=', offset, 'len=', slice.length, 'waitForResponse=', finalWaitForResponse, 'outstanding=', outstandingWrites);
|
||||
|
||||
// Fire-and-forget path: do not await the write if waitForResponse == false.
|
||||
if (finalWaitForResponse == false) {
|
||||
if (failureBackoffMs > 0) {
|
||||
console.log('[DFU] applying failure backoff', failureBackoffMs, 'ms before next write for', deviceId);
|
||||
await this._sleep(failureBackoffMs);
|
||||
failureBackoffMs = Math.floor(failureBackoffMs / 2);
|
||||
}
|
||||
while (outstandingWrites >= adaptiveMaxOutstanding) {
|
||||
await this._sleep(Math.max(1, writeSleepMs));
|
||||
}
|
||||
// increment outstanding counter and kick the write without awaiting.
|
||||
outstandingWrites = outstandingWrites + 1;
|
||||
// fire-and-forget: start the write but don't await its Promise
|
||||
const writeOffset = offset;
|
||||
serviceManager.writeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_PACKET_UUID, slice, writeOpts).then((res) => {
|
||||
outstandingWrites = Math.max(0, outstandingWrites - 1);
|
||||
if (res == true) {
|
||||
if (adaptiveMaxOutstanding < maxOutstandingCeiling) {
|
||||
adaptiveMaxOutstanding = Math.min(maxOutstandingCeiling, adaptiveMaxOutstanding + 1);
|
||||
}
|
||||
if (failureBackoffMs > 0) failureBackoffMs = Math.floor(failureBackoffMs / 2);
|
||||
}
|
||||
// log occasional completions
|
||||
if ((outstandingWrites & 0x1f) == 0) {
|
||||
console.log('[DFU] write completion callback, outstandingWrites=', outstandingWrites, 'adaptiveWindow=', adaptiveMaxOutstanding, 'device=', deviceId);
|
||||
}
|
||||
// detect write failure signaled by service manager
|
||||
if (res !== true) {
|
||||
adaptiveMaxOutstanding = Math.max(minOutstandingWindow, Math.floor(adaptiveMaxOutstanding / 2));
|
||||
failureBackoffMs = Math.min(200, Math.max(failureBackoffMs, Math.max(5, writeRetryDelay)));
|
||||
console.error('[DFU] writeCharacteristic returned false for device=', deviceId, 'offset=', writeOffset, 'adaptiveWindow now=', adaptiveMaxOutstanding);
|
||||
try { this._emitDfuEvent(deviceId, 'onError', { phase: 'write', offset: writeOffset, reason: 'write returned false' }); } catch (e) { }
|
||||
}
|
||||
}).catch((e) => {
|
||||
outstandingWrites = Math.max(0, outstandingWrites - 1);
|
||||
adaptiveMaxOutstanding = Math.max(minOutstandingWindow, Math.floor(adaptiveMaxOutstanding / 2));
|
||||
failureBackoffMs = Math.min(200, Math.max(failureBackoffMs, Math.max(5, writeRetryDelay)));
|
||||
console.warn('[DFU] fire-and-forget write failed for device=', deviceId, e, 'adaptiveWindow now=', adaptiveMaxOutstanding);
|
||||
try {
|
||||
const errMsg ='[DFU] fire-and-forget write failed for device=';
|
||||
this._emitDfuEvent(deviceId, 'onError', { phase: 'write', offset: writeOffset, reason: errMsg });
|
||||
} catch (e2) { }
|
||||
});
|
||||
// account bytes for throughput
|
||||
throughputWindowBytes += slice.length;
|
||||
_logThroughputIfNeeded(false);
|
||||
} else {
|
||||
console.log('[DFU] awaiting write for chunk offset=', offset);
|
||||
try {
|
||||
const writeResult = await serviceManager.writeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_PACKET_UUID, slice, writeOpts);
|
||||
if (writeResult !== true) {
|
||||
console.error('[DFU] writeCharacteristic(await) returned false at offset=', offset, 'device=', deviceId);
|
||||
try { this._emitDfuEvent(deviceId, 'onError', { phase: 'write', offset: offset, reason: 'write returned false' }); } catch (e) { }
|
||||
// abort DFU by throwing
|
||||
throw new Error('write failed');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[DFU] awaiting write failed at offset=', offset, 'device=', deviceId, e);
|
||||
try {
|
||||
const errMsg = '[DFU] awaiting write failed ';
|
||||
this._emitDfuEvent(deviceId, 'onError', { phase: 'write', offset: offset, reason: errMsg });
|
||||
} catch (e2) { }
|
||||
throw e;
|
||||
}
|
||||
// account bytes for throughput
|
||||
throughputWindowBytes += slice.length;
|
||||
_logThroughputIfNeeded(false);
|
||||
}
|
||||
// update PRN counters and wait when window reached
|
||||
const sessAfter = this.sessions.get(deviceId);
|
||||
if (sessAfter != null && sessAfter.useNordic == true && typeof sessAfter.prn == 'number' && (sessAfter.prn ?? 0) > 0) {
|
||||
sessAfter.packetsSincePrn = (sessAfter.packetsSincePrn ?? 0) + 1;
|
||||
if ((sessAfter.packetsSincePrn ?? 0) >= (sessAfter.prn ?? 0) && (sessAfter.prn ?? 0) > 0) {
|
||||
// wait for PRN (device notification) before continuing
|
||||
try {
|
||||
console.log('[DFU] reached PRN window, waiting for PRN for', deviceId, 'packetsSincePrn=', sessAfter.packetsSincePrn, 'prn=', sessAfter.prn);
|
||||
await this._waitForPrn(deviceId, prnTimeoutMs);
|
||||
console.log('[DFU] PRN received, resuming transfer for', deviceId);
|
||||
} catch (e) {
|
||||
console.warn('[DFU] PRN wait failed/timed out, continuing anyway for', deviceId, e);
|
||||
if (disablePrnOnTimeout) {
|
||||
console.warn('[DFU] disabling PRN waits after timeout for', deviceId);
|
||||
sessAfter.prn = 0;
|
||||
}
|
||||
}
|
||||
// reset counter
|
||||
sessAfter.packetsSincePrn = 0;
|
||||
}
|
||||
}
|
||||
offset = end;
|
||||
// 如果启用 nordic 模式,统计已发送字节
|
||||
const sess = this.sessions.get(deviceId);
|
||||
if (sess != null && typeof sess.bytesSent == 'number') {
|
||||
sess.bytesSent = (sess.bytesSent ?? 0) + slice.length;
|
||||
}
|
||||
// 简单节流与日志,避免过快。默认睡眠非常短以提高吞吐量; 可在设备上调节
|
||||
console.log('[DFU] wrote chunk for', deviceId, 'offset=', offset, '/', total, 'chunkSize=', slice.length, 'bytesSent=', sess != null ? sess.bytesSent : null, 'outstanding=', outstandingWrites);
|
||||
// emit upload progress event (percent) if available
|
||||
if (sess != null && typeof sess.bytesSent == 'number' && typeof sess.totalBytes == 'number') {
|
||||
const p = Math.floor((sess.bytesSent / sess.totalBytes) * 100);
|
||||
this._emitDfuEvent(deviceId, 'onProgress', p);
|
||||
}
|
||||
// yield to event loop and avoid starving the Android BLE stack
|
||||
await this._sleep(Math.max(0, writeSleepMs));
|
||||
}
|
||||
// wait for outstanding writes to drain before continuing with control commands
|
||||
if (outstandingWrites > 0) {
|
||||
const drainStart = Date.now();
|
||||
while (outstandingWrites > 0 && (Date.now() - drainStart) < drainOutstandingTimeout) {
|
||||
await this._sleep(Math.max(0, writeSleepMs));
|
||||
}
|
||||
if (outstandingWrites > 0) {
|
||||
console.warn('[DFU] outstandingWrites remain after drain timeout, continuing with', outstandingWrites);
|
||||
} else {
|
||||
console.log('[DFU] outstandingWrites drained before control phase');
|
||||
}
|
||||
}
|
||||
this._emitDfuEvent(deviceId, 'onUploadingCompleted', null);
|
||||
|
||||
// force final throughput log before activate/validate
|
||||
_logThroughputIfNeeded(true);
|
||||
|
||||
// 发送 activate/validate 命令到 control point(需根据设备协议实现)
|
||||
// 下面为占位:请替换为实际的 opcode
|
||||
// 在写入前先启动控制结果等待,防止设备快速响应导致丢失通知
|
||||
const controlTimeout = 20000;
|
||||
const controlResultPromise = this._waitForControlResult(deviceId, controlTimeout);
|
||||
try {
|
||||
// control writes: pass undefined options explicitly to satisfy the generator/typechecker
|
||||
const activatePayload = new Uint8Array([0x04]);
|
||||
console.log('[DFU] sending activate/validate payload=', Array.from(activatePayload));
|
||||
await serviceManager.writeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID, activatePayload, null);
|
||||
console.log('[DFU] activate/validate write returned for', deviceId);
|
||||
} catch (e) {
|
||||
// 写入失败时取消控制等待,避免悬挂定时器
|
||||
try {
|
||||
const sessOnWriteFail = this.sessions.get(deviceId);
|
||||
if (sessOnWriteFail != null && typeof sessOnWriteFail.reject == 'function') {
|
||||
sessOnWriteFail.reject(e);
|
||||
}
|
||||
} catch (rejectErr) { }
|
||||
await controlResultPromise.catch(() => { });
|
||||
try { await serviceManager.unsubscribeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID); } catch (e2) { }
|
||||
this.sessions.delete(deviceId);
|
||||
throw e;
|
||||
}
|
||||
console.log('[DFU] sent control activate/validate command to control point for', deviceId);
|
||||
this._emitDfuEvent(deviceId, 'onValidating', null);
|
||||
|
||||
// 等待 control-point 返回最终结果(成功或失败),超时可配置
|
||||
console.log('[DFU] waiting for control result (timeout=', controlTimeout, ') for', deviceId);
|
||||
try {
|
||||
await controlResultPromise;
|
||||
console.log('[DFU] control result resolved for', deviceId);
|
||||
} catch (err) {
|
||||
// 清理订阅后抛出
|
||||
try { await serviceManager.unsubscribeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID); } catch (e) { }
|
||||
this.sessions.delete(deviceId);
|
||||
throw err;
|
||||
}
|
||||
|
||||
// 取消订阅
|
||||
try {
|
||||
await serviceManager.unsubscribeCharacteristic(deviceId, DFU_SERVICE_UUID, DFU_CONTROL_POINT_UUID);
|
||||
} catch (e) { }
|
||||
console.log('[DFU] unsubscribed control point for', deviceId);
|
||||
|
||||
// 清理会话
|
||||
this.sessions.delete(deviceId);
|
||||
console.log('[DFU] session cleared for', deviceId);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
async _requestMtu(gatt : BluetoothGatt, mtu : number, timeoutMs : number) : Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
// 在当前项目,BluetoothGattCallback.onMtuChanged 未被封装;简单发起请求并等待短超时
|
||||
try {
|
||||
const ok = gatt.requestMtu(Math.floor(mtu) as Int);
|
||||
if (!ok) {
|
||||
return reject(new Error('requestMtu failed'));
|
||||
}
|
||||
} catch (e) {
|
||||
return reject(e);
|
||||
}
|
||||
// 无 callback 监听时退回,等待一小段时间以便成功
|
||||
setTimeout(() => resolve(), Math.min(2000, timeoutMs));
|
||||
});
|
||||
}
|
||||
|
||||
_sleep(ms : number) {
|
||||
return new Promise<void>((r) => { setTimeout(() => { r() }, ms); });
|
||||
}
|
||||
|
||||
_waitForPrn(deviceId : string, timeoutMs : number) : Promise<void> {
|
||||
const session = this.sessions.get(deviceId);
|
||||
if (session == null) return Promise.reject(new Error('no dfu session'));
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
// timeout waiting for PRN
|
||||
// clear pending handlers
|
||||
session.prnResolve = null;
|
||||
session.prnReject = null;
|
||||
reject(new Error('PRN timeout'));
|
||||
}, timeoutMs);
|
||||
const prnResolve = () => {
|
||||
clearTimeout(timer);
|
||||
resolve();
|
||||
};
|
||||
const prnReject = (err ?: any) => {
|
||||
clearTimeout(timer);
|
||||
reject(err);
|
||||
};
|
||||
session.prnResolve = prnResolve;
|
||||
session.prnReject = prnReject;
|
||||
});
|
||||
}
|
||||
|
||||
// 默认 control point 解析器(非常通用的尝试解析:如果设备发送 progress byte 或成功码)
|
||||
_defaultControlParser(data : Uint8Array) : ControlParserResult | null {
|
||||
// 假设协议:第一个字节为 opcode, 第二字节可为状态或进度
|
||||
if (data == null || data.length === 0) return null;
|
||||
const op = data[0];
|
||||
// Nordic-style response: [0x10, requestOp, resultCode]
|
||||
if (op === 0x10 && data.length >= 3) {
|
||||
const requestOp = data[1];
|
||||
const resultCode = data[2];
|
||||
if (resultCode === 0x01) {
|
||||
return { type: 'success' };
|
||||
}
|
||||
return { type: 'error', error: { requestOp: requestOp, resultCode: resultCode, raw: Array.from(data) } };
|
||||
}
|
||||
// Nordic PRN notification: [0x11, LSB, MSB] -> return as progress (bytes received)
|
||||
if (op === 0x11 && data.length >= 3) {
|
||||
const lsb = data[1];
|
||||
const msb = data[2];
|
||||
const received = (msb << 8) | lsb;
|
||||
return { type: 'progress', progress: received };
|
||||
}
|
||||
// vendor-specific opcode example: 0x60 may mean 'response/progress' for some firmwares
|
||||
if (op === 0x60) {
|
||||
if (data.length >= 3) {
|
||||
const requestOp = data[1];
|
||||
const status = data[2];
|
||||
// Known success/status codes observed in field devices
|
||||
if (status === 0x00 || status === 0x01 || status === 0x0A) {
|
||||
return { type: 'success' };
|
||||
}
|
||||
return { type: 'error', error: { requestOp: requestOp, resultCode: status, raw: Array.from(data) } };
|
||||
}
|
||||
if (data.length >= 2) {
|
||||
return { type: 'progress', progress: data[1] };
|
||||
}
|
||||
}
|
||||
// 通用进度回退:若第二字节位于 0-100 之间,当作百分比
|
||||
if (data.length >= 2) {
|
||||
const maybeProgress = data[1];
|
||||
if (maybeProgress >= 0 && maybeProgress <= 100) {
|
||||
return { type: 'progress', progress: maybeProgress };
|
||||
}
|
||||
}
|
||||
// 若找到明显的 success opcode (示例 0x01) 或 error 0xFF
|
||||
if (op === 0x01) return { type: 'success' };
|
||||
if (op === 0xFF) return { type: 'error', error: data };
|
||||
return { type: 'info' };
|
||||
}
|
||||
|
||||
// Nordic DFU control-parser(支持 Response and Packet Receipt Notification)
|
||||
_nordicControlParser(data : Uint8Array) : ControlParserResult | null {
|
||||
// Nordic opcodes (简化):
|
||||
// - 0x10 : Response (opcode, requestOp, resultCode)
|
||||
// - 0x11 : Packet Receipt Notification (opcode, value LSB, value MSB)
|
||||
if (data == null || data.length == 0) return null;
|
||||
const op = data[0];
|
||||
if (op == 0x11 && data.length >= 3) {
|
||||
// packet receipt notif: bytes received (little endian)
|
||||
const lsb = data[1];
|
||||
const msb = data[2];
|
||||
const received = (msb << 8) | lsb;
|
||||
// Return received bytes as progress value; parser does not resolve device-specific session here.
|
||||
return { type: 'progress', progress: received };
|
||||
}
|
||||
// Nordic vendor-specific progress/response opcode (example 0x60)
|
||||
if (op == 0x60) {
|
||||
if (data.length >= 3) {
|
||||
const requestOp = data[1];
|
||||
const status = data[2];
|
||||
if (status == 0x00 || status == 0x01 || status == 0x0A) {
|
||||
return { type: 'success' };
|
||||
}
|
||||
return { type: 'error', error: { requestOp, resultCode: status, raw: Array.from(data) } };
|
||||
}
|
||||
if (data.length >= 2) {
|
||||
return { type: 'progress', progress: data[1] };
|
||||
}
|
||||
}
|
||||
// Response: check result code for success (0x01 may indicate success in some stacks)
|
||||
if (op == 0x10 && data.length >= 3) {
|
||||
const requestOp = data[1];
|
||||
const resultCode = data[2];
|
||||
// Nordic resultCode 0x01 = SUCCESS typically
|
||||
if (resultCode == 0x01) return { type: 'success' };
|
||||
else return { type: 'error', error: { requestOp, resultCode } };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
_handleControlNotification(deviceId : string, data : Uint8Array) {
|
||||
const session = this.sessions.get(deviceId);
|
||||
if (session == null) {
|
||||
console.warn('[DFU] control notification received but no session for', deviceId, 'data=', Array.from(data));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// human readable opcode mapping
|
||||
let opcodeName = 'unknown';
|
||||
switch (data[0]) {
|
||||
case 0x10: opcodeName = 'Response'; break;
|
||||
case 0x11: opcodeName = 'PRN'; break;
|
||||
case 0x60: opcodeName = 'VendorProgress'; break;
|
||||
case 0x01: opcodeName = 'SuccessOpcode'; break;
|
||||
case 0xFF: opcodeName = 'ErrorOpcode'; break;
|
||||
}
|
||||
console.log('[DFU] _handleControlNotification deviceId=', deviceId, 'opcode=0x' + data[0].toString(16), 'name=', opcodeName, 'raw=', Array.from(data));
|
||||
const parsed = session.controlParser != null ? session.controlParser(data) : null;
|
||||
if (session.onLog != null) session.onLog('DFU control notify: ' + Array.from(data).join(','));
|
||||
console.log('[DFU] parsed control result=', parsed);
|
||||
if (parsed == null) return;
|
||||
if (parsed.type == 'progress' && parsed.progress != null) {
|
||||
// 如果在 nordic 模式 parsed.progress 可能是已接收字节数,则转换为百分比
|
||||
if (session.useNordic == true && session.totalBytes != null && session.totalBytes > 0) {
|
||||
const percent = Math.floor((parsed.progress / session.totalBytes) * 100);
|
||||
session.onProgress?.(percent);
|
||||
// If we have written all bytes locally, log that event
|
||||
if (session.bytesSent != null && session.totalBytes != null && session.bytesSent >= session.totalBytes) {
|
||||
console.log('[DFU] all bytes written locally for', deviceId, 'bytesSent=', session.bytesSent, 'total=', session.totalBytes);
|
||||
// emit uploading completed once
|
||||
this._emitDfuEvent(deviceId, 'onUploadingCompleted', null);
|
||||
}
|
||||
// If a PRN wait is pending, resolve it (PRN indicates device received packets)
|
||||
if (typeof session.prnResolve == 'function') {
|
||||
try { session.prnResolve(); } catch (e) { }
|
||||
session.prnResolve = null;
|
||||
session.prnReject = null;
|
||||
session.packetsSincePrn = 0;
|
||||
}
|
||||
} else {
|
||||
const progress = parsed.progress
|
||||
if (progress != null) {
|
||||
console.log('[DFU] progress for', deviceId, 'progress=', progress);
|
||||
session.onProgress?.(progress);
|
||||
// also resolve PRN if was waiting (in case device reports numeric progress)
|
||||
if (typeof session.prnResolve == 'function') {
|
||||
try { session.prnResolve(); } catch (e) { }
|
||||
session.prnResolve = null;
|
||||
session.prnReject = null;
|
||||
session.packetsSincePrn = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (parsed.type == 'success') {
|
||||
console.log('[DFU] parsed success for', deviceId, 'resolving session');
|
||||
session.resolve();
|
||||
// Log final device-acknowledged success
|
||||
console.log('[DFU] device reported DFU success for', deviceId);
|
||||
this._emitDfuEvent(deviceId, 'onDfuCompleted', null);
|
||||
} else if (parsed.type == 'error') {
|
||||
console.error('[DFU] parsed error for', deviceId, parsed.error);
|
||||
session.reject(parsed.error ?? new Error('DFU device error'));
|
||||
this._emitDfuEvent(deviceId, 'onError', parsed.error ?? {});
|
||||
} else {
|
||||
// info - just log
|
||||
}
|
||||
} catch (e) {
|
||||
session.onLog?.('control parse error: ' + e);
|
||||
console.error('[DFU] control parse exception for', deviceId, e);
|
||||
}
|
||||
}
|
||||
|
||||
_waitForControlResult(deviceId : string, timeoutMs : number) : Promise<void> {
|
||||
const session = this.sessions.get(deviceId);
|
||||
if (session == null) return Promise.reject(new Error('no dfu session'));
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
// wrap resolve/reject to clear timer
|
||||
const timer = setTimeout(() => {
|
||||
// 超时
|
||||
console.error('[DFU] _waitForControlResult timeout for', deviceId);
|
||||
reject(new Error('DFU control timeout'));
|
||||
}, timeoutMs);
|
||||
const origResolve = () => {
|
||||
clearTimeout(timer);
|
||||
console.log('[DFU] _waitForControlResult resolved for', deviceId);
|
||||
resolve();
|
||||
};
|
||||
const origReject = (err ?: any) => {
|
||||
clearTimeout(timer);
|
||||
console.error('[DFU] _waitForControlResult rejected for', deviceId, 'err=', err);
|
||||
reject(err);
|
||||
};
|
||||
// replace session handlers temporarily (guard nullable)
|
||||
if (session != null) {
|
||||
session.resolve = origResolve;
|
||||
session.reject = origReject;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const dfuManager = new DfuManager();
|
||||
110
uni_modules/ak-sbsrv/utssdk/app-android/index.uts
Normal file
110
uni_modules/ak-sbsrv/utssdk/app-android/index.uts
Normal file
@@ -0,0 +1,110 @@
|
||||
import * as BluetoothManager from './bluetooth_manager.uts';
|
||||
import { ServiceManager } from './service_manager.uts';
|
||||
import type { ScanDevicesOptions, BleConnectOptionsExt, MultiProtocolDevice, BleEvent, BleEventCallback, BleService, BleCharacteristic, WriteCharacteristicOptions, AutoBleInterfaces, BleDataReceivedCallback } from '../interface.uts';
|
||||
import { DeviceManager } from './device_manager.uts';
|
||||
|
||||
const serviceManager = ServiceManager.getInstance();
|
||||
|
||||
export class BluetoothService {
|
||||
scanDevices(options?: ScanDevicesOptions) { return BluetoothManager.scanDevices(options); }
|
||||
connectDevice(deviceId: string, protocol: string, options?: BleConnectOptionsExt) { return BluetoothManager.connectDevice(deviceId, protocol, options); }
|
||||
disconnectDevice(deviceId: string, protocol: string) { return BluetoothManager.disconnectDevice(deviceId, protocol); }
|
||||
getConnectedDevices(): MultiProtocolDevice[] { return BluetoothManager.getConnectedDevices(); }
|
||||
on(event: BleEvent, callback: BleEventCallback) { return BluetoothManager.on(event, callback); }
|
||||
off(event: BleEvent, callback?: BleEventCallback) { return BluetoothManager.off(event, callback); }
|
||||
getServices(deviceId: string): Promise<BleService[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
serviceManager.getServices(deviceId, (list, err) => {
|
||||
console.log('getServices:', list, err); // 新增日志
|
||||
if (err != null) reject(err);
|
||||
else resolve((list as BleService[]) ?? []);
|
||||
});
|
||||
});
|
||||
}
|
||||
getCharacteristics(deviceId: string, serviceId: string): Promise<BleCharacteristic[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(deviceId,serviceId)
|
||||
serviceManager.getCharacteristics(deviceId, serviceId, (list, err) => {
|
||||
if (err != null) reject(err);
|
||||
else resolve((list as BleCharacteristic[]) ?? []);
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 自动发现服务和特征,返回可用的写入和通知特征ID
|
||||
* @param deviceId 设备ID
|
||||
* @returns {Promise<AutoBleInterfaces>}
|
||||
*/
|
||||
async getAutoBleInterfaces(deviceId: string): Promise<AutoBleInterfaces> {
|
||||
// 1. 获取服务列表
|
||||
const services = await this.getServices(deviceId);
|
||||
if (services == null || services.length == 0) throw new Error('未发现服务');
|
||||
|
||||
// 2. 选择目标服务(优先bae前缀,可根据需要调整)
|
||||
let serviceId = '';
|
||||
for (let i = 0; i < services.length; i++) {
|
||||
const s = services[i];
|
||||
const uuidCandidate: string | null = (s.uuid != null ? s.uuid : null)
|
||||
const uuid: string = uuidCandidate != null ? uuidCandidate : ''
|
||||
// prefer regex test to avoid nullable receiver calls in generated Kotlin
|
||||
if (/^bae/i.test(uuid)) {
|
||||
serviceId = uuid
|
||||
break;
|
||||
}
|
||||
}
|
||||
console.log(serviceId)
|
||||
if (serviceId == null || serviceId == '') serviceId = services[0].uuid;
|
||||
|
||||
// 3. 获取特征列表
|
||||
const characteristics = await this.getCharacteristics(deviceId, serviceId);
|
||||
console.log(characteristics)
|
||||
if (characteristics == null || characteristics.length == 0) throw new Error('未发现特征值');
|
||||
|
||||
// 4. 筛选write和notify特征
|
||||
let writeCharId = '';
|
||||
let notifyCharId = '';
|
||||
for (let i = 0; i < characteristics.length; i++) {
|
||||
|
||||
const c = characteristics[i];
|
||||
console.log(c)
|
||||
if ((writeCharId == null || writeCharId == '') && c.properties != null && (c.properties.write || c.properties.writeWithoutResponse==true)) writeCharId = c.uuid;
|
||||
if ((notifyCharId == null || notifyCharId == '') && c.properties != null && (c.properties.notify || c.properties.indicate)) notifyCharId = c.uuid;
|
||||
}
|
||||
console.log(serviceId, writeCharId, notifyCharId);
|
||||
if ((writeCharId == null || writeCharId == '') || (notifyCharId == null || notifyCharId == '')) throw new Error('未找到合适的写入或通知特征');
|
||||
console.log(serviceId, writeCharId, notifyCharId);
|
||||
// // 发现服务和特征后
|
||||
const deviceManager = DeviceManager.getInstance();
|
||||
console.log(deviceManager);
|
||||
const device = deviceManager.getDevice(deviceId);
|
||||
console.log(deviceId,device)
|
||||
device!.serviceId = serviceId;
|
||||
device!.writeCharId = writeCharId;
|
||||
device!.notifyCharId = notifyCharId;
|
||||
console.log(device);
|
||||
return { serviceId, writeCharId, notifyCharId };
|
||||
}
|
||||
async subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, onData: BleDataReceivedCallback): Promise<void> {
|
||||
return serviceManager.subscribeCharacteristic(deviceId, serviceId, characteristicId, onData);
|
||||
}
|
||||
async readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<ArrayBuffer> {
|
||||
return serviceManager.readCharacteristic(deviceId, serviceId, characteristicId);
|
||||
}
|
||||
async writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, data: Uint8Array, options?: WriteCharacteristicOptions): Promise<boolean> {
|
||||
return serviceManager.writeCharacteristic(deviceId, serviceId, characteristicId, data, options);
|
||||
}
|
||||
async unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise<void> {
|
||||
return serviceManager.unsubscribeCharacteristic(deviceId, serviceId, characteristicId);
|
||||
}
|
||||
async autoDiscoverAll(deviceId: string): Promise<any> {
|
||||
return serviceManager.autoDiscoverAll(deviceId);
|
||||
}
|
||||
async subscribeAllNotifications(deviceId: string, onData: BleDataReceivedCallback): Promise<void> {
|
||||
return serviceManager.subscribeAllNotifications(deviceId, onData);
|
||||
}
|
||||
}
|
||||
|
||||
export const bluetoothService = new BluetoothService();
|
||||
|
||||
// Ensure protocol handlers are registered when this module is imported.
|
||||
// import './protocol_registry.uts';
|
||||
663
uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts
Normal file
663
uni_modules/ak-sbsrv/utssdk/app-android/service_manager.uts
Normal file
@@ -0,0 +1,663 @@
|
||||
import type { BleService, BleCharacteristic, BleDataReceivedCallback, BleCharacteristicProperties, WriteCharacteristicOptions, ByteArray } from '../interface.uts'
|
||||
import BluetoothGatt from "android.bluetooth.BluetoothGatt";
|
||||
import BluetoothGattService from "android.bluetooth.BluetoothGattService";
|
||||
import BluetoothGattCharacteristic from "android.bluetooth.BluetoothGattCharacteristic";
|
||||
import BluetoothGattDescriptor from "android.bluetooth.BluetoothGattDescriptor";
|
||||
import BluetoothGattCallback from "android.bluetooth.BluetoothGattCallback";
|
||||
|
||||
import UUID from "java.util.UUID";
|
||||
import { DeviceManager } from './device_manager.uts'
|
||||
import { AkBleErrorImpl, AkBluetoothErrorCode } from '../unierror.uts'
|
||||
import { AutoDiscoverAllResult } from '../interface.uts'
|
||||
|
||||
|
||||
|
||||
|
||||
// 补全UUID格式,将短格式转换为标准格式
|
||||
function getFullUuid(shortUuid : string) : string {
|
||||
return `0000${shortUuid}-0000-1000-8000-00805f9b34fb`
|
||||
}
|
||||
|
||||
|
||||
const deviceWriteQueues = new Map<string, Promise<void>>();
|
||||
|
||||
function enqueueDeviceWrite<T>(deviceId : string, work : () => Promise<T>) : Promise<T> {
|
||||
const previous = deviceWriteQueues.get(deviceId) ?? Promise.resolve();
|
||||
const next = (async () : Promise<T> => {
|
||||
try {
|
||||
await previous;
|
||||
} catch (e) { /* ignore previous rejection to keep queue alive */ }
|
||||
return await work();
|
||||
})();
|
||||
const queued = next.then(() => { }, () => { });
|
||||
deviceWriteQueues.set(deviceId, queued);
|
||||
return next.finally(() => {
|
||||
if (deviceWriteQueues.get(deviceId) == queued) {
|
||||
deviceWriteQueues.delete(deviceId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
function createCharProperties(props : number) : BleCharacteristicProperties {
|
||||
const result : BleCharacteristicProperties = {
|
||||
read: false,
|
||||
write: false,
|
||||
notify: false,
|
||||
indicate: false,
|
||||
canRead: false,
|
||||
canWrite: false,
|
||||
canNotify: false,
|
||||
writeWithoutResponse: false
|
||||
};
|
||||
result.read = (props & BluetoothGattCharacteristic.PROPERTY_READ) !== 0;
|
||||
result.write = (props & BluetoothGattCharacteristic.PROPERTY_WRITE) !== 0;
|
||||
result.notify = (props & BluetoothGattCharacteristic.PROPERTY_NOTIFY) !== 0;
|
||||
result.indicate = (props & BluetoothGattCharacteristic.PROPERTY_INDICATE) !== 0;
|
||||
result.writeWithoutResponse = (props & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) !== 0;
|
||||
|
||||
result.canRead = result.read;
|
||||
const writeWithoutResponse = result.writeWithoutResponse;
|
||||
result.canWrite = (result.write != null && result.write) || (writeWithoutResponse != null && writeWithoutResponse);
|
||||
result.canNotify = result.notify;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 定义 PendingCallback 类型和实现类
|
||||
interface PendingCallback {
|
||||
resolve : (data : any) => void;
|
||||
reject : (err ?: any) => void;
|
||||
timer ?: number; // Changed from any to number
|
||||
}
|
||||
|
||||
class PendingCallbackImpl implements PendingCallback {
|
||||
resolve : (data : any) => void;
|
||||
reject : (err ?: any) => void;
|
||||
timer ?: number; // Changed from any to number
|
||||
|
||||
constructor(resolve : (data : any) => void, reject : (err ?: any) => void, timer ?: number) {
|
||||
this.resolve = resolve;
|
||||
this.reject = reject;
|
||||
this.timer = timer;
|
||||
}
|
||||
}
|
||||
|
||||
// 全局回调管理(必须在类外部声明)
|
||||
let pendingCallbacks : Map<string, PendingCallback>;
|
||||
let notifyCallbacks : Map<string, BleDataReceivedCallback>;
|
||||
|
||||
// 在全局范围内初始化
|
||||
pendingCallbacks = new Map<string, PendingCallback>();
|
||||
notifyCallbacks = new Map<string, BleDataReceivedCallback>();
|
||||
|
||||
// 服务发现等待队列:deviceId -> 回调数组
|
||||
const serviceDiscoveryWaiters = new Map<string, ((services : BleService[] | null, error ?: Error) => void)[]>();
|
||||
// 服务发现状态:deviceId -> 是否已发现
|
||||
const serviceDiscovered = new Map<string, boolean>();
|
||||
|
||||
// 特征发现等待队列:deviceId|serviceId -> 回调数组
|
||||
const characteristicDiscoveryWaiters = new Map<string, ((characteristics : BleCharacteristic[] | null, error ?: Error) => void)[]>();
|
||||
|
||||
class GattCallback extends BluetoothGattCallback {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
override onServicesDiscovered(gatt : BluetoothGatt, status : Int) : void {
|
||||
console.log('ak onServicesDiscovered')
|
||||
const deviceId = gatt.getDevice().getAddress();
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
console.log(`服务发现成功: ${deviceId}`);
|
||||
serviceDiscovered.set(deviceId, true);
|
||||
// 统一回调所有等待 getServices 的调用
|
||||
const waiters = serviceDiscoveryWaiters.get(deviceId);
|
||||
if (waiters != null && waiters.length > 0) {
|
||||
const services = gatt.getServices();
|
||||
const result : BleService[] = [];
|
||||
if (services != null) {
|
||||
const servicesList = services;
|
||||
const size = servicesList.size;
|
||||
for (let i = 0; i < size; i++) {
|
||||
const service = servicesList.get(i as Int);
|
||||
if (service != null) {
|
||||
const bleService : BleService = {
|
||||
uuid: service.getUuid().toString(),
|
||||
isPrimary: service.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY
|
||||
};
|
||||
result.push(bleService);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < waiters.length; i++) {
|
||||
const cb = waiters[i];
|
||||
if (cb != null) { cb(result, null); }
|
||||
}
|
||||
serviceDiscoveryWaiters.delete(deviceId);
|
||||
}
|
||||
} else {
|
||||
console.log(`服务发现失败: ${deviceId}, status: ${status}`);
|
||||
// 失败时也要通知等待队列
|
||||
const waiters = serviceDiscoveryWaiters.get(deviceId);
|
||||
if (waiters != null && waiters.length > 0) {
|
||||
for (let i = 0; i < waiters.length; i++) {
|
||||
const cb = waiters[i];
|
||||
if (cb != null) { cb(null, new Error('服务发现失败')); }
|
||||
}
|
||||
serviceDiscoveryWaiters.delete(deviceId);
|
||||
}
|
||||
}
|
||||
}
|
||||
override onConnectionStateChange(gatt : BluetoothGatt, status : Int, newState : Int) : void {
|
||||
const deviceId = gatt.getDevice().getAddress();
|
||||
if (newState == BluetoothGatt.STATE_CONNECTED) {
|
||||
console.log(`设备已连接: ${deviceId}`);
|
||||
DeviceManager.handleConnectionStateChange(deviceId, 2, null); // 2 = STATE_CONNECTED
|
||||
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
|
||||
console.log(`设备已断开: ${deviceId}`);
|
||||
serviceDiscovered.delete(deviceId);
|
||||
DeviceManager.handleConnectionStateChange(deviceId, 0, null); // 0 = STATE_DISCONNECTED
|
||||
}
|
||||
}
|
||||
override onCharacteristicChanged(gatt : BluetoothGatt, characteristic : BluetoothGattCharacteristic) : void {
|
||||
console.log('ak onCharacteristicChanged')
|
||||
const deviceId = gatt.getDevice().getAddress();
|
||||
const serviceId = characteristic.getService().getUuid().toString();
|
||||
const charId = characteristic.getUuid().toString();
|
||||
const key = `${deviceId}|${serviceId}|${charId}|notify`;
|
||||
const callback = notifyCallbacks.get(key);
|
||||
const value = characteristic.getValue();
|
||||
console.log('[onCharacteristicChanged]', key, value);
|
||||
if (callback != null && value != null) {
|
||||
const valueLength = value.size;
|
||||
const arr = new Uint8Array(valueLength);
|
||||
for (let i = 0 as Int; i < valueLength; i++) {
|
||||
const v = value[i as Int];
|
||||
arr[i] = v != null ? v : 0;
|
||||
}
|
||||
// 保存接收日志
|
||||
console.log(`
|
||||
INSERT INTO ble_data_log (device_id, service_id, char_id, direction, data, timestamp)
|
||||
VALUES ('${deviceId}', '${serviceId}', '${charId}', 'recv', '${Array.from(arr).join(',')}', ${Date.now()})
|
||||
`)
|
||||
|
||||
callback(arr);
|
||||
}
|
||||
}
|
||||
override onCharacteristicRead(gatt : BluetoothGatt, characteristic : BluetoothGattCharacteristic, status : Int) : void {
|
||||
console.log('ak onCharacteristicRead', status)
|
||||
const deviceId = gatt.getDevice().getAddress();
|
||||
const serviceId = characteristic.getService().getUuid().toString();
|
||||
const charId = characteristic.getUuid().toString();
|
||||
const key = `${deviceId}|${serviceId}|${charId}|read`;
|
||||
const pending = pendingCallbacks.get(key);
|
||||
const value = characteristic.getValue();
|
||||
console.log('[onCharacteristicRead]', key, 'status=', status, 'value=', value);
|
||||
if (pending != null) {
|
||||
try {
|
||||
const timer = pending.timer;
|
||||
if (timer != null) {
|
||||
clearTimeout(timer);
|
||||
pending.timer = null;
|
||||
}
|
||||
pendingCallbacks.delete(key);
|
||||
if (status == BluetoothGatt.GATT_SUCCESS && value != null) {
|
||||
const valueLength = value.size
|
||||
const arr = new Uint8Array(valueLength);
|
||||
for (let i = 0 as Int; i < valueLength; i++) {
|
||||
const v = value[i as Int];
|
||||
arr[i] = v != null ? v : 0;
|
||||
}
|
||||
|
||||
// resolve with ArrayBuffer
|
||||
pending.resolve(arr.buffer as ArrayBuffer);
|
||||
} else {
|
||||
pending.reject(new Error('Characteristic read failed'));
|
||||
}
|
||||
} catch (e) {
|
||||
try { pending.reject(e); } catch (e2) { console.error(e2); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override onCharacteristicWrite(gatt : BluetoothGatt, characteristic : BluetoothGattCharacteristic, status : Int) : void {
|
||||
console.log('ak onCharacteristicWrite', status)
|
||||
const deviceId = gatt.getDevice().getAddress();
|
||||
const serviceId = characteristic.getService().getUuid().toString();
|
||||
const charId = characteristic.getUuid().toString();
|
||||
const key = `${deviceId}|${serviceId}|${charId}|write`;
|
||||
const pending = pendingCallbacks.get(key);
|
||||
console.log('[onCharacteristicWrite]', key, 'status=', status);
|
||||
if (pending != null) {
|
||||
try {
|
||||
const timer = pending.timer;
|
||||
if (timer != null) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
pendingCallbacks.delete(key);
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
pending.resolve('ok');
|
||||
} else {
|
||||
pending.reject(new Error('Characteristic write failed'));
|
||||
}
|
||||
} catch (e) {
|
||||
try { pending.reject(e); } catch (e2) { console.error(e2); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 导出单例实例供外部使用
|
||||
export const gattCallback = new GattCallback();
|
||||
|
||||
export class ServiceManager {
|
||||
private static instance : ServiceManager | null = null;
|
||||
private services = new Map<string, BleService[]>();
|
||||
private characteristics = new Map<string, Map<string, BleCharacteristic[]>>();
|
||||
private deviceManager = DeviceManager.getInstance();
|
||||
private constructor() { }
|
||||
static getInstance() : ServiceManager {
|
||||
if (ServiceManager.instance == null) {
|
||||
ServiceManager.instance = new ServiceManager();
|
||||
}
|
||||
return ServiceManager.instance!;
|
||||
}
|
||||
|
||||
getServices(deviceId : string, callback ?: (services : BleService[] | null, error ?: Error) => void) : any | Promise<BleService[]> {
|
||||
console.log('ak start getservice', deviceId);
|
||||
const gatt = this.deviceManager.getGattInstance(deviceId);
|
||||
if (gatt == null) {
|
||||
if (callback != null) { callback(null, new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", "")); }
|
||||
return Promise.reject(new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", ""));
|
||||
}
|
||||
console.log('ak serviceDiscovered', gatt)
|
||||
// 如果服务已发现,直接返回
|
||||
if (serviceDiscovered.get(deviceId) == true) {
|
||||
const services = gatt.getServices();
|
||||
console.log(services)
|
||||
const result : BleService[] = [];
|
||||
if (services != null) {
|
||||
const servicesList = services;
|
||||
const size = servicesList.size;
|
||||
if (size > 0) {
|
||||
for (let i = 0 as Int; i < size; i++) {
|
||||
const service = servicesList != null ? servicesList.get(i) : servicesList[i];
|
||||
if (service != null) {
|
||||
const bleService : BleService = {
|
||||
uuid: service.getUuid().toString(),
|
||||
isPrimary: service.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY
|
||||
};
|
||||
result.push(bleService);
|
||||
|
||||
if (bleService.uuid == getFullUuid('0001')) {
|
||||
const device = this.deviceManager.getDevice(deviceId);
|
||||
if (device != null) {
|
||||
device.serviceId = bleService.uuid;
|
||||
this.getCharacteristics(deviceId, device.serviceId, (chars, err) => {
|
||||
if (err == null && chars != null) {
|
||||
const writeChar = chars.find(c => c.uuid == getFullUuid('0010'));
|
||||
const notifyChar = chars.find(c => c.uuid == getFullUuid('0011'));
|
||||
if (writeChar != null) device.writeCharId = writeChar.uuid;
|
||||
if (notifyChar != null) device.notifyCharId = notifyChar.uuid;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (callback != null) { callback(result, null); }
|
||||
return Promise.resolve(result);
|
||||
}
|
||||
// 未发现则发起服务发现并加入等待队列
|
||||
if (!serviceDiscoveryWaiters.has(deviceId)) {
|
||||
serviceDiscoveryWaiters.set(deviceId, []);
|
||||
gatt.discoverServices();
|
||||
}
|
||||
return new Promise<BleService[]>((resolve, reject) => {
|
||||
const cb = (services : BleService[] | null, error ?: Error) => {
|
||||
if (error != null) reject(error);
|
||||
else resolve(services ?? []);
|
||||
if (callback != null) callback(services, error);
|
||||
};
|
||||
const arr = serviceDiscoveryWaiters.get(deviceId);
|
||||
if (arr != null) arr.push(cb);
|
||||
});
|
||||
}
|
||||
getCharacteristics(deviceId : string, serviceId : string, callback : (characteristics : BleCharacteristic[] | null, error ?: Error) => void) : void {
|
||||
const gatt = this.deviceManager.getGattInstance(deviceId);
|
||||
if (gatt == null) return callback(null, new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", ""));
|
||||
// 如果服务还没发现,等待服务发现后再查特征
|
||||
if (serviceDiscovered.get(deviceId) !== true) {
|
||||
// 先注册到服务发现等待队列
|
||||
this.getServices(deviceId, (services, err) => {
|
||||
if (err != null) {
|
||||
callback(null, err);
|
||||
} else {
|
||||
this.getCharacteristics(deviceId, serviceId, callback);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 服务已发现,正常获取特征
|
||||
const service = gatt.getService(UUID.fromString(serviceId));
|
||||
if (service == null) return callback(null, new AkBleErrorImpl(AkBluetoothErrorCode.ServiceNotFound, "Service not found", ""));
|
||||
const chars = service.getCharacteristics();
|
||||
console.log(chars)
|
||||
const result : BleCharacteristic[] = [];
|
||||
if (chars != null) {
|
||||
const characteristicsList = chars;
|
||||
const size = characteristicsList.size;
|
||||
const bleService : BleService = {
|
||||
uuid: serviceId,
|
||||
isPrimary: service.getType() == BluetoothGattService.SERVICE_TYPE_PRIMARY
|
||||
};
|
||||
for (let i = 0 as Int; i < size; i++) {
|
||||
const char = characteristicsList != null ? characteristicsList.get(i as Int) : characteristicsList[i];
|
||||
if (char != null) {
|
||||
const props = char.getProperties();
|
||||
try {
|
||||
const charUuid = char.getUuid() != null ? char.getUuid().toString() : '';
|
||||
console.log('[ServiceManager] characteristic uuid=', charUuid);
|
||||
} catch (e) { console.warn('[ServiceManager] failed to read char uuid', e); }
|
||||
console.log(props);
|
||||
const bleCharacteristic : BleCharacteristic = {
|
||||
uuid: char.getUuid().toString(),
|
||||
service: bleService,
|
||||
properties: createCharProperties(props)
|
||||
};
|
||||
result.push(bleCharacteristic);
|
||||
}
|
||||
}
|
||||
}
|
||||
callback(result, null);
|
||||
}
|
||||
|
||||
public async readCharacteristic(deviceId : string, serviceId : string, characteristicId : string) : Promise<ArrayBuffer> {
|
||||
const gatt = this.deviceManager.getGattInstance(deviceId);
|
||||
if (gatt == null) throw new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", "");
|
||||
const service = gatt.getService(UUID.fromString(serviceId));
|
||||
if (service == null) throw new AkBleErrorImpl(AkBluetoothErrorCode.ServiceNotFound, "Service not found", "");
|
||||
const char = service.getCharacteristic(UUID.fromString(characteristicId));
|
||||
if (char == null) throw new AkBleErrorImpl(AkBluetoothErrorCode.CharacteristicNotFound, "Characteristic not found", "");
|
||||
const key = `${deviceId}|${serviceId}|${characteristicId}|read`;
|
||||
console.log(key)
|
||||
return new Promise<ArrayBuffer>((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
pendingCallbacks.delete(key);
|
||||
reject(new AkBleErrorImpl(AkBluetoothErrorCode.ConnectionTimeout, "Connection timeout", ""));
|
||||
}, 5000);
|
||||
const resolveAdapter = (data : any) => { console.log('read resolve:', data); resolve(data as ArrayBuffer); };
|
||||
const rejectAdapter = (err ?: any) => { reject(new AkBleErrorImpl(AkBluetoothErrorCode.UnknownError, "Unknown error occurred", "")); };
|
||||
pendingCallbacks.set(key, new PendingCallbackImpl(resolveAdapter, rejectAdapter, timer));
|
||||
if (gatt.readCharacteristic(char) == false) {
|
||||
clearTimeout(timer);
|
||||
pendingCallbacks.delete(key);
|
||||
reject(new AkBleErrorImpl(AkBluetoothErrorCode.UnknownError, "Unknown error occurred", ""));
|
||||
}
|
||||
else {
|
||||
console.log('read should be succeed', key)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async writeCharacteristic(deviceId : string, serviceId : string, characteristicId : string, data : Uint8Array, options ?: WriteCharacteristicOptions) : Promise<boolean> {
|
||||
console.log('[writeCharacteristic] deviceId:', deviceId, 'serviceId:', serviceId, 'characteristicId:', characteristicId, 'data:', data);
|
||||
const gatt = this.deviceManager.getGattInstance(deviceId);
|
||||
if (gatt == null) {
|
||||
console.error('[writeCharacteristic] gatt is null');
|
||||
throw new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", "");
|
||||
}
|
||||
const service = gatt.getService(UUID.fromString(serviceId));
|
||||
if (service == null) {
|
||||
console.error('[writeCharacteristic] service is null');
|
||||
throw new AkBleErrorImpl(AkBluetoothErrorCode.ServiceNotFound, "Service not found", "");
|
||||
}
|
||||
const char = service.getCharacteristic(UUID.fromString(characteristicId));
|
||||
if (char == null) {
|
||||
console.error('[writeCharacteristic] characteristic is null');
|
||||
throw new AkBleErrorImpl(AkBluetoothErrorCode.CharacteristicNotFound, "Characteristic not found", "");
|
||||
}
|
||||
const key = `${deviceId}|${serviceId}|${characteristicId}|write`;
|
||||
const wantsNoResponse = options != null && options.waitForResponse == false;
|
||||
let retryMaxAttempts = 20;
|
||||
let retryDelay = 100;
|
||||
let giveupTimeout = 20000;
|
||||
if (options != null) {
|
||||
try {
|
||||
if (options.maxAttempts != null) {
|
||||
const parsedAttempts = Math.floor(options.maxAttempts as number);
|
||||
if (!isNaN(parsedAttempts) && parsedAttempts > 0) retryMaxAttempts = parsedAttempts;
|
||||
}
|
||||
} catch (e) { }
|
||||
try {
|
||||
if (options.retryDelayMs != null) {
|
||||
const parsedDelay = Math.floor(options.retryDelayMs as number);
|
||||
if (!isNaN(parsedDelay) && parsedDelay >= 0) retryDelay = parsedDelay;
|
||||
}
|
||||
} catch (e) { }
|
||||
try {
|
||||
if (options.giveupTimeoutMs != null) {
|
||||
const parsedGiveup = Math.floor(options.giveupTimeoutMs as number);
|
||||
if (!isNaN(parsedGiveup) && parsedGiveup > 0) giveupTimeout = parsedGiveup;
|
||||
}
|
||||
} catch (e) { }
|
||||
}
|
||||
const gattInstance = gatt;
|
||||
const executeWrite = () : Promise<boolean> => {
|
||||
|
||||
return new Promise<boolean>((resolve) => {
|
||||
const initialTimeout = Math.max(giveupTimeout + 5000, 10000);
|
||||
let timer = setTimeout(() => {
|
||||
pendingCallbacks.delete(key);
|
||||
console.error('[writeCharacteristic] timeout');
|
||||
resolve(false);
|
||||
}, initialTimeout);
|
||||
console.log('[writeCharacteristic] initial timeout set to', initialTimeout, 'ms for', key);
|
||||
const resolveAdapter = (data : any) => {
|
||||
console.log('[writeCharacteristic] resolveAdapter called');
|
||||
resolve(true);
|
||||
};
|
||||
const rejectAdapter = (err ?: any) => {
|
||||
console.error('[writeCharacteristic] rejectAdapter called', err);
|
||||
resolve(false);
|
||||
};
|
||||
pendingCallbacks.set(key, new PendingCallbackImpl(resolveAdapter, rejectAdapter, timer));
|
||||
const byteArray = new ByteArray(data.length as Int);
|
||||
for (let i = 0 as Int; i < data.length; i++) {
|
||||
byteArray[i] = data[i].toByte();
|
||||
}
|
||||
const forceWriteTypeNoResponse = options != null && options.forceWriteTypeNoResponse == true;
|
||||
let usesNoResponse = forceWriteTypeNoResponse || wantsNoResponse;
|
||||
try {
|
||||
const props = char.getProperties();
|
||||
console.log('[writeCharacteristic] characteristic properties mask=', props);
|
||||
if (usesNoResponse == false) {
|
||||
const supportsWriteWithResponse = (props & BluetoothGattCharacteristic.PROPERTY_WRITE) !== 0;
|
||||
const supportsWriteNoResponse = (props & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) !== 0;
|
||||
if (supportsWriteWithResponse == false && supportsWriteNoResponse == true) {
|
||||
usesNoResponse = true;
|
||||
}
|
||||
}
|
||||
if (usesNoResponse) {
|
||||
try { char.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); } catch (e) { }
|
||||
console.log('[writeCharacteristic] using WRITE_TYPE_NO_RESPONSE');
|
||||
} else {
|
||||
try { char.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT); } catch (e) { }
|
||||
console.log('[writeCharacteristic] using WRITE_TYPE_DEFAULT');
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[writeCharacteristic] failed to inspect/set write type', e);
|
||||
}
|
||||
const maxAttempts = retryMaxAttempts;
|
||||
function attemptWrite(att : Int) : void {
|
||||
try {
|
||||
let setOk = true;
|
||||
try {
|
||||
const setRes = char.setValue(byteArray);
|
||||
if (typeof setRes == 'boolean' && setRes == false) {
|
||||
setOk = false;
|
||||
console.warn('[writeCharacteristic] setValue returned false for', key, 'attempt', att);
|
||||
}
|
||||
} catch (e) {
|
||||
setOk = false;
|
||||
console.warn('[writeCharacteristic] setValue threw for', key, 'attempt', att, e);
|
||||
}
|
||||
if (setOk == false) {
|
||||
if (att >= maxAttempts) {
|
||||
try { clearTimeout(timer); } catch (e) { }
|
||||
pendingCallbacks.delete(key);
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
setTimeout(() => { attemptWrite((att + 1) as Int); }, retryDelay);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
console.log('[writeCharacteristic] attempt', att, 'calling gatt.writeCharacteristic');
|
||||
const r = gattInstance.writeCharacteristic(char);
|
||||
console.log('[writeCharacteristic] attempt', att, 'result=', r);
|
||||
if (r == true) {
|
||||
if (usesNoResponse) {
|
||||
console.log('[writeCharacteristic] WRITE_TYPE_NO_RESPONSE success for', key);
|
||||
try { clearTimeout(timer); } catch (e) { }
|
||||
pendingCallbacks.delete(key);
|
||||
resolve(true);
|
||||
return;
|
||||
}
|
||||
try { clearTimeout(timer); } catch (e) { }
|
||||
const extra = 20000;
|
||||
timer = setTimeout(() => {
|
||||
pendingCallbacks.delete(key);
|
||||
console.error('[writeCharacteristic] timeout after write initiated');
|
||||
resolve(false);
|
||||
}, extra);
|
||||
const pendingEntry = pendingCallbacks.get(key);
|
||||
if (pendingEntry != null) pendingEntry.timer = timer;
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[writeCharacteristic] attempt', att, 'exception when calling writeCharacteristic', e);
|
||||
}
|
||||
if (att < maxAttempts) {
|
||||
const nextAtt = (att + 1) as Int;
|
||||
setTimeout(() => { attemptWrite(nextAtt); }, retryDelay);
|
||||
return;
|
||||
}
|
||||
if (usesNoResponse) {
|
||||
try { clearTimeout(timer); } catch (e) { }
|
||||
pendingCallbacks.delete(key);
|
||||
console.warn('[writeCharacteristic] all attempts failed with WRITE_NO_RESPONSE for', key);
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
try { clearTimeout(timer); } catch (e) { }
|
||||
const giveupTimeoutLocal = giveupTimeout;
|
||||
console.warn('[writeCharacteristic] all attempts failed; waiting for late callback up to', giveupTimeoutLocal, 'ms for', key);
|
||||
const giveupTimer = setTimeout(() => {
|
||||
pendingCallbacks.delete(key);
|
||||
console.error('[writeCharacteristic] giveup timeout expired for', key);
|
||||
resolve(false);
|
||||
}, giveupTimeoutLocal);
|
||||
const pendingEntryAfter = pendingCallbacks.get(key);
|
||||
if (pendingEntryAfter != null) pendingEntryAfter.timer = giveupTimer;
|
||||
} catch (e) {
|
||||
clearTimeout(timer);
|
||||
pendingCallbacks.delete(key);
|
||||
console.error('[writeCharacteristic] Exception in attemptWrite', e);
|
||||
resolve(false);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
attemptWrite(1 as Int);
|
||||
} catch (e) {
|
||||
clearTimeout(timer);
|
||||
pendingCallbacks.delete(key);
|
||||
console.error('[writeCharacteristic] Exception before attempting write', e);
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
};
|
||||
return enqueueDeviceWrite(deviceId, executeWrite);
|
||||
}
|
||||
|
||||
public async subscribeCharacteristic(deviceId : string, serviceId : string, characteristicId : string, onData : BleDataReceivedCallback) : Promise<void> {
|
||||
const gatt = this.deviceManager.getGattInstance(deviceId);
|
||||
if (gatt == null) throw new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", "");
|
||||
const service = gatt.getService(UUID.fromString(serviceId));
|
||||
if (service == null) throw new AkBleErrorImpl(AkBluetoothErrorCode.ServiceNotFound, "Service not found", "");
|
||||
const char = service.getCharacteristic(UUID.fromString(characteristicId));
|
||||
if (char == null) throw new AkBleErrorImpl(AkBluetoothErrorCode.CharacteristicNotFound, "Characteristic not found", "");
|
||||
const key = `${deviceId}|${serviceId}|${characteristicId}|notify`;
|
||||
notifyCallbacks.set(key, onData);
|
||||
if (gatt.setCharacteristicNotification(char, true) == false) {
|
||||
notifyCallbacks.delete(key);
|
||||
throw new AkBleErrorImpl(AkBluetoothErrorCode.UnknownError, "Failed to unsubscribe characteristic", "");
|
||||
} else {
|
||||
// 写入 CCCD 描述符,启用 notify
|
||||
const descriptor = char.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
|
||||
if (descriptor != null) {
|
||||
// 设置描述符值
|
||||
const value =
|
||||
BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
|
||||
|
||||
descriptor.setValue(value);
|
||||
const writedescript = gatt.writeDescriptor(descriptor);
|
||||
console.log('subscribeCharacteristic: CCCD written for notify', writedescript);
|
||||
} else {
|
||||
console.warn('subscribeCharacteristic: CCCD descriptor not found!');
|
||||
}
|
||||
console.log('subscribeCharacteristic ok!!');
|
||||
}
|
||||
}
|
||||
|
||||
public async unsubscribeCharacteristic(deviceId : string, serviceId : string, characteristicId : string) : Promise<void> {
|
||||
const gatt = this.deviceManager.getGattInstance(deviceId);
|
||||
if (gatt == null) throw new AkBleErrorImpl(AkBluetoothErrorCode.DeviceNotFound, "Device not found", "");
|
||||
const service = gatt.getService(UUID.fromString(serviceId));
|
||||
if (service == null) throw new AkBleErrorImpl(AkBluetoothErrorCode.ServiceNotFound, "Service not found", "");
|
||||
const char = service.getCharacteristic(UUID.fromString(characteristicId));
|
||||
if (char == null) throw new AkBleErrorImpl(AkBluetoothErrorCode.CharacteristicNotFound, "Characteristic not found", "");
|
||||
const key = `${deviceId}|${serviceId}|${characteristicId}|notify`;
|
||||
notifyCallbacks.delete(key);
|
||||
if (gatt.setCharacteristicNotification(char, false) == false) {
|
||||
throw new AkBleErrorImpl(AkBluetoothErrorCode.UnknownError, "Failed to unsubscribe characteristic", "");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 自动发现所有服务和特征
|
||||
public async autoDiscoverAll(deviceId : string) : Promise<AutoDiscoverAllResult> {
|
||||
const services = await this.getServices(deviceId, null) as BleService[];
|
||||
const allCharacteristics : BleCharacteristic[] = [];
|
||||
for (const service of services) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
this.getCharacteristics(deviceId, service.uuid, (chars, err) => {
|
||||
if (err != null) reject(err);
|
||||
else {
|
||||
if (chars != null) allCharacteristics.push(...chars);
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
return { services, characteristics: allCharacteristics };
|
||||
}
|
||||
|
||||
// 自动订阅所有支持 notify/indicate 的特征
|
||||
public async subscribeAllNotifications(deviceId : string, onData : BleDataReceivedCallback) : Promise<void> {
|
||||
const { services, characteristics } = await this.autoDiscoverAll(deviceId);
|
||||
for (const char of characteristics) {
|
||||
if (char.properties.notify || char.properties.indicate) {
|
||||
try {
|
||||
await this.subscribeCharacteristic(deviceId, char.service.uuid, char.uuid, onData);
|
||||
} catch (e) {
|
||||
// 可以选择忽略单个特征订阅失败
|
||||
console.warn(`订阅特征 ${char.uuid} 失败:`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user