同样的代码,在安卓手机可以正常调用writeBLECharacteristicValue向蓝牙设备写入,但是在iphone手机(版本11.2.2)上调用,返回错误编码10008,错误信息“the value's length is invalid”,请大佬告知如何解决。代码如下:
wx.notifyBLECharacteristicValueChange({
state: true,
deviceId: deviceId,
serviceId: notifyserviceId,
characteristicId: notifycharacteristicId,
success: function (res) {
console.log("notify success")
setTimeout(function () {
that.writeBLECharacteristicValue(deviceId, writeserviceId, writecharacteristicId)
}, 2000);
},
fail: function (res) {
console.log("notify fail:" + res.errMsg)
}, complete : function() {
wx.onBLECharacteristicValueChange(function (res) {
console.log(res)
});
}
})
},
writeBLECharacteristicValue: function (deviceId, writeserviceId, writecharacteristicId) {
var that = this
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0, 0)
wx.writeBLECharacteristicValue({
deviceId: deviceId,
serviceId: writeserviceId,
characteristicId: writecharacteristicId,
value: buffer,
success: function (res) {
console.log('write success')
},
fail(res) {
console.log('write fail')
console.log(res)
}
})
},
同样出现这个问题,因为数据是20字节,还说字节length错误
写入的字节长度改大一些(不超过20个字节)试试吧
谢谢回复,我设置了16位,还是报错
iOS下请确保在调用wx.notifyBLECharacteristicValueChange之前已经成功调用下面2个方法:
wx.getBLEDeviceServices
wx.getBLEDeviceCharacteristics
已经成功调用这两个方法了,两个方法都返回success