业务场景中:不需要订阅读完特征值之后直接调用wx.writeBLECharacteristicValuea这个接口
d代码如下:
onLoad: function (options) {
this.setData({
curDeviceId: options.deviceId
})
this.handleGetServices()
wx.onBLECharacteristicValueChange(res => {
console.log(`CONNECTION Page on NetworkStatusBLECharacteristicValueChange res:`, res)
})
},
editName(){
console.log("Ssssssssssss")
this.setData({
showModal:true
})
},
hideModal(){
this.setData({
showModal:false
})
},
updateName(){
},
handleGetServices() {
let that = this
console.log("CONNECTION Page handleGetServices", this.data.curDeviceId)
wx.getBLEDeviceServices({
deviceId: this.data.curDeviceId,
success: (res) => {
console.log(`CONNECTION Page getBLEDeviceServices res:`, res)
that.handleGetCharacteristics()
},
fail: (error) => {
console.error(`CONNECTION Page getBLEDeviceServices error:`, error)
}
})
},
handleGetCharacteristics() {
console.log(`CONNECTION Page handleGetCharacteristics`, this.data.curDeviceId)
wx.getBLEDeviceCharacteristics({
deviceId: this.data.curDeviceId,
serviceId: '0000180A-1111-2222-0000-123456780000',
success: (res) => {
console.log(`CONNECTION Page getBLEDeviceCharacteristics res:`, res)
this.handleReadCharacteristics()
},
fail: (error) => {
console.error(`CONNECTION Page getBLEDeviceCharacteristics error:`, error)
}
})
},
handleReadCharacteristics() {
console.log(`CONNECTION Page handleReadCharacteristics`)
wx.readBLECharacteristicValue({
deviceId: this.data.curDeviceId,
serviceId: '0000180A-1111-2222-0000-123456780000',
characteristicId: 'DDDDEEEE-1111-2222-0000-123456780005',
success: (res) => {
console.log(`CONNECTION Page readBLECharacteristicValue res:`, res)
},
fail: (error) => {
console.error(`CONNECTION Page readBLECharacteristicValue error:`, error)
}
})
},
handleNotifyCharacteristics() {
console.log(`CONNECTION Page handleNotifyNetworkStatusCharacteristics`)
wx.notifyBLECharacteristicValueChange({
deviceId: this.data.curDeviceId,
serviceId: '0000180A-1111-2222-0000-123456780000',
characteristicId: 'DDDDEEEE-1111-2222-0000-123456780005',
state: true,
success: (res) => {
console.log(`CONNECTION Page notifyNetworkStatusBLECharacteristicValueChange res:`, res)
},
fail: (error) => {
console.error(`CONNECTION Page notifyNetworkStatusBLECharacteristicValueChange error:`, error)
}
})
},
enterKey(e: any) {
let keyVal = e.currentTarget.dataset.key
console.log("用户按键", keyVal)
let value = this.string2buffer(keyVal)
console.log("转成arrayBuffer字符串",value)
this.handleWriteBLECharacteristic(value)
},
handleWriteBLECharacteristic(bufferVal: ArrayBuffer) {
console.log("开始写特征值===========",bufferVal,wx.getStorageSync("curDeviceId"))
wx.writeBLECharacteristicValue({
deviceId: wx.getStorageSync("curDeviceId"),
serviceId: '0000180A-1111-2222-0000-123456780000',
characteristicId: 'DDDDEEEE-1111-2222-0000-123456780005',
value: bufferVal,
success: (res) => {
console.log('writeBLECharacteristicValue success', wx.getStorageSync("curDeviceId"), res)
if (res.errCode == 0 || res.errCode == -1) {
}
},
fail: (error: any) => {
console.log('writeBLECharacteristicValue error', error)
},
complete:(value:any)=>{
console.log('writeBLECharacteristicValue complete', value)
}
})
},
你好,10008是有再细分的,具体可以看errMsg;这类错误都是系统直接回调错误,不是小程序逻辑上增加的;具体到writeBLECharacteristicValue,就是write这个动作系统接口返回fail了;偶现的情况,建议延后一会重试。