- uni.writeBLECharacteristicValue(OBJECT)
- uni.readBLECharacteristicValue(OBJECT)
- uni.onBLEConnectionStateChange(CALLBACK)
- uni.onBLECharacteristicValueChange(CALLBACK)
- uni.notifyBLECharacteristicValueChange(OBJECT)
- uni.getBLEDeviceServices(OBJECT)
- uni.getBLEDeviceCharacteristics(OBJECT)
- uni.createBLEConnection(OBJECT)
- uni.closeBLEConnection(OBJECT)
低功耗蓝牙 API 平台差异说明
uni.writeBLECharacteristicValue(OBJECT)
向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。 OBJECT 参数说明
错误
注意
notifyBLECharacteristicValueChange成功后立即调用writeBLECharacteristicValue接口,在部分机型上会发生 10008 系统错误示例代码
// 向蓝牙设备发送一个0x00的16进制数据const buffer = new ArrayBuffer(1)const dataView = new DataView(buffer)dataView.setUint8(0, 0)uni.writeBLECharacteristicValue({ // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId, // 这里的value是ArrayBuffer类型 value: buffer, success(res) { console.log('writeBLECharacteristicValue success', res.errMsg) }})
uni.readBLECharacteristicValue(OBJECT)
读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持 read 才可以成功调用。 OBJECT 参数说明错误
注意
onBLECharacteristicValueChange方法注册的回调中获取。示例代码
// 必须在这里的回调才能获取uni.onBLECharacteristicValueChange(function (characteristic) { console.log('characteristic value comed:', characteristic)})uni.readBLECharacteristicValue({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId, success(res) { console.log('readBLECharacteristicValue:', res.errCode) }})
uni.onBLEConnectionStateChange(CALLBACK)
监听低功耗蓝牙连接状态的改变事件。包括开发者主动连接或断开连接,设备丢失,连接异常断开等等 CALLBACK 返回参数示例代码
uni.onBLEConnectionStateChange(function (res) { // 该方法回调中可以用于处理连接意外断开等异常情况 console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)})
uni.onBLECharacteristicValueChange(CALLBACK)
notifyBLECharacteristicValueChange接口才能接收到设备推送的 notification。 CALLBACK 返回参数示例代码
// ArrayBuffer转16进度字符串示例function ab2hex(buffer) { const hexArr = Array.prototype.map.call( new Uint8Array(buffer), function (bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join('')}uni.onBLECharacteristicValueChange(function (res) { console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`) console.log(ab2hex(res.value))})
uni.notifyBLECharacteristicValueChange(OBJECT)
notifyBLECharacteristicValueChange才能监听到设备characteristicValueChange事件 OBJECT 参数说明错误
注意
- 回调。
notifyBLECharacteristicValueChange成功后立即调用writeBLECharacteristicValue接口,在部分机型上会发生 10008 系统错误示例代码
uni.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId, // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取 characteristicId, success(res) { console.log('notifyBLECharacteristicValueChange success', res.errMsg) }})
uni.getBLEDeviceServices(OBJECT)
获取蓝牙设备所有服务(service)。 OBJECT 参数说明 success 返回参数说明: res.services 的结构错误
示例代码
uni.getBLEDeviceServices({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, success(res) { console.log('device services:', res.services) }})
uni.getBLEDeviceCharacteristics(OBJECT)
获取蓝牙设备某个服务中所有特征值(characteristic)。 OBJECT 参数说明 success 返回参数说明: res.characteristics 的结构 properties 的结构错误
示例代码
uni.getBLEDeviceCharacteristics({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId, success(res) { console.log('device getBLEDeviceCharacteristics:', res.characteristics) }})
uni.createBLEConnection(OBJECT)
连接低功耗蓝牙设备。 若APP在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需进行搜索操作。 OBJECT 参数说明错误
注意
createBLEConnection和closeBLEConnection接口。安卓如果多次调用createBLEConnection创建连接,有可能导致系统持有同一设备多个连接的实例,导致调用closeBLEConnection的时候并不能真正的断开与设备的连接。- 回调事件,当蓝牙设备断开时按需执行重连操作
-
示例代码
uni.createBLEConnection({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, success(res) { console.log(res) }})
uni.closeBLEConnection(OBJECT)
断开与低功耗蓝牙设备的连接。 OBJECT 参数说明错误
示例代码
uni.closeBLEConnection({ deviceId, success(res) { console.log(res) }})
在 GitHub 上编辑此页面!
