HarmonyOS鸿蒙Next中spp蓝牙连接时不时连接不上并报错2900099

HarmonyOS鸿蒙Next中spp蓝牙连接时不时连接不上并报错2900099

const connect: connectFunction = async (option)=>{
    console.log('[SPP connect parameter:]', JSON.stringify(option));
    try {
        await initBluetoothPermission();
    } catch (err) {
        console.error('errCode: ' + (err as BusinessError1).code + ', errMessage: ' + (err as BusinessError1).message);
        const res = new ReturnOption1((err as BusinessError1).message, (err as BusinessError1).code);
        option.fail?.(res);
        option.complete?.(res);
        return;
    }
    try {
        // 检查配对
        await pair(option.address);
        let sppOption: socket.SppOptions = {
            uuid: option.uuid || '00001101-0000-1000-8000-00805f9b34fb',
            secure: true,
            type: socket.SppType.SPP_RFCOMM
        };
        let clientSocket = (err: BusinessError1, clientSocketId: number)=>{
            if (err) {
                console.error('errCode: ' + (err as BusinessError1).code + ', errMessage: ' + (err as BusinessError1).message);
                const res = new ReturnOption1((err as BusinessError1).message, (err as BusinessError1).code);
                option.fail?.(res);
                option.complete?.(res);
                return;
            } else {
                clientSocketMap.set(option.address, clientSocketId);
                const res = new ReturnOption1('ok', 0);
                option.success?.(res);
                option.complete?.(res);
                callBack.emitDeviceConnectionStateChangeCallback?.({
                    address: option.address,
                    state: true
                } as DeviceConnectionStateChangeCallbackEvent);
                connection.off('bondStateChange');
                connection.on('bondStateChange', (data: connection.BondStateParam)=>{
                    if (data.state === connection.BondState.BOND_STATE_INVALID) {
                        console.log('设备' + option.address + '断开连接,原因:' + data.cause);
                        callBack.emitDeviceConnectionStateChangeCallback?.({
                            address: option.address,
                            state: false
                        } as DeviceConnectionStateChangeCallbackEvent);
                    }
                });
                const dataListener = (data: ArrayBuffer)=>{
                    const uint8Data = new Uint8Array(data);
                    const hexArray: number[] = Array.from(uint8Data);
                    const text = new util.TextDecoder().decodeWithStream(uint8Data);
                    console.log('接收数据:', text);
                    callBack.emitDataReceivedCallback?.({
                        address: option.address,
                        data: hexArray
                    } as DataReceivedCallbackEvent);
                };
                try {
                    socket.on('sppRead', clientSocketId, dataListener);
                } catch (err) {
                    console.error('errCode: ' + (err as BusinessError1).code + ', errMessage: ' + (err as BusinessError1).message);
                    console.error('sppRead', clientSocketId);
                }
            }
        };
        socket.sppConnect(option.address, sppOption, clientSocket);
    } catch (err) {
        console.error('errCode: ' + (err as BusinessError1).code + ', errMessage: ' + (err as BusinessError1).message);
        const res = new ReturnOption1((err as BusinessError1).message, (err as BusinessError1).code);
        option.fail?.(res);
        option.complete?.(res);
    }
};

失败

05-07 10:24:04.200   56761-56761   A03D00/com.han....uniapp/JSAPP  com.hanfe...c.uniapp  I     [SPP connect parameter:] {"address":"72:0A:46:AD:F7:A5"}
05-07 10:24:04.201   56761-56761   C00101/com.han...remote_device  com.hanfe...c.uniapp  I     (GetPairState:181)enter
05-07 10:24:04.207   56761-56761   C00101/com.han...pi_connection  com.hanfe...c.uniapp  I     (GetPairState:632)getPairState :2
05-07 10:24:04.207   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (SppConnect:109)enter
05-07 10:24:04.207   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (CheckSppConnectParams:39)enter
05-07 10:24:04.207   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (GetSppOptionFromJS:90)uuid is 00001101-0000-1000-8000-00805f9b34fb
05-07 10:24:04.207   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (GetSppOptionFromJS:95)secure is 1
05-07 10:24:04.207   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (GetSppOptionFromJS:103)uuid: 00001101-0000-1000-8000-00805f9b34fb, secure: 1, type: 0
05-07 10:24:04.207   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (CheckSppConnectParams:58)callback mode
05-07 10:24:04.207   56761-7542    C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():121)SppConnect execute
05-07 10:24:04.207   56761-7542    C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():127)SppConnect client_ constructed
05-07 10:24:04.210   56761-7542    C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  E     (Connect:461)Connect error 2900099
05-07 10:24:04.210   56761-7542    C00101/com.han...et_spp_client  com.hanfe...c.uniapp  E     (operator():133)SppConnect failed
05-07 10:24:04.210   56761-2999    C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  E     (OnConnectionStateChanged:345)clientSptr->pimpl->observer_: callback is nullptr
05-07 10:24:04.210   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():137)SppConnect execute back
05-07 10:24:04.210   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():156)SppConnect execute back failed
05-07 10:24:04.210   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():161)SppConnect execute Callback mode
05-07 10:24:04.210   56761-56761   C00101/com.han...bt_napi_utils  com.hanfe...c.uniapp  E     (GetCallbackErrorValue:40)errCode: 2900099
05-07 10:24:04.210   56761-56761   A03D00/com.han....uniapp/JSAPP  com.hanfe...c.uniapp  E     errCode: 2900099, errMessage: BussinessError 2900099: Operation failed

成功

05-07 10:27:02.343   56761-56761   A03D00/com.han....uniapp/JSAPP  com.hanfe...c.uniapp  I     [SPP connect parameter:] {"address":"72:0A:46:AD:F7:A5"}
05-07 10:27:02.345   56761-56761   C00101/com.han...remote_device  com.hanfe...c.uniapp  I     (GetPairState:181)enter
05-07 10:27:02.351   56761-56761   C00101/com.han...pi_connection  com.hanfe...c.uniapp  I     (GetPairState:632)getPairState :2
05-07 10:27:02.351   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (SppConnect:109)enter
05-07 10:27:02.352   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (CheckSppConnectParams:39)enter
05-07 10:27:02.352   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (GetSppOptionFromJS:90)uuid is 00001101-0000-1000-8000-00805f9b34fb
05-07 10:27:02.352   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (GetSppOptionFromJS:95)secure is 1
05-07 10:27:02.352   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (GetSppOptionFromJS:103)uuid: 00001101-0000-1000-8000-00805f9b34fb, secure: 1, type: 0
05-07 10:27:02.352   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (CheckSppConnectParams:58)callback mode
05-07 10:27:02.352   56761-9096    C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():121)SppConnect execute
05-07 10:27:02.352   56761-9096    C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():127)SppConnect client_ constructed
05-07 10:27:02.365   56761-9096    C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  I     (Connect:463)fd_: 190
05-07 10:27:02.368   56761-56843   C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  E     (OnConnectionStateChanged:345)clientSptr->pimpl->observer_: callback is nullptr
05-07 10:27:02.368   56761-56843   C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  E     (OnConnectionStateChanged:345)clientSptr->pimpl->observer_: callback is nullptr
05-07 10:27:02.373   56761-56843   C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  E     (OnConnectionStateChanged:345)clientSptr->pimpl->observer_: callback is nullptr
05-07 10:27:02.851   56761-56843   C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  E     (OnConnectionStateChanged:345)clientSptr->pimpl->observer_: callback is nullptr
05-07 10:27:02.884   56761-9096    C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  I     (RecvSocketPsmOrScn:269)psm or scn = 1, type = 0
05-07 10:27:02.885   56761-56843   C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  E     (OnConnectionStateChanged:345)clientSptr->pimpl->observer_: callback is nullptr
05-07 10:27:02.886   56761-56843   C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  E     (OnConnectionStateChanged:345)clientSptr->pimpl->observer_: callback is nullptr
05-07 10:27:02.939   56761-56843   C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  E     (OnConnectionStateChanged:345)clientSptr->pimpl->observer_: callback is nullptr
05-07 10:27:03.105   56761-56843   C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  E     (OnConnectionStateChanged:345)clientSptr->pimpl->observer_: callback is nullptr
05-07 10:27:03.125   56761-9096    C00101/com.han...bt_fwk_socket  com.hanfe...c.uniapp  I     (Connect:468)recvret: 1
05-07 10:27:03.126   56761-9096    C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():130)SppConnect successfully
05-07 10:27:03.126   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():137)SppConnect execute back
05-07 10:27:03.126   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():146)SppConnect execute back success
05-07 10:27:03.126   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():153)SppConnect execute back successfully
05-07 10:27:03.126   56761-56761   C00101/com.han...et_spp_client  com.hanfe...c.uniapp  I     (operator():161)SppConnect execute Callback mode
05-07 10:27:03.126   56761-56761   C00101/com.han...bt_napi_utils  com.hanfe...c.uniapp  E     (GetCallbackErrorValue:40)errCode: 0
05-07 10:27:03.128   56761-56761   A03D00/com.han....uniapp/JSAPP  com.hanfe...c.uniapp  I     连接打印机成功 72:0A:46:AD:F7:A5 at utils/print.ts:72

更多关于HarmonyOS鸿蒙Next中spp蓝牙连接时不时连接不上并报错2900099的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

您好,为了更快速解决您的问题,并且吸引更多用户一同参与您问题的解答与讨论,建议您补全如下信息:

补全版本信息,让参与用户更快速复现您的问题;

更多提问技巧,请参考:【Tips】如何提个好问题

更多关于HarmonyOS鸿蒙Next中spp蓝牙连接时不时连接不上并报错2900099的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


HarmonyOS Next中SPP蓝牙连接报错2900099通常与底层协议栈异常有关。该错误表明蓝牙服务在建立RFCOMM通道时失败,可能由以下原因导致:

  1. 设备兼容性问题,部分旧设备不符合鸿蒙的蓝牙协议规范;
  2. 系统资源冲突,当多个应用同时请求蓝牙服务时可能触发;
  3. 协议参数不匹配,如UUID配置错误。

可通过adb日志查看具体握手失败阶段,需重点检查蓝牙适配器状态和配对信息缓存。

从日志分析来看,2900099错误是SPP连接过程中出现的常见问题。以下是关键点分析:

  1. 错误原因:
  • 失败日志显示"Connect error 2900099",这是蓝牙底层连接失败的标准错误码
  • 对比成功和失败日志,主要差异在于连接阶段是否成功获取到有效的socket fd(190)
  1. 可能原因:
  • 蓝牙设备未正确配对或配对信息已失效(虽然日志显示pairState=2已配对)
  • 设备蓝牙服务未正常启动或UUID不匹配
  • 射频干扰导致连接不稳定
  1. 建议检查:
  • 确保设备已完全配对(不只是系统级配对)
  • 检查UUID是否与设备端严格一致(当前使用默认SPP UUID)
  • 添加重试机制,捕获2900099错误时自动重连
  • 检查设备是否支持secure连接模式(当前设置为true)

临时解决方案可以增加重试逻辑:

let retryCount = 0;
const maxRetries = 3;

const connectWithRetry = async (option) => {
  try {
    await connect(option);
  } catch (err) {
    if (err.code === 2900099 && retryCount < maxRetries) {
      retryCount++;
      await new Promise(resolve => setTimeout(resolve, 1000));
      return connectWithRetry(option);
    }
    throw err;
  }
};
回到顶部