微信小程式 藍芽 長資料包 分包拆包
阿新 • • 發佈:2018-10-31
https://www.jianshu.com/p/de7bd0093c43
關於 微信小程式藍芽 分包傳送 及 多包傳送 不返回問題
關於分包傳送 20位元組分包,微信小程式支援多於20位元組傳送。但是低功耗藍芽傳輸可能會有問題,建議分包傳送
for (var i = 0;i<e.length;i+=20) { var endLength = 0 if (i+20<e.length) { var senddata = e let buffer = new ArrayBuffer(20) let dataView = new DataView(buffer) let dataSend = [] for (var j = i; j < i + 20; j++) { dataView.setUint8(j - i, senddata[j]) dataSend.push(dataView.getUint8(j-i)) } console.log('多包傳送的包資料:'+dataSend) wx.writeBLECharacteristicValue({ deviceId: app.globalData.deviceId+"", serviceId: app.globalData.writeServicweId+'', characteristicId: app.globalData.writeCharacteristicsId+'', value: buffer, success: function (res) { console.log('多包writeBLECharacteristicValue success',res.errMsg) }, fail: function (res) { console.log('傳送失敗') } }) sleep(0.02) }else{ //console.log(app.globalData.writeServicweId+'-----------') var senddata = e if (20 < e.length) { endLength = senddata.length-i }else{ endLength = senddata.length } let buffer = new ArrayBuffer(endLength) let dataView = new DataView(buffer) let dataSend = [] for (var j = i; j < senddata.length; j++) { dataView.setUint8(j-i, senddata[j]) dataSend.push(dataView.getUint8(j-i)) } console.log('最後一包或第一資料:' + dataSend) wx.writeBLECharacteristicValue({ deviceId: app.globalData.deviceId+"", serviceId: app.globalData.writeServicweId+'', characteristicId: app.globalData.writeCharacteristicsId+'', value: buffer, success: function (res) { console.log('一包writeBLECharacteristicValue success',res.errMsg) }, fail: function (res) { console.log('傳送失敗') } }) sleep(0.02) } } 作者:gazyy1 連結:https://www.jianshu.com/p/de7bd0093c43 來源:簡書 簡書著作權歸作者所有,任何形式的轉載都請聯絡作者獲得授權並註明出處。