小程序蓝牙-监听方法接收数据丢失问题
发布于 6 年前 作者 uxiong 1793 次浏览 来自 问答

安卓系统(已测试多种版本的安卓手机和微信版本),在使用小程序的BLE蓝牙功能时,通过nofity接收数据,均出现了不同程度的数据不完整情况(数据量大概2万字节左右 20kb左右 设备端每次发送20字节),但是在IOS系统下就没出现问题。

//收到数据时回调

var that = this;    

wx.onBLECharacteristicValueChange(function (characteristic) {        

   that.data.str16 += that.ab2hex(characteristic.value);        

   clearTimeout(that.data.eTimeout);        

   that.data.eTimeout = setTimeout(function () {          

       console.log(’—监听数据返回----’, that.data.str16);        

   }, 1000);          

});

ab2hex:function(buffer) {

    var hexArr = Array.prototype.map.call(

        new Uint8Array(buffer),

        function (bit) {

            return (‘00’ + bit.toString(16)).slice(-2)

        }

    )

    return hexArr.join(’’);

}

PS:如果安卓手机第一次连接蓝牙,然后通过nofity接收数据,这时数据可能接收完成,这时退出该页面,断开蓝牙,释放蓝牙,再次进入这个页面时,连接蓝牙,再次通过nofity接收数据,就必出现数据不完成数据情况; ios上都没有此问题。

释放蓝牙代码如下:

//断开蓝牙连接

var that = this;

if (that.data.connectedDeviceId){

   wx.closeBLEConnection({

       deviceId: that.data.connectedDeviceId,

       success: function (res) {

       }

   })

}

//释放蓝牙

wx.closeBluetoothAdapter({

   success: function () {

           console.log(’----释放蓝牙成功----’);//已打印出来

   }

});

麻烦小程序官方给个解决方案

1 回复

我加一下你微信了解一下这个问题吧,单从你的描述也不能复现问题,定位问题。

回到顶部