美文网首页
关于微信小程序蓝牙分包发送及多包发送不返回问题

关于微信小程序蓝牙分包发送及多包发送不返回问题

作者: gazyy1 | 来源:发表于2018-10-12 16:17 被阅读0次

关于分包发送 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)

}

}


自己写的比较简易的分包发送,基本可以实现。刚接触小程序,代码还未做优化。

关于多包发送后设备不返回信息。多数是因为ArrayBuffer的长度不正确,暂时是项目中遇到的不返回是因为这个原因。其他 原因暂时未遇到。

相关文章

网友评论

      本文标题:关于微信小程序蓝牙分包发送及多包发送不返回问题

      本文链接:https://www.haomeiwen.com/subject/lnsgaftx.html