1

我正在遵循在 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

谁能解释为什么会这样?

4

1 回答 1

-4

First you should realize that Android BLE unstable at all. What about your task, it seems your BLE device unalble provide some service that was discovered. It can be device brain damaged reason. If you have stable lost connection after that service was called, possibly you shouldn't call it at all.

于 2014-05-30T12:19:44.173 回答