(附上测试代码与图片)BLE蓝牙断开后重新连接收到多个相同数据包
发布于 5 年前 作者 qgong 17138 次浏览 来自 问答

之前反应过这个问题,但是没有人回应,以为是蓝牙有问题,买了新的ble蓝牙模块,通过串口连接到电脑测试,断开之后再连接收到多个数据包,下面是测试的代码和测试的图片,希望得到帮助!

代码:

Page({

data: {

deviceId: ‘’,

serviceId: ‘’,

charId: ‘’,

devicecon: [],

},

onLoad: function() {

var that = this;

wx.openBluetoothAdapter({

success: function(res) {

wx.getBluetoothAdapterState({

success: function(res) {

wx.startBluetoothDevicesDiscovery({

success: function(res) {

wx.getBluetoothDevices({

success: function(res) {

for (var i = 0; i < res.devices.length; i++) {

that.data.devicecon.push(res.devices[i][‘deviceId’]);

}

wx.onBluetoothDeviceFound(function(devices) {

for (var i = 0; i < devices.devices.length; i++) {

that.data.devicecon.push(devices.devices[i][‘deviceId’]);

}

that.setData({

devicecon: that.data.devicecon,

});

console.log(that.data.devicecon);

})

}

})

}

})

}

})

}

}) wx.onBLEConnectionStateChange(function(res) {

if (!res.connected) {

console.log(‘已经断开了’);

}

}) //特征值改变    

wx.onBLECharacteristicValueChange(function (res) {

var buf = (res.value.byteLength);      

console.log(“我真的只发送一次”);      

console.log(buf);      

console.log(“我真的只发送一次”);    

})  

},  

linkbluetooth:function(e){    

var that = this;    

that.data.deviceId = e.currentTarget.dataset.value;    

wx.createBLEConnection({      

deviceId: that.data.deviceId,      

success: function (res) {        

console.log(‘经连接成功了!’)        

wx.getBLEDeviceServices({          

deviceId: that.data.deviceId,          

success: function (res) {            

for(var i=0;i<res.services.length;i++){                

if(res.services[i][‘uuid’].indexOf(‘ffe0’) != -1){                  

that.data.serviceId = res.services[i][‘uuid’];                

}            

}            //获取特征值            

if(that.data.serviceId){              

setTimeout(function () {                

wx.getBLEDeviceCharacteristics({                  

deviceId: that.data.deviceId,                  

serviceId: that.data.serviceId,                  

success: function (res) {                    

for(var i=0;i<res.characteristics.length;i++){                      

if (res.characteristics[i][‘uuid’].indexOf(‘ffe1’)!=-1){                        

that.data.charId = res.characteristics[i][‘uuid’];                      

}                    

}                    

wx.notifyBLECharacteristicValueChange({                      

state: true, 

// 启用 notify 功能                     

// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取                      

deviceId: that.data.deviceId,                      

// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取                      

serviceId: that.data.serviceId,                      

// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取                      

characteristicId: that.data.charId,                      

success: function (res) {                      

console.log(‘notifyBLECharacteristicValueChange success’, res.errMsg)                      

}                    

});                    

console.log(that.data.charId);                    

that.writecomm();                  

}                

})              

}, 2000);            

}          

}        

})      }    })  },

writecomm:function(){  

var that = this;  

let buffer = new ArrayBuffer(1)  

let dataView = new DataView(buffer)  

dataView.setUint8(0,99);  

wx.writeBLECharacteristicValue({    

// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取    

deviceId: that.data.deviceId,    // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取    

serviceId: that.data.serviceId,    // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取    

characteristicId: that.data.charId,    // 这里的value是ArrayBuffer类型    

value: buffer,    

success: function (res) {      

console.log(‘writeBLECharacteristicValue success’, res.errMsg)    

}  

}

)}

串口已经连接到了蓝牙模块,最后一行R:0是电脑接收到的字节,S:0是发送的给蓝牙的字节,刚开始都是0

连接到了蓝牙

此时,串口收到了1个字节的数据,为代码中10进制的 99,就是16进制的63

通过串口发送一个字节的数字给蓝牙模块

此时 接收 和发送的数据都是1

完全是没有问题的,

但是在蓝牙模块意外中断之后,问题就来了(图片太大了,不能超过5M,只能缩小一下图片了)

拔掉蓝牙电源线,通信中断了

蓝牙断开之后重新接电,连接蓝牙

再次连接成功

收到了有收到了第二个63 ,再发送一个31给蓝牙,

监听特征值缺收到了2个数据包

加第一次的数据包一共是3个。

而且你断开n次,重新连接之后你就可以收到n+1个数据包。用

wx.closeBluetoothAdapter(OBJECT)

断开资源,重新连接,依然是收到两个数据包,除非退出小程序重新进入,或者关闭手机蓝牙再次打开。

10 回复

@Kevin马凯 oooooccs

应该会在安卓最新版本解决  @Kin

请问修复了吗?我close后,再连就找不到蓝牙,得再close一次才能找到。

楼主微信多少,我加下你。

楼主的问题我也碰到过。不知有木有解决?重新连接蓝牙设备后调用closeBLEConnection并没有完全释放蓝牙,重新连接后能收到比断开次数多一次的数据。

了解,问题已经定位了,是底层实现的bug,明天或后天发新版本修复这个问题,十分抱歉   @Kin

我的le2,FastBLE扫描到设备后不连接,小程序扫描到设备后连,再用FastBLE连接设备,竟然可以连接上还可一发数据,不进行断开操作,回到小程序竟然也可以发数据!难道我连的设备是1:M模式!

安卓的客户端只要在重连设备之前关闭上次的notify即可解决重复收到的重复信息问题。@ifwang 

稍等,我拉相关同学看下

回到顶部