// 蓝牙相关接口和类型定义 // 基础设备信息类型 export type BleDeviceInfo = { deviceId : string; name : string; RSSI ?: number; connected ?: boolean; // 新增 serviceId ?: string; writeCharId ?: string; notifyCharId ?: string; } export type AutoDiscoverAllResult = { services : BleService[]; characteristics : BleCharacteristic[]; } // 服务信息类型 export type BleServiceInfo = { uuid : string; isPrimary : boolean; } // 特征值属性类型 export type BleCharacteristicProperties = { read : boolean; write : boolean; notify : boolean; indicate : boolean; writeWithoutResponse ?: boolean; canRead ?: boolean; canWrite ?: boolean; canNotify ?: boolean; } // 特征值信息类型 export type BleCharacteristicInfo = { uuid : string; serviceId : string; properties : BleCharacteristicProperties; } // 错误状态码 export enum BleErrorCode { UNKNOWN_ERROR = 0, BLUETOOTH_UNAVAILABLE = 1, PERMISSION_DENIED = 2, DEVICE_NOT_CONNECTED = 3, SERVICE_NOT_FOUND = 4, CHARACTERISTIC_NOT_FOUND = 5, OPERATION_TIMEOUT = 6 } // 命令类型 export enum CommandType { BATTERY = 1, DEVICE_INFO = 2, CUSTOM = 99, TestBatteryLevel = 0x01 } // 错误接口 export type BleError { errCode : number; errMsg : string; errSubject ?: string; } // 连接选项 export type BleConnectOptions = { deviceId : string; timeout ?: number; success ?: (result : any) => void; fail ?: (error : BleError) => void; complete ?: (result : any) => void; } // 断开连接选项 export type BleDisconnectOptions = { deviceId : string; success ?: (result : any) => void; fail ?: (error : BleError) => void; complete ?: (result : any) => void; } // 获取特征值选项 export type BleCharacteristicOptions = { deviceId : string; serviceId : string; characteristicId : string; success ?: (result : any) => void; fail ?: (error : BleError) => void; complete ?: (result : any) => void; } // 写入特征值选项 export type BleWriteOptions = { deviceId : string; serviceId : string; characteristicId : string; value : Uint8Array; writeType ?: number; success ?: (result : any) => void; fail ?: (error : BleError) => void; complete ?: (result : any) => void; } // Options for writeCharacteristic helper export type WriteCharacteristicOptions = { waitForResponse ?: boolean; maxAttempts ?: number; retryDelayMs ?: number; giveupTimeoutMs ?: number; forceWriteTypeNoResponse ?: boolean; } // 通知特征值回调函数 export type BleNotifyCallback = (data : Uint8Array) => void; // 通知特征值选项 export type BleNotifyOptions = { deviceId : string; serviceId : string; characteristicId : string; state ?: boolean; // true: 启用通知,false: 禁用通知 onCharacteristicValueChange : BleNotifyCallback; success ?: (result : any) => void; fail ?: (error : BleError) => void; complete ?: (result : any) => void; } // 获取服务选项 export type BleDeviceServicesOptions = { deviceId : string; success ?: (result : BleServicesResult) => void; fail ?: (error : BleError) => void; complete ?: (result : any) => void; } // 获取特征值选项 export type BleDeviceCharacteristicsOptions = { deviceId : string; serviceId : string; success ?: (result : BleCharacteristicsResult) => void; fail ?: (error : BleError) => void; complete ?: (result : any) => void; } // 蓝牙扫描选项 export type BluetoothScanOptions = { services ?: string[]; timeout ?: number; onDeviceFound ?: (device : BleDeviceInfo) => void; success ?: (result : BleScanResult) => void; fail ?: (error : BleError) => void; complete ?: (result : any) => void; } // 扫描结果 // 服务结果 export type BleServicesResult = { services : BleServiceInfo[]; errMsg ?: string; } // 特征值结果 export type BleCharacteristicsResult = { characteristics : BleCharacteristicInfo[]; errMsg ?: string; } // 定义连接状态枚举 export enum BLE_CONNECTION_STATE { DISCONNECTED = 0, CONNECTING = 1, CONNECTED = 2, DISCONNECTING = 3 } // 电池状态类型定义 export type BatteryStatus = { batteryLevel : number; // 电量百分比 isCharging : boolean; // 充电状态 } // 蓝牙服务接口类型定义 - 转换为type类型 export type BleService = { uuid : string; isPrimary : boolean; } // 蓝牙特征值接口定义 - 转换为type类型 export type BleCharacteristic = { uuid : string; service : BleService; properties : BleCharacteristicProperties; } // PendingPromise接口定义 export interface PendingCallback { resolve : (data : any) => void; reject : (err ?: any) => void; timer ?: number; } // 蓝牙相关接口和类型定义 export type BleDevice = { deviceId : string; name : string; rssi ?: number; lastSeen ?: number; // 新增 // 新增 serviceId ?: string; writeCharId ?: string; notifyCharId ?: string; } // BLE常规选项 export type BleOptions = { timeout ?: number; success ?: (result : any) => void; fail ?: (error : any) => void; complete ?: () => void; } export type BleConnectionState = number; // 0: DISCONNECTED, 1: CONNECTING, 2: CONNECTED, 3: DISCONNECTING export type BleConnectOptionsExt = { timeout ?: number; services ?: string[]; requireResponse ?: boolean; autoReconnect ?: boolean; maxAttempts ?: number; interval ?: number; }; // 回调函数类型 export type BleDeviceFoundCallback = (device : BleDevice) => void; export type BleConnectionStateChangeCallback = (deviceId : string, state : BleConnectionState) => void; export type BleDataPayload = { deviceId : string; serviceId ?: string; characteristicId ?: string; data : string | ArrayBuffer; format ?: number; // 0: JSON, 1: XML, 2: RAW } export type BleDataSentCallback = (payload : BleDataPayload, success : boolean, error ?: BleError) => void; export type BleErrorCallback = (error : BleError) => void; // 健康数据类型定义 export enum HealthDataType { HEART_RATE = 1, BLOOD_OXYGEN = 2, TEMPERATURE = 3, STEP_COUNT = 4, SLEEP_DATA = 5, HEALTH_DATA = 6 } // Shared health notification payloads used by protocol handler and UI consumers export type HealthSubscription = { serviceUuid : string charUuid : string } export type WbbpPacket = { cmd : number seq : number payload : Uint8Array } export type HealthData = { type : 'heart' | 'spo2' | 'steps' | 'raw' cmd ?: number seq ?: number timestamp ?: number heartRate ?: number spo2 ?: number pulse ?: number steps ?: number distance ?: number calories ?: number quality ?: number activity ?: number raw ?: Uint8Array payload?: Uint8Array; data?: Uint8Array; } // Platform-specific services should be imported from per-platform entrypoints // (e.g. './app-android/index.uts' or './web/index.uts'). // Avoid re-exporting platform modules at the SDK root to prevent bundlers // Platform-specific services should be imported from per-platform entrypoints // (e.g. './app-android/index.uts' or './web/index.uts'). // Avoid re-exporting platform modules at the SDK root to prevent bundlers // from pulling android.* symbols into web bundles. // If a typed ambient reference is required, declare the shape here instead of importing implementation. // Example lightweight typed placeholder (do not import platform code here): // export type BluetoothService = any; // platform-specific implementation exported from platform index files // ====== 新增多协议、统一事件、协议适配、状态管理支持 ====== export type BleProtocolType = | 'standard' | 'custom' | 'health' | 'ibeacon' | 'mesh'; export type BleEvent = | 'deviceFound' | 'scanFinished' | 'connectionStateChanged' | 'dataReceived' | 'dataSent' | 'error' | 'servicesDiscovered' | 'connected' // 新增 | 'disconnected'; // 新增 // 事件回调参数 export type BleEventPayload = { event : BleEvent; device ?: BleDevice; protocol ?: BleProtocolType; state ?: BleConnectionState; data ?: ArrayBuffer | string | object; format ?: string; error ?: BleError; extra ?: any; } // 事件回调函数 export type BleEventCallback = (payload : BleEventPayload) => void; // 多协议设备信息(去除交叉类型,直接展开字段) export type MultiProtocolDevice = { deviceId : string; name : string; rssi ?: number; protocol : BleProtocolType; }; export type ScanDevicesOptions = { protocols ?: BleProtocolType[]; optionalServices ?: string[]; timeout ?: number; onDeviceFound ?: (device : BleDevice) => void; onScanFinished ?: () => void; }; // Named payload type used by sendData export type SendDataPayload = { deviceId : string; serviceId ?: string; characteristicId ?: string; data : string | ArrayBuffer; format ?: number; protocol : BleProtocolType; } // 协议处理器接口(为 protocol-handler 适配器预留) export type ScanHandler = { protocol : BleProtocolType; scanDevices ?: (options : ScanDevicesOptions) => Promise; connect : (device : BleDevice, options ?: BleConnectOptionsExt) => Promise; disconnect : (device : BleDevice) => Promise; // Optional: send arbitrary data via the protocol's write characteristic sendData ?: (device : BleDevice, payload : SendDataPayload, options ?: BleOptions) => Promise; // Optional: try to connect and discover service/characteristic ids for this device autoConnect ?: (device : BleDevice, options ?: BleConnectOptionsExt) => Promise; } // 自动发现服务和特征返回类型 export type AutoBleInterfaces = { serviceId : string; writeCharId : string; notifyCharId : string; } export type ResponseCallbackEntry = { cb : (data : Uint8Array) => boolean | void; multi : boolean; }; // Result returned by a DFU control parser. Use a plain string `type` to keep // the generated Kotlin simple and avoid inline union types which the generator // does not handle well. export type ControlParserResult = { type : string; // e.g. 'progress', 'success', 'error', 'info' progress ?: number; error ?: any; } // DFU types export type DfuOptions = { mtu ?: number; useNordic ?: boolean; // If true, the DFU upload will await a write response per-packet. Set false to use // WRITE_NO_RESPONSE (fire-and-forget) for higher throughput. Default: false. waitForResponse ?: boolean; // Maximum number of outstanding NO_RESPONSE writes to allow before throttling. // This implements a simple sliding window. Default: 32. maxOutstanding ?: number; // Per-chunk sleep (ms) to yield to event loop / Android BLE stack. Default: 2. writeSleepMs ?: number; // Retry delay (ms) used by the Android write helper when gatt.writeCharacteristic // returns false. Smaller values can improve throughput on congested stacks. writeRetryDelayMs ?: number; // Maximum number of immediate write attempts before falling back to the give-up timeout. writeMaxAttempts ?: number; // Timeout (ms) to wait for a late onCharacteristicWrite callback after all retries fail. writeGiveupTimeoutMs ?: number; // Packet Receipt Notification (PRN) window size in packets. If set, DFU // manager will send a Set PRN command to the device and wait for PRN // notifications after this many packets. Default: 12. prn ?: number; // Timeout (ms) to wait for a PRN notification once the window is reached. // Default: 10000 (10s). prnTimeoutMs ?: number; // When true, disable PRN waits automatically after the first timeout to prevent // repeated long stalls on devices that do not send PRNs. Default: true. disablePrnOnTimeout ?: boolean; // Time (ms) to wait for outstanding fire-and-forget writes to drain before issuing // the activate/validate control command. Default: 3000. drainOutstandingTimeoutMs ?: number; controlTimeout ?: number; onProgress ?: (percent : number) => void; onLog ?: (message : string) => void; controlParser ?: (data : Uint8Array) => ControlParserResult | null; } export type DfuManagerType = { startDfu : (deviceId : string, firmwareBytes : Uint8Array, options ?: DfuOptions) => Promise; } // Lightweight runtime / UTS shims and missing types // These are conservative placeholders to satisfy typings used across platform files. // UTSJSONObject: bundler environments used by the build may not support // TypeScript-style index signatures in this .uts context. Use a conservative // 'any' alias so generated code doesn't rely on unsupported syntax while // preserving a usable type at the source level. export type UTSJSONObject = any; // ByteArray / Int are used in the Android platform code to interop with Java APIs. // Define minimal aliases so source can compile. Runtime uses Uint8Array and number. export type ByteArray = any; // runtime will use Java byte[] via UTS bridge; keep as any here // Callback types used by service_manager and index wrappers export type BleDataReceivedCallback = (data: Uint8Array) => void; export type BleScanResult = { deviceId: string; name?: string; rssi?: number; advertising?: any; }; // Minimal UI / framework placeholders (some files reference these in types only) export type ComponentPublicInstance = any; export type UniElement = any; export type UniPage = any; // Platform service contract (actual implementations live in per-platform entrypoints). // Provide a lightweight interface so source-level code can rely on concrete method // names and signatures without emitting duplicate runtime classes from this shared file. export interface BluetoothService { // Event emitter style on(event: BleEvent | string, callback: BleEventCallback): void; off(event: BleEvent | string, callback?: BleEventCallback): void; // Scanning / discovery scanDevices(options?: ScanDevicesOptions): Promise; // Connection management connectDevice(deviceId: string, protocol?: string, options?: BleConnectOptionsExt): Promise; disconnectDevice(deviceId: string, protocol?: string): Promise; getConnectedDevices(): MultiProtocolDevice[]; // Services / characteristics getServices(deviceId: string): Promise; getCharacteristics(deviceId: string, serviceId: string): Promise; // Read / write / notify readCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise; writeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, value: Uint8Array | ArrayBuffer, options?: WriteCharacteristicOptions): Promise; subscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string, callback: BleNotifyCallback): Promise; unsubscribeCharacteristic(deviceId: string, serviceId: string, characteristicId: string): Promise; // Convenience helpers getAutoBleInterfaces(deviceId: string): Promise; } // Runtime protocol handler base class. Exporting a concrete class ensures the // generator emits a resolvable runtime type that platform handlers can extend. // Source-level code can still use the ScanHandler type for typing. // Runtime ProtocolHandler is implemented in `protocol_handler.uts`. // Keep the public typing in this file minimal to avoid duplicate runtime // declarations. Consumers that need the runtime class should import it from // './protocol_handler.uts'.