HarmonyOS 调用writeCharacteristicValue接口时报错 errCode: -10, errMessage: Inner error.

调用 writeCharacteristicValue 接口时报错 errCode: -10, errMessage: Inner error. 接口参数 serviceUUID及CharacteristicUuid填写无误,并且该Characteristic有写入权限

HarmonyOS  调用writeCharacteristicValue接口时报错 errCode: -10, errMessage: Inner error.  -鸿蒙开发者社区

HarmonyOS
2025-01-10 10:16:01
1.0w浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

可能为创建参数中设置descriptors或characteristicValue参数值存在问题,写入特征值时可参考如下封装方式:

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 value : string = ''
  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);
  }
}

string2ArrayBuffer(value: string) {
  let buf = buffer.from(value,'utf16le').buffer;
  return buf;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
分享
微博
QQ
微信
回复
2025-01-10 12:25:50


相关问题
HarmonyOS 调用相册函数时报错
526浏览 • 1回复 待解决
HarmonyOS getaddrinfo接口调用报错
439浏览 • 1回复 待解决
HarmonyOS 调用屏幕截图接口报错801
799浏览 • 1回复 待解决
HarmonyOS 内购接口调用报错
455浏览 • 1回复 待解决
HarmonyOS 应用发布时报错
433浏览 • 1回复 待解决
HarmonyOS 运行HmosWorld时报错
788浏览 • 1回复 待解决