关于分包发送 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)
}
}
自己写的比较简易的分包发送,基本可以实现。刚接触小程序,代码还未做优化。
网友评论