收藏
回答

小程序蓝牙读取大量数据会丢包

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug wx.onBLECharacteristicValueChange 工具 7.0.3 2.6.6

- 当前 Bug 的表现(可附上截图)


- 预期表现


- 复现路径


- 提供一个最简复现 Demo


notifyBLECharacteristicValueChange: function (){

var that = this;

wx.notifyBLECharacteristicValueChange({

state: true, // 启用 notify 功能

deviceId: that.data.deviceId,

serviceId: that.data.serviceId,

characteristicId: that.data.notifyCharacteristicId,

success: function (res) {

console.log("notify启动成功");

setTimeout(function (){

// ble.monitor_ble(that.data.deviceId,that.data.serviceId,that.data.writeCharacteristicId);

ble.refresh_ble(that.data.connected,that.data.deviceId, that.data.serviceId, that.data.writeCharacteristicId);

}//这个是我做的一个发送刷新命令的函数

,2000

);

setTimeout(function () {

wx.hideToast();

}, 2000);

},

fail: function (res) {

wx.showToast({

title: 'notify启动失败',

mask: true

});


}


})

},


/***我把那条函数封装成模块了*/

function ble_write_fun(orderStr, deviceId, serviceId, writeId) {

console.log("orderStr:", orderStr);

let order = utils.stringToBytes(orderStr);//字符串转byte

let byteLength = order.byteLength;

console.log("byteLength:", byteLength);

wx.writeBLECharacteristicValue({

deviceId: deviceId,

serviceId: serviceId,

characteristicId: writeId,

// 这里的value是ArrayBuffer类型(二进制数组)

value: order.slice(0, 20),//从0-20个字节的数据

success: function (res) {

// console.log("value:", utils.ab2str(order));

onBLECharacteristicValueChange(deviceId, serviceId, writeId);

},

fail: function (res) {

console.log("res:", res);

}

})

}

function onBLECharacteristicValueChange(deviceId, serviceId, writeId) {

wx.onBLECharacteristicValueChange(function (res) {

console.log("res.value:", utils.ab2str(res.value));

var resValueStr = utils.ab2str(res.value);//2进制数组转成字符串

console.log("读到字符串", resValueStr);

rev_page_parse(resValueStr,deviceId, serviceId, writeId);

});

}


function refreshPara(connected, deviceId, serviceId, writeId) {

if (connected) {

ble_write_fun(get_cmd, deviceId, serviceId, writeId);//写入发送

}

else {

app.showModal1("蓝牙已断开");

}

}


问题是这样。我发送一个刷新命令后开始监听读取数据。数据很小到问题不明显,设备发了528个字节。。然后在APP看到是完整收到,换成小程序后收到的是断断续续不确定。然后设备分包成20个字节发送出来,结论也一样,设备延时发送也一样,,问题是啥情况。。设备发送的数据




回答关注问题邀请回答
收藏

2 个回答

  • Tummy
    Tummy
    2019-04-17
    1. Android和ios都有问题么..

    2. android用的是系统接口,每包接受的大小是有限制的,20个字节..

      这里最好可以试试https://github.com/search?q=ble原生app看看有没有类似问题,降低排查问题的难度。万分感谢

    2019-04-17
    有用
    回复 13
    • 圈圈
      圈圈
      2019-04-17

      我用过蓝牙模块的APP啊。。这个APP是完整的数据。。。苹果我没测过。。我安卓测的是APP没问题的。。小程序接受不完整。。

      2019-04-17
      回复
    • Tummy
      Tummy
      2019-04-18回复圈圈

      给个微信号,我加一下你了解下具体的情况,那听起来是小程序有差异导致的

      2019-04-18
      回复
    • Tummy
      Tummy
      2019-04-18回复圈圈

      看了下,应该是多次回调中的几次回调有异常导致的。

      2019-04-18
      回复
    • 圈圈
      圈圈
      2019-04-18回复Tummy

      测出来的结果是分包刚好碰到中文,导致上个包和下个包的中文字被分离,而我是边接收边解析转编码,导致编码有问题。。谢谢大佬,,还有我是写C的。。只是顺便兼职做下前端而已。。

      2019-04-18
      回复
    • Wm
      Wm
      2019-08-20
      我现在也遇到蓝牙发送丢包(概率出现)的问题
      2019-08-20
      回复
    查看更多(8)
  • 拾忆
    拾忆
    2020-12-22

    我现在也遇到蓝牙发送丢包(概率出现)的问题,大哥你这个解决了嘛

    2020-12-22
    有用
    回复
登录 后发表内容