title: Taro.writeBLECharacteristicValue(option)

sidebar_label: writeBLECharacteristicValue

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

注意

  • 并行调用多次会存在写失败的可能性。
  • 小程序不会对写入数据包大小做限制,但系统与蓝牙设备会限制蓝牙4.0单次传输的数据大小,超过最大字节数后会发生写入错误,建议每次写入不超过20字节。
  • 若单次写入数据过长,iOS 上存在系统不会有任何回调的情况(包括错误回调)。
  • 安卓平台上,在调用 notifyBLECharacteristicValueChange 成功后立即调用 writeBLECharacteristicValue 接口,在部分机型上会发生 10008 系统错误

支持情况:writeBLECharacteristicValue - 图1 writeBLECharacteristicValue - 图2 writeBLECharacteristicValue - 图3

参考文档

类型

  1. (option: Option) => Promise<Promised>

参数

参数 类型
option Option

Promised

参数 类型 说明
errMsg string 成功:ok,错误:详细信息

Option

参数 类型 必填 说明
characteristicId string 蓝牙特征值的 uuid
deviceId string 蓝牙设备 id
serviceId string 蓝牙特征值对应服务的 uuid
value ArrayBuffer 蓝牙设备特征值对应的二进制值
complete (res: TaroGeneral.BluetoothError) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: TaroGeneral.BluetoothError) => void 接口调用失败的回调函数
success (res: TaroGeneral.BluetoothError) => void 接口调用成功的回调函数

示例代码

  1. // 向蓝牙设备发送一个0x00的16进制数据
  2. let buffer = new ArrayBuffer(1)
  3. let dataView = new DataView(buffer)
  4. dataView.setUint8(0, 0)
  5. Taro.writeBLECharacteristicValue({
  6. // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
  7. deviceId,
  8. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  9. serviceId,
  10. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  11. characteristicId,
  12. // 这里的value是ArrayBuffer类型
  13. value: buffer,
  14. success: function (res) {
  15. console.log('writeBLECharacteristicValue success', res.errMsg)
  16. }
  17. })