赛捷软件论坛's Archiver

vicky.yu 发表于 2026-6-9 10:44

Bug & Tip

tip: 并行调用多次读写接口存在读写失败的可能性。
tip:read接口读取到的信息需要在onBLECharacteristicValueChange方法注册的回调中获取。
wx.writeBLECharacteristicValue(OBJECT)
基础库版本 1.1.0 开始支持,低版本需做兼容处理
向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持write才可以成功调用,具体参照 characteristic 的 properties 属性

tips: 并行调用多次读写接口存在读写失败的可能性

OBJECT参数说明:

参数        类型        必填        说明
deviceId        string        是        蓝牙设备 id,参考 device 对象
serviceId        string        是        蓝牙特征值对应服务的 uuid
characteristicId        string        是        蓝牙特征值的 uuid
value        ArrayBuffer        是        蓝牙设备特征值对应的二进制值(注意:vConsole 无法打印出 ArrayBuffer 类型数据)
success        Function        是        成功则返回本机蓝牙适配器状态
fail        Function        否        接口调用失败的回调函数
complete        Function        否        接口调用结束的回调函数(调用成功、失败都会执行)
success返回参数:

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

// 这里的回调可以获取到 write 导致的特征值改变
wx.onBLECharacteristicValueChange(function(characteristic) {
  console.log('characteristic value changed:', characteristic)
})

// 向蓝牙设备发送一个0x00的16进制数据
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0, 0)

wx.writeBLECharacteristicValue({
  // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
  deviceId: deviceId,
  // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
  serviceId: serviceId,
  // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
  characteristicId: characteristicId,
  // 这里的value是ArrayBuffer类型
  value: buffer,
  success: function (res) {
    console.log('writeBLECharacteristicValue success', res.errMsg)
  }
})

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.