微信小程序 蓝牙 长数据包 分包拆包

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
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值