You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android蓝牙栈与BLE踏频传感器随机断开连接问题求助

问题日志(中文翻译)

2023/09/01 18:33:03.457 Info bt_stack [INFO:btsnoop.cc(361)] clear_l2cap_allowlist: 清除L2CAP通道的允许列表。conn_handle=4 cid=4:4
2023/09/01 18:33:03.457 Info bt_stack [INFO:gatt_attr.cc(338)] gatt_connect_cback: 移除不可信客户端状态,bda=d9:12:e7:b6:c5:44
2023/09/01 18:33:03.457 Info bluetooth system/bt/stack/gap/gap_ble.cc:298 client_connect_cback: 未找到对等设备xx:xx:xx:xx:c5:44的活跃GAP服务,回调:已断开连接
2023/09/01 18:33:03.457 Info bt_bta_gattc system/bt/bta/gatt/bta_gattc_act.cc:1115 bta_gattc_conn_cback: 已断开连接 att_id:3 传输方式:le 原因:GATT_CONN_TIMEOUT
2023/09/01 18:33:03.457 Info bt_bta_gattc system/bt/bta/gatt/bta_gattc_act.cc:1115 bta_gattc_conn_cback: 已断开连接 att_id:4 传输方式:le 原因:GATT_CONN_TIMEOUT
2023/09/01 18:33:03.457 Info bt_bta_gattc system/bt/bta/gatt/bta_gattc_act.cc:1115 bta_gattc_conn_cback: 已断开连接 att_id:7 传输方式:le 原因:GATT_CONN_TIMEOUT
2023/09/01 18:33:03.457 Warn bt_btm_sec system/bt/stack/btm/btm_sec.cc:3669 btm_sec_disconnected: 收到不常见的断开原因:连接超时 handle:0x0004
2023/09/01 18:33:03.458 Info btif_av system/bt/btif/src/btif_av.cc:3352 btif_av_acl_disconnected: btif_av_acl_disconnected: 对等设备d9:12:e7:b6:c5:44:ACL已断开
2023/09/01 18:33:03.459 Info btif_av system/bt/btif/src/btif_av.cc:1078 FindOrCreatePeer: BtifAvPeer *BtifAvSource::FindOrCreatePeer(const RawAddress &, tBTA_AV_HNDL): 创建对等设备:peer_address=d9:12:e7:b6:c5:44 bta_handle=0x41 peer_id=0
2023/09/01 18:33:03.459 Info btif_av system/bt/btif/src/btif_av.cc:3279 btif_av_get_peer_sep: 未找到活跃的接收器或源对等设备
2023/09/01 18:33:03.459 Info bt_btif_a2dp_source system/bt/btif/src/btif_a2dp_source.cc:691 btif_a2dp_source_on_idle: btif_a2dp_source_on_idle: 状态=STATE_OFF
2023/09/01 18:33:03.459 Info btif_av system/bt/btif/src/btif_av.cc:3279 btif_av_get_peer_sep: 未找到活跃的接收器或源对等设备
2023/09/01 18:33:03.459 Info bt_btif_a2dp_source system/bt/btif/src/btif_a2dp_source.cc:691 btif_a2dp_source_on_idle: btif_a2dp_source_on_idle: 状态=STATE_OFF
2023/09/01 18:33:03.459 Info btif_av system/bt/btif/src/btif_av.cc:1123 DeleteIdlePeers: DeleteIdlePeers: 删除闲置对等设备:d9:12:e7:b6:c5:44 bta_handle=0x41


问题定位

核心原因是蓝牙GATT连接超时,这种随机断开通常和BLE连接参数、设备闲置判定、Android蓝牙栈资源管理有关,结合Quest 2(Android 12)环境,可按以下方案修复:


解决方案

  • 调整BLE连接优先级
    建立GATT连接后,主动设置高优先级连接,降低蓝牙栈判定闲置的概率:

    if (gatt != null) {
        // 高优先级=低延迟+高功耗,适合实时数据场景
        gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
    }
    
  • 添加心跳保活机制
    定期向传感器发送空的GATT读请求(比如读设备名称或只读特征),避免连接被判定为闲置:

    private Timer keepAliveTimer;
    
    private void startKeepAlive() {
        keepAliveTimer = new Timer();
        // 每30秒发送一次保活请求
        keepAliveTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (gatt == null) return;
                // 替换为传感器的某个只读特征UUID
                BluetoothGattCharacteristic chara = gatt.getService(SERVICE_UUID)
                        .getCharacteristic(KEEP_ALIVE_CHAR_UUID);
                if (chara != null) {
                    gatt.readCharacteristic(chara);
                }
            }
        }, 0, 30000);
    }
    
    // 断开连接时记得取消定时器
    private void stopKeepAlive() {
        if (keepAliveTimer != null) {
            keepAliveTimer.cancel();
            keepAliveTimer = null;
        }
    }
    
  • 优化蓝牙资源释放

    • 在onConnectionStateChange()回调中,断开连接时必须调用gatt.close(),避免蓝牙栈残留无效连接实例。
    • 确保一个传感器对应一个BluetoothGatt实例,不要重复创建。
    • 确认已申请Android 12+要求的BLUETOOTH_CONNECT、BLUETOOTH_SCAN权限,并完成运行时授权。
  • 传感器侧设置检查
    部分踏频传感器在静止时会自动进入休眠或降低广播频率,可尝试:

    • 关闭传感器的自动休眠功能(如果支持)。
    • 静止时模拟踏动动作触发数据传输,维持连接活性。

内容的提问来源于stack exchange,提问作者Boodums

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 19:40:24