HarmonyOS 鸿蒙Next 蓝牙BLE调用writeCharacteristicValue时报错2900099: Operation failed

发布于 1周前 作者 gougou168 最后一次编辑是 5天前 来自 鸿蒙OS

在确认建立连接并且获取特征值信息之后调用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

2 回复
蓝牙2900099错误码信息:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/errorcode-bluetoothmanager-V5#section2900099

可尝试重新连接蓝牙调用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


在HarmonyOS鸿蒙Next系统中,遇到蓝牙BLE调用writeCharacteristicValue时报错2900099: Operation failed,通常是由于以下几个原因导致的:

  1. 特征值权限:确保目标特征值(Characteristic)支持写操作,并且你的应用已获取相应的权限。某些特征值可能被设置为只读,或者需要特定的认证过程才能写入。

  2. 连接状态:检查你的蓝牙设备是否已成功连接,并且在尝试写入特征值时连接未断开。

  3. 服务状态:确认目标蓝牙服务已启动,并且处于可用状态。服务可能由于设备配置变更或电量管理等原因被禁用。

  4. 数据格式:检查写入的数据格式是否符合特征值的要求,包括长度和数据类型。

  5. 系统限制:部分系统更新或配置可能限制了蓝牙操作,查阅最新的HarmonyOS开发文档了解是否有相关变更。

  6. 设备兼容性:确保你的设备(包括开发设备和目标蓝牙设备)都支持当前的蓝牙标准和协议。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部