低功耗蓝牙 API 平台差异说明

uni.writeBLECharacteristicValue(OBJECT)

向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。 OBJECT 参数说明

错误

注意

  • notifyBLECharacteristicValueChange 成功后立即调用 writeBLECharacteristicValue 接口,在部分机型上会发生 10008 系统错误

    示例代码

    1. // 向蓝牙设备发送一个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 方法注册的回调中获取。

    示例代码

    1. // 必须在这里的回调才能获取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 返回参数

    示例代码

    1. uni.onBLEConnectionStateChange(function (res) { // 该方法回调中可以用于处理连接意外断开等异常情况 console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)})

    uni.onBLECharacteristicValueChange(CALLBACK)

    notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification。 CALLBACK 返回参数

    示例代码

    1. // 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 系统错误

    示例代码

    1. 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 的结构

    错误

    示例代码

    1. uni.getBLEDeviceServices({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, success(res) { console.log('device services:', res.services) }})

    uni.getBLEDeviceCharacteristics(OBJECT)

    获取蓝牙设备某个服务中所有特征值(characteristic)。 OBJECT 参数说明 success 返回参数说明: res.characteristics 的结构 properties 的结构

    错误

    示例代码

    1. uni.getBLEDeviceCharacteristics({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId, success(res) { console.log('device getBLEDeviceCharacteristics:', res.characteristics) }})

    uni.createBLEConnection(OBJECT)

    连接低功耗蓝牙设备。 若APP在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需进行搜索操作。 OBJECT 参数说明

    错误

    注意

  • createBLEConnectioncloseBLEConnection 接口。安卓如果多次调用 createBLEConnection 创建连接,有可能导致系统持有同一设备多个连接的实例,导致调用 closeBLEConnection 的时候并不能真正的断开与设备的连接。
  • 回调事件,当蓝牙设备断开时按需执行重连操作 -

    示例代码

    1. uni.createBLEConnection({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId, success(res) { console.log(res) }})

    uni.closeBLEConnection(OBJECT)

    断开与低功耗蓝牙设备的连接。 OBJECT 参数说明

    错误

    示例代码

    1. uni.closeBLEConnection({ deviceId, success(res) { console.log(res) }})

在 GitHub 上编辑此页面!