HarmonyOS 鸿蒙Next 蓝牙BLE调用writeCharacteristicValue时报错2900099: Operation failed
在确认建立连接并且获取特征值信息之后调用writeCharacteristicValue写入数据会遇到BussinessError 2900099: Operation failed的报错,在调用前有确认连接状态为已连接。 进行BLE蓝牙开发,手机作为Client端连接激光测距蓝牙模块进行读写通信,当手机作为Client端时连接server端,可以成功建立连接,并且获取特征值信息,log可以打印。
步骤: 问题代码如下:
// 写入数据
writeBLECharacteristicValue(msg:BluetoothMsgType, isHex:boolean)
{
if (this.mGattClientDevice !== undefined) {
Logger.info(TAG, "the private mGattClientDevice is= " + this.mGattClientDevice)
}
let data: string = bleMsgDetail.getValue(msg);
Logger.info(TAG, "writeBLECharacteristicValue-data = " + data + " isHex = " + isHex)
let byteArray: Uint8Array = isHex ? this.hexStrToBytes(data) : new util.TextEncoder().encodeInto(data);
Logger.info(TAG, "ecCharacteristicWrite is: " + this.ecCharacteristicWrite?.serviceUuid)
if (this.ecCharacteristicWrite == undefined) {
Logger.info(TAG, "ecCharacteristicWrite is undefined")
// return;
let mCharacteristicWrite: ble.BLECharacteristic = {
characteristicUuid: this.ecCharacteristicWrite.characteristicUuid,
serviceUuid: this.ecCharacteristicWrite.serviceUuid,
characteristicValue: byteArray,
descriptors: this.ecCharacteristicWrite.descriptors,
}
// 设置回复形式 + 开始写数据
Logger.info(TAG, "in writeBLECharacteristicValue connection state is: " + this.bleConnectionStateInfo)
if (this.bleConnectionStateInfo === 'STATE_CONNECTED') {
try {
Logger.info(TAG, 'bluetooth writeCharacteristicValue start');
if (this.mGattClientDevice !== undefined) {
this.mGattClientDevice.writeCharacteristicValue(this.ecCharacteristicWrite, ble.GattWriteType.WRITE,
this.writeCharacteristicValueCallBack);
// Logger.info(TAG, 'bluetooth writeCharacteristicValue success');
}
} catch (err) {
Logger.error(TAG, 'writeCharacteristicValue errCode: ' + (err as BusinessError).code + ', errMessage: ' +
(err as BusinessError).message);
}
}
}
writeCharacteristicValueCallBack(code: BusinessError ) {
if (code != null) {
Logger.error(TAG, "error with writeCharacteristicValue" + (code) + ', errMessage: ' + (code.message));
return;
}
Logger.info(TAG, 'bluetooth writeCharacteristicValue success');
}
}
更多关于HarmonyOS 鸿蒙Next 蓝牙BLE调用writeCharacteristicValue时报错2900099: Operation failed的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
可尝试重新连接蓝牙调用writeCharacteristicValue,若还是报错,提供相关报错的日志文本,点击deveco左下角日志窗口的左边工具栏的save将日志信息保存到txt中
从日志分析,问题为入参错误
08-08 13:44:56.958 27883-27883 A0FF00/[BleManager] com.hos.comm I getServices success, serviceUuid is 0000FFF0-0000-1000-8000-00805F9B34FB
08-08 13:44:56.958 27883-27883 A00000/testTag com.hos.comm I Succeeded in loading the content.
08-08 13:44:56.958 27883-27904 C00101/Bluetooth com.hos.comm I [bluetooth_gatt_client.cpp(WriteDescriptor:923)]enter
08-08 13:44:56.966 27883-27904 C00101/Bluetooth com.hos.comm E [bluetooth_gatt_client.cpp(WriteDescriptor:942)]Invalid parameters
08-08 13:44:56.967 27883-27883 C00101/Bluetooth com.hos.comm I [napi_timer.cpp(Register:57)]timerId: 1
08-08 13:44:56.967 27883-27883 C00101/Bluetooth com.hos.comm E [napi_bluetooth_utils.cpp(GetCallbackErrorValue:37)]errCode: 2900099
可参考以下demo:
```
writeCharacteristicValue() {
if (!this.gattServiceInfo) {
this.characteristicValue = ‘’;
console.log('BluetoothPage bluetooth gattServiceInfo is undefined ');
return
}
let services: ble.GattService = this.gattServiceInfo;
let descriptors: Array<ble.BLEDescriptor> = [];
let descriptor: ble.BLEDescriptor = {
serviceUuid: services.serviceUuid,
characteristicUuid: services.characteristics[0].characteristicUuid,
descriptorUuid: services.characteristics[0].descriptors[0].descriptorUuid,
descriptorValue: services.characteristics[0].descriptors[0].descriptorValue
};
descriptors[0] = descriptor;
let characteristic: ble.BLECharacteristic = {
serviceUuid: services.serviceUuid,
characteristicUuid: services.characteristics[0].characteristicUuid,
characteristicValue: Utils.string2ArrayBuffer(this.cValue),
descriptors: descriptors
};
try {
if (this.gattClient) {
this.gattClient.writeCharacteristicValue(characteristic, ble.GattWriteType.WRITE);
promptAction.showToast({
message: ‘特征值写结束’
})
console.log(‘BluetoothPage writeCharacteristicValue finish’);
}
} catch (err) {
console.error('errCode: ’ + (err as BusinessError).code + ', errMessage: ’ + (err as BusinessError).message);
}
}
}
```
descriptorUuid不能为空,可以在写入前做判空检验
更多关于HarmonyOS 鸿蒙Next 蓝牙BLE调用writeCharacteristicValue时报错2900099: Operation failed的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html