// 开启/关闭特征值变化通知
notifyBLECharacteristicValueChange(characteristic: BluetoothCharacteristic, enable: boolean): void {
if ( !this.connected) return
uni.notifyBLECharacteristicValueChange({
deviceId: this.device.deviceId,
serviceId: this.getServiceIdByCharacteristic(characteristic),
characteristicId: characteristic.uuid,
state: enable,
success: () => {
if (enable) {
if (!this.isNotifying(characteristic.uuid)) {
this.notifyingCharacteristics.push(characteristic.uuid)
}
this.addDebugLog('info', `开启通知成功: ${characteristic.uuid}`)
uni.showToast({
title: '已开启通知',
icon: 'success'
})
} else {
this.notifyingCharacteristics = this.notifyingCharacteristics.filter(
uuid => uuid !== characteristic.uuid
)
this.addDebugLog('info', `关闭通知成功: ${characteristic.uuid}`)
uni.showToast({
title: '已关闭通知',
icon: 'success'
})
}
},
fail: (res) => {
this.addDebugLog('error', `失败: ${characteristic.uuid}`)
this.addDebugLog('error', `${enable ? '开启' : '关闭'}1通知失败: ${res.errMsg}`)
uni.showToast({
title: (enable ? '开启' : '关闭') + '1通知失败: ' + res.errMsg,
icon: 'error'
})
}
})
},
1 个回复
a***@163.com (作者) - 完全的不靠谱
我用ios试了 notifyBLECharacteristicValueChange 是可以正常订阅的。
android上不成功的原因是
{
"errMsg": "notifyBLECharacteristicValueChange:fail no characteristic",
"code": 10005
}