更新记录
1.0.0(2026-07-03)
- 首发 BleLib 实例化 API
- 支持 Android / iOS App 端 BLE 扫描、连接、读写与广播
- Harmony 保留 stub
平台兼容性
uni-app(5.14)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | - | - | √ | - | √ | √ | - |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| - | - | - | - | - | - | - | - | - | - | - | - |
uni-app x(5.14)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| - | - | √ | √ | - | - |
hans-ble-kit
BLE UTS 插件,目标是用同一套 BleLib 实例 API 同时服务 uni-app 和 uni-app x 的 Android / iOS App 端。
支持平台
| 平台 | 状态 | 说明 |
|---|---|---|
| Android App | Y | central 主链路、状态查询、广播入口 |
| iOS App | Y | central 主链路、状态查询、设置页引导 |
| Harmony | - | stub |
| Web / 小程序 | - | 不在本期范围 |
快速接入
import { BleLib } from '@/uni_modules/hans-ble-kit'
const ble = new BleLib()
ble.setDebugMode(true)
ble.onBluetoothStateListener((res) => {
console.log('state', res)
})
ble.startScanBleDevice({
namePrefix: 'demo',
timeout: 3000,
autoStop: true,
})
API 概览
- 状态:
isEnabled、getBluetoothSystemSetting、getBluetoothAuthorizeSetting、openBluetooth、openBluetoothSettings、onBluetoothStateListener、offBluetoothStateListener - 扫描:
startScanBleDevice、stopScanBle、onScanDataListener、offScanDataListener - 连接:
connect、close、onConnectStateListener、offConnectStateListener、isConnected、getConnectMac - 服务与特征:
scanServices、getUUIDs、selectUUID(serviceId)、getServiceUUID、getNotifyUUID、getWriteUUID、getReadUUID - 广播:
isAdvertising、startAdvertise、stopAdvertise、onAdvertiseStateListener、offAdvertiseStateListener - 读写:
onNotifyBleData、offNotifyBleData、onReadData、offReadData、sendData、readRssi、setMtu、setPhy2MMode - 编解码:
encodeText、decodeText - 调试与生命周期:
setDebugMode、setThreadTypeName、setIOSConnectNotifyAlertEnabled、destroy
扫描、广播、写入配置对象按 UTSJSONObject 接收;同步返回值和回调 res 也直接使用 UTSJSONObject。插件不导出设备、通知、结果等强类型别名,字段契约以本文档为准。
通用结果
除同步布尔值、字符串和数组 getter 外,带回调的 API 均返回同一层 UTSJSONObject 结果:
type: number,事件类型。0表示同步请求成功,1001状态,1002扫描,1003连接,1004服务,1005通知,1006读取,1007写入,1008RSSI,1009MTU,1010PHY,1011广播,1099错误。message: string,结果说明。data: any,具体数据,按事件类型变化。instanceId: number,BleLib实例 id。platform: string,android/ios/harmony。timestamp: number,毫秒时间戳。
错误结果
当 type == 1099 时,data 固定为错误对象:
errSubject: string,固定为hans-ble-kit。errCode: number,插件错误码。errMsg: string,稳定错误消息。method: string,触发错误的 API 名;原生异步事件无法精确归因时为nativeEvent。details: UTSJSONObject | null,原生层带回的上下文,例如reason、status、code、mac、serviceId、characteristicId、payload。
错误码:
9010001: unsupported9010002: invalid parameter9010003: bluetooth disabled9010004: permission denied9010005: not initialized9010006: scan failed9010007: connect failed9010008: disconnect failed9010009: service not found9010010: characteristic not found9010011: notify failed9010012: read failed9010013: write failed9010014: rssi failed9010015: mtu unsupported9010016: phy unsupported9010017: settings unavailable9010018: advertise failed9010019: not connected9010099: unknown
data 字段契约
系统设置,来自 getBluetoothSystemSetting() 或状态事件:
bluetoothSupported: boolean,当前平台/设备是否支持蓝牙。bluetoothEnabled: boolean,系统蓝牙是否开启。bluetoothError: string | null,不可用原因。
授权设置,来自 getBluetoothAuthorizeSetting() 或状态事件:
bluetoothAuthorized: string,authorized/denied/not determined/config error/unsupported。scanAuthorized: string,扫描授权状态。connectAuthorized: string,连接授权状态。advertiseAuthorized: string,广播授权状态。scanReady: boolean,是否可直接扫描。connectReady: boolean,是否可直接连接。advertiseReady: boolean,是否可直接广播。authError: string | null,授权或配置错误。
扫描设备,来自 onScanDataListener 或 startScanBleDevice({ onScanResult }):
deviceId: string,平台设备 id;Android 通常等于 MAC,iOS 通常为 peripheral UUID。mac: string | null,设备地址;iOS 无真实 MAC 时为空。name: string | null,设备名。device: UTSJSONObject,含address、name。rssi: number | null,信号强度。connectable: boolean,是否可连接。services: string[],广播中的 service UUID。manufacturerData: string | null,十六进制厂商数据。serviceData: string | null,十六进制 service data。advertisement: string | null,十六进制广播原始数据。
连接状态,来自 connect、close、onConnectStateListener 或连接事件。connect/close 的回调会先收到请求回执,并继续收到本次连接生命周期内的成功、失败或断开事件:
mac: string | null,连接目标。deviceId: string | null,平台设备 id。autoConnect: boolean | null,仅请求回执中可能存在。
服务 profile,来自 scanServices 和 getUUIDs():
serviceId: string,服务 UUID。serviceName: string | null,服务名。readId: string | null,读取特征 UUID。writeId: string | null,写入特征 UUID。notifyId: string | null,通知特征 UUID。indicateId: string | null,indicate 特征 UUID。properties: string[],如read、write、notify、indicate。
通知/读取数据,来自 onNotifyBleData、onReadData 或异步通知:
serviceId: string,服务 UUID。characteristicId: string,特征 UUID。hexStrData: string,十六进制数据。data: number[],字节数组。buffer: ArrayBuffer | null,平台可用时返回。mac: string | null,设备地址或设备 id。
写入回执,来自 sendData:
serviceId: string,服务 UUID。characteristicId: string,写入特征 UUID。payload: string,十六进制写入内容。withoutResponse: boolean,是否无响应写。segmentSize: number,分包大小,0表示不强制分包。segmentInterval: number,分包间隔毫秒。
RSSI / MTU / PHY / 广播:
- RSSI
data:rssinumber。 - MTU
data:mtunumber。 - PHY
data:txPhynumber、rxPhynumber、statusnumber。 - 广播
data:isAdvertisingboolean;失败时错误上下文在details.code或details.reason。
options 字段契约
startScanBleDevice(options, cb?):
serviceUUIDs: string[],按服务 UUID 过滤。deviceIds: string[],按设备 id 过滤。name: string,按完整设备名过滤。namePrefix: string,按设备名前缀过滤。allowDuplicates: boolean,是否允许重复上报。autoStop: boolean,超时后是否自动停止。timeout: number,扫描超时毫秒;大于 0 时默认autoStop=true。scanMode: number,Android 扫描模式,默认2。onScanResult: function,单设备扫描回调。scanComplete: function,停止扫描回调。
startAdvertise(options, cb?):
profile: UTSJSONObject | null,服务 profile。serviceId: string,服务 UUID,优先级高于profile.serviceId。serviceName: string,服务名。localName: string,本机广播名。connectable: boolean,是否可连接。includeDeviceName: boolean,是否携带设备名。includeTxPowerLevel: boolean,是否携带发射功率。autoStop: boolean,超时后是否自动停止。timeout: number,广播超时毫秒。onAdvertiseResult: function,停止广播回调。
sendData(data, cb?):
serviceId: string,服务 UUID;为空时使用当前选中的 service。characteristicId: string,写入特征 UUID;为空时使用当前选中的 write/notify UUID。buffer: ArrayBuffer,写入二进制数据。data: number[],写入字节数组。hexStrData: string,写入十六进制字符串。text: string,写入文本。charset: string,utf-8/hex。withoutResponse: boolean,是否无响应写。segmentSize: number,分包大小。segmentInterval: number,分包间隔毫秒。
监听器生命周期
onBluetoothStateListener(cb)/offBluetoothStateListener():注册或移除蓝牙状态监听。onScanDataListener(cb)/offScanDataListener():注册或移除扫描结果监听;不会停止扫描。onConnectStateListener(cb)/offConnectStateListener():注册或移除连接状态监听;不会主动连接或断开。onAdvertiseStateListener(cb)/offAdvertiseStateListener():注册或移除广播状态监听;不会停止广播。onNotifyBleData(serviceId, characteristicId, enable, cb?)/offNotifyBleData():注册或移除通知数据回调;关闭底层通知仍应调用onNotifyBleData(..., false)。onReadData(serviceId, characteristicId, cb?)/offReadData():发起读取并注册读取回调;offReadData()只移除后续读取事件回调。
服务 profile 保留在插件内部,页面按 serviceId 调用 selectUUID 选择后,通过 getter 读取当前 UUID。
当前已知边界
- iOS
setMtu/setPhy2MMode当前明确返回unsupported。 - iOS peripheral / advertising 当前明确返回
unsupported,发布配置中不声明NSBluetoothPeripheralUsageDescription或bluetooth-peripheral后台模式。 - 广播 / peripheral 能力已经有入口,但仍需要按验证清单补齐真机验收。
权限说明
- Android:声明
BLUETOOTH、BLUETOOTH_ADMIN、ACCESS_FINE_LOCATION、ACCESS_COARSE_LOCATION、BLUETOOTH_SCAN、BLUETOOTH_CONNECT、BLUETOOTH_ADVERTISE,用于扫描、连接、读写和广播 BLE 设备。 - iOS:声明
NSBluetoothAlwaysUsageDescription,用于扫描、连接和读写 BLE 设备。 - 插件不主动上传蓝牙数据到服务器;业务侧如需存储或上传扫描结果、设备标识或读写数据,应自行完成用户授权和隐私声明。

收藏人数:
购买普通授权版(
试用
使用 HBuilderX 导入示例项目
赞赏(0)
下载 376
赞赏 0
下载 12387838
赞赏 1928
赞赏
京公网安备:11010802035340号