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
浏览
收藏 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;
}
分享
微博
QQ
微信
回复
2025-01-10 12:25:50
相关问题
HarmonyOS 调用相册函数时报错
415浏览 • 1回复 待解决
HarmonyOS getaddrinfo接口调用报错
291浏览 • 1回复 待解决
HarmonyOS 调用屏幕截图接口报错801
611浏览 • 1回复 待解决
HarmonyOS 内购接口调用报错
251浏览 • 1回复 待解决
HarmonyOS 运行HmosWorld时报错
613浏览 • 1回复 待解决
HarmonyOS 应用发布时报错
322浏览 • 1回复 待解决