Android中不稳定的BLE连接

Posted

技术标签:

【中文标题】Android中不稳定的BLE连接【英文标题】:Unstable BLE connection in Android 【发布时间】:2014-05-30 09:43:26 【问题描述】:

我正在遵循在 android 4.3 中针对低功耗蓝牙进行开发的指南。

当尝试从Android手机连接BLE设备时,有时会在连接到BLE设备后立即断开。

连接代码为:

public void connect(final String address) 
        // TODO Auto-generated method stub
        Log.w(TAG, "BluetoothLeService Connect function.");
        if(mBluetoothAdapter == null || address == null)
            Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        

        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        int connectState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
        mBluetoothGatt = device.connectGatt(this, true, mGattCallback); 

    

生成的日志是:

D/BluetoothGatt(13308): onClientConnectionState() - status=0 clientIf=4 device=20:73:20:00:6C:B4
I/BluetoothLeService(13308): BluetoothGattCallback-----newState = 2
I/BluetoothLeService(13308): STATE_CONNECTED:
I/Device_information(13308): BroadcastReceiver---action = ti.android.ble.common.ACTION_GATT_CONNECTED
I/BluetoothLeService(13308): ServiceDiscover
D/BluetoothGatt(13308): discoverServices() - device: 20:73:20:00:6C:B4
D/BtGatt.GattService(12125): discoverServices() - address=20:73:20:00:6C:B4, connId=4
D/BtGatt.btif(12125): btif_gattc_search_service
D/BtGatt.btif(12125): btgattc_handle_event: Event 1006
E/MP-Decision( 2172): Error setting a sleep mode for secondary cores - -38
E/BtGatt.GattService(12125): getService() - Service requested, but not available!
W/bt-l2cap(12125): L2CA_EnableUpdateBleConnParams - unknown BD_ADDR 207320006cb4
E/bt-btm  (12125): btm_sec_disconnected - Clearing Pending flag
D/BtGatt.btif(12125): btif_gattc_upstreams_evt: Event 6
D/BtGatt.GattService(12125): onSearchCompleted() - connId=4, status=129
D/BluetoothGatt(13308): onSearchComplete() = Device=20:73:20:00:6C:B4 Status=129
I/BluetoothLeService(13308): onServicesDiscovered@@@@@@-----State = 129 =
W/BluetoothLeService(13308): BluetoothLeService Connect function.
D/BluetoothManager(13308): getConnectionState()
D/BluetoothManager(13308): getConnectedDevices
D/BluetoothAdapterService(1107670808)(12125): Get Bonded Devices being called
D/BluetoothAdapterProperties(12125): getBondedDevices: length=1
D/BtGatt.GattService(12125): getDeviceType() - device=20:73:20:00:6C:B4, type=2
D/BluetoothGatt(13308): connect() - device: 20:73:20:00:6C:B4, auto: true
D/BluetoothGatt(13308): registerApp()
D/BluetoothGatt(13308): registerApp() - UUID=de8cc9a9-98e5-45d7-84f5-709bbd76e680
D/BtGatt.GattService(12125): registerClient() - UUID=de8cc9a9-98e5-45d7-84f5-709bbd76e680
D/BtGatt.btif(12125): btif_gattc_register_app
D/BtGatt.btif(12125): btif_gattc_upstreams_evt: Event 5
D/BtGatt.GattService(12125): onDisconnected() - clientIf=4, connId=4, address=20:73:20:00:6C:B4
D/BluetoothGatt(13308): onClientConnectionState() - status=133 clientIf=4 device=20:73:20:00:6C:B4
I/BluetoothLeService(13308): BluetoothGattCallback-----newState = 0
I/BluetoothLeService(13308): STATE_DISCONNECTED:

看来断线的主要原因是:

E/BtGatt.GattService(12125): getService() - Service requested, but not available!
W/bt-l2cap(12125): L2CA_EnableUpdateBleConnParams - unknown BD_ADDR 207320006cb4
E/bt-btm  (12125): btm_sec_disconnected - Clearing Pending flag

谁能解释为什么会这样?

【问题讨论】:

【参考方案1】:

首先你应该意识到Android BLE unstable。你的任务呢,你的 BLE 设备似乎无法提供一些被发现的服务。这可能是设备大脑损坏的原因。如果您在调用该服务后稳定失去连接,则可能根本不应该调用它。

【讨论】:

以上是关于Android中不稳定的BLE连接的主要内容,如果未能解决你的问题,请参考以下文章

如何在Android中为BLE编写快速稳定的特性?

Java cloudera impala 连接不稳定:使用 [Simba][ImpalaJDBCDriver](500151) 错误设置/关闭会话:0

在 Android 中不接收 BLE 通知

Android BLE开发的各种坑

Android 蓝牙低功耗 (BLE) API 尚未准备好迎接黄金时段

根据您的最低稳定性设置,该软件包在足够稳定的版本中不可用