如题,同一串二进制串,成功下发的几率约为80%,偶尔失败,有遇到同样问题的吗?为什么?```javascript
var that = this;
var bleKey = iotKey;
bleKey = Array.prototype.map.call(new Uint8Array(uni.base64ToArrayBuffer(bleKey)), x => ('00' + x.toString(16)).slice(
-2)).join("");
var typedArray = new Uint8Array(bleKey.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16)
}))
for (var i = 0; i < typedArray.length; i += 20) {
var endLength = 0
if (i + 20 < typedArray.length) {
var senddata = typedArray
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))
}
uni.writeBLECharacteristicValue({
deviceId: that.deviceId,
serviceId: that.serviceId,
characteristicId: that.uartRXCharacteristic.uuid,
value: buffer,
success: function(res) {
console.log('多包', res.errMsg)
},
fail: function(res) {
console.log(buffer)
console.log('发送失败', res.errMsg)
}
})
} else {
var senddata = typedArray
if (20 < typedArray.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))
}
uni.writeBLECharacteristicValue({
deviceId: that.deviceId,
serviceId: that.serviceId,
characteristicId: that.uartRXCharacteristic.uuid,
value: buffer,
success: function(res) {},
fail: function(res) {
console.log('发送失败', res.errMsg)
}
})
}
}
2 个回复
千羽羊驼 (作者)
这社区死气沉沉的
y***@163.com
坑多没人填,uniapp 是个好东西,真希望官方把多完善完善。