HarmonyOS鸿蒙Next中手机系统设置中已配对蓝牙连接后,APP连接该蓝牙,打开notify时报2900099或2900007
HarmonyOS鸿蒙Next中手机系统设置中已配对蓝牙连接后,APP连接该蓝牙,打开notify时报2900099或2900007
import ble from '@ohos.bluetooth.ble';
const BUSINESS_SERVICE_UUID = "xxxx";
const NOTIFY_CHARACTERISTIC_UUID = "xxxx";
// 创建客户端
const device = ble.createGattClientDevice(mac);
// 注册回调
device.on("BLEConnectionStateChange", (state) => {
console.info("Ble connection state did change " + state.state);
if (state.state === bleConstant.ProfileConnectionState.STATE_CONNECTED) {
// 设置MTU
device.setBLEMtuSize(247);
}
});
device.on("BLEMtuChange", async (mtu) => {
console.info(`Ble mtu did change, mtu = ${mtu}`);
// 发现特征
const services = await device.getServices();
let notifyCharacteristic: ble.BLECharacteristic | undefined;
for (const service of services) {
if (BUSINESS_SERVICE_UUID === service.serviceUuid) {
const characteristics = service.characteristics;
for (const characteristic of characteristics) {
if (NOTIFY_CHARACTERISTIC_UUID === characteristic.characteristicUuid) {
notifyCharacteristic = characteristic;
}
}
}
}
if (!notifyCharacteristic) {
console.warning(`Ble open notify failed: ${NOTIFY_CHARACTERISTIC_UUID} is not found`);
return;
}
try {
await device.setCharacteristicChangeNotification(notifyCharacteristic, true);
console.info(`Ble open notify success`);
} catch (error) {
console.error(`Ble open notify failed: ${JSON.stringify(error)}`);
}
});
// 开始连接
device.connect();
代码如上, 按照流程APP连接蓝牙->配对->手机系统设置蓝牙处于已连接状态->APP断开蓝牙->APP再次连接蓝牙操作后报错,报错日志为:
Ble open notify failed: {"code":2900007,"message":"Inner error."}
更多关于HarmonyOS鸿蒙Next中手机系统设置中已配对蓝牙连接后,APP连接该蓝牙,打开notify时报2900099或2900007的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
蹲
更多关于HarmonyOS鸿蒙Next中手机系统设置中已配对蓝牙连接后,APP连接该蓝牙,打开notify时报2900099或2900007的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,APP连接已配对的蓝牙设备时,若打开notify出现2900099或2900007错误,通常与蓝牙服务的权限或配置有关。2900099可能表示GATT操作失败,2900007可能表示GATT服务未找到或不可用。确保APP已正确声明蓝牙权限,且设备支持所需的GATT服务。检查设备的蓝牙配置文件是否与APP需求匹配。
根据代码和问题描述,2900007错误通常发生在蓝牙Notify操作时,可能原因如下:
- 蓝牙连接状态问题:
- 虽然系统设置显示已连接,但APP层面的GATT连接可能未完全建立
- 建议在调用setCharacteristicChangeNotification前检查device.isConnected状态
- 时序问题:
- MTU设置和Notify操作之间可能需要延迟
- 建议在MTU变更回调后增加100-200ms延迟再进行Notify操作
- 重复连接问题:
- 系统配对后直接连接可能导致GATT服务发现不完全
- 建议在每次连接前先调用device.close()确保干净状态
- 特征值权限:
- 确认NOTIFY_CHARACTERISTIC_UUID确实具有Notify属性
- 可通过getServices结果检查characteristic.properties是否包含NOTIFY
典型修复方案:
- 增加连接状态检查
- 添加重试机制
- 确保每次连接前正确断开
- 检查蓝牙设备端的Notify配置
建议优先检查蓝牙设备端的特征值配置是否符合规范。