I am following the guidelines for developing in Android 4.3 for Bluetooth Low Energy.
When trying to connect to BLE device from an Android phone, sometimes it will disconnect immediately after connect to BLE device.
The connection code is:
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);
}
The resulting log is:
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:
It seems the main reason for the disconnection is:
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
Can anyone explain why this happens?