HarmonyOS 鸿蒙Next 蓝牙功能写入操作writeCharacteristicValue遇Error: BussinessError 401 Invalid parameter
HarmonyOS 鸿蒙Next 蓝牙功能写入操作writeCharacteristicValue遇Error: BussinessError 401 Invalid parameter
蓝牙功能,写入操作writeCharacteristicValue。问题:Error: BussinessError 401: Invalid parameter
怎么弄?求解
更多关于HarmonyOS 鸿蒙Next 蓝牙功能写入操作writeCharacteristicValue遇Error: BussinessError 401 Invalid parameter的实战教程也可以访问 https://www.itying.com/category-93-b0.html
试一下这个呢?
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;
}
更多关于HarmonyOS 鸿蒙Next 蓝牙功能写入操作writeCharacteristicValue遇Error: BussinessError 401 Invalid parameter的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
现在手头上没有相关设备,只能是下周一才能试一下了,非常感谢
总的来说,HarmonyOS是一款非常优秀的操作系统,期待它能在未来带给我们更多惊喜!
不需要再配置uuid吗,能不能看看你的代码
//服务发现回调
private servicesDiscovered = (code: BusinessError, gattServices: Array<ble.GattService>) => {
hilog.debug(Domain, LogTag, 服务发现回调,code= ${JSON.stringify(code)}}
)
if (code && code.code != 0) {
hilog.debug(Domain, LogTag, 服务发现回调成功
)
return;
}
let servicesArray: Array<ble.GattService> = gattServices;
if (servicesArray == undefined || servicesArray.length == 0) {
hilog.debug(Domain, LogTag, 未发现服务
)
return
}
for (let i = 0; i < servicesArray.length; i++) {
hilog.debug(Domain, LogTag, 服务发现回调 serviceUuid= ${servicesArray[i].serviceUuid}
)
}
let service =
BleMeetInPersonHelper.getInstance().getBluetoothGattService(gattServices, BleConstants.UUID_SERVICE)
}
这里拿到gattServices通过gattServices拿到characteristics去写值就行
没有其他await 代码了哦,能不能帮我解决一下
在HarmonyOS鸿蒙Next系统中,遇到蓝牙功能写入操作writeCharacteristicValue
时返回Error: BussinessError 401 Invalid parameter
,通常表明传递给写入函数的参数不符合预期格式或范围。
-
检查参数格式:确保传递给
writeCharacteristicValue
的数据格式(如长度、数据类型)与蓝牙特征值(Characteristic)的定义相匹配。例如,如果特征值定义为只接受16位无符号整数,而传入的是32位整数或字符串,则会导致此错误。 -
验证权限:确认应用已获取足够的权限来写入该特征值。某些特征值可能受蓝牙设备的安全策略保护,需要特定的认证或授权。
-
服务与支持特征:确认目标蓝牙设备上的服务(Service)和特征值(Characteristic)是否支持写入操作。有些特征值仅支持读取。
-
设备兼容性:检查设备兼容性,确保鸿蒙Next系统的蓝牙栈与蓝牙设备的固件版本兼容。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html 。