• 小程序不会对写入数据包大小做限制,但系统与蓝牙设备会限制蓝牙4.0单次传输的数据大小,超过最大字节数后会发生写入错误,建议每次写入不超过20字节。

可能你已经在用小程序开发BLE应用了,也遇上了这个问题,我这儿是一种写法,并不限于这一种,仅供参考

    
let buffer = BLEControl.addPassUser(1,1,0,"123456");
let pos = 0;
let bytes = buffer.byteLength;
while(bytes > 0) {
  let tmpBuffer;
  if(bytes > 20) {
    tmpBuffer = buffer.slice(pos, pos + 20);
    pos += 20;
    bytes -= 20;
  } else {
    tmpBuffer = buffer.slice(pos, pos + bytes);
    pos += bytes;
    bytes -= bytes;
  }
  wx.writeBLECharacteristicValue({
    deviceId: this._deviceId,
    serviceId: this._serviceId,
    characteristicId: this._characteristicId,
    value: tmpBuffer,
  })
}

原创文章,转载请注明: 转载自贝壳博客

本文链接地址: 微信小程序蓝牙BLE发送数据包大于20字节的写法