How to solve the BluetoothGatt : android.os.DeadOb

2019-03-25 08:29发布

问题:

I following the page Bluetooth Low Energy for developing in Android 4.3 for Bluetooth Low Energy .

I already can turn on the Bluetooth , scan the device and connect to the BLE device.

But it show the BluetoothGatt : android.os.DeadObjectException after I try to connect to device(device.connectGatt) and before discover the Service (mBluetoothGatt.discoverServices) for the BLE device.

Before BluetoothGatt : android.os.DeadObjectException it show the log like the following

W/bt-smp  (10670): io_cap = 4
W/bt-smp  (10670): new io_cap = 4 p_cb->loc_enc_size = 16
W/BluetoothEventManager( 7380): CachedBluetoothDevice for device 20:73:20:00:6C:B4 not found, calling readPairedDevices().
D/BluetoothAdapterService(1108123608)(10670): Get Bonded Devices being called
E/BluetoothEventManager( 7380): Got bonding state changed for 20:73:20:00:6C:B4, but we have no record of that device.

The full log when I connect to BLE device before discover Service is like the following

I/BluetoothLeService(10888): BluetoothGattCallback-----newState = 2
I/BluetoothLeService(10888): STATE_CONNECTED:
I/Device_information(10888): BroadcastReceiver---action = ti.android.ble.common.ACTION_GATT_CONNECTED
W/qdhwcomposer(  326): Excessive delay reading vsync: took 816 ms
I/BluetoothBondStateMachine(10670): Bond address is:20:73:20:00:6C:B4
I/BluetoothBondStateMachine(10670): Entering PendingCommandState State
I/BluetoothBondStateMachine(10670): bondStateChangeCallback: Status: 0 Address: 20:73:20:00:6C:B4 newState: 1
D/BluetoothAdapterService(1108123608)(10670): Get Bonded Devices being called
I/BluetoothBondStateMachine(10670): Bond State Change Intent:20:73:20:00:6C:B4 OldState: 10 NewState: 11
W/bt-smp  (10670): io_cap = 4
W/bt-smp  (10670): new io_cap = 4 p_cb->loc_enc_size = 16
W/BluetoothEventManager( 7380): CachedBluetoothDevice for device 20:73:20:00:6C:B4 not found, calling readPairedDevices().
D/BluetoothAdapterService(1108123608)(10670): Get Bonded Devices being called
E/BluetoothEventManager( 7380): Got bonding state changed for 20:73:20:00:6C:B4, but we have no record of that device.
E/BluetoothGatt( 8804): 
E/BluetoothGatt( 8804): android.os.DeadObjectException
E/BluetoothGatt( 8804):     at android.os.BinderProxy.transact(Native Method)
E/BluetoothGatt( 8804):     at android.bluetooth.IBluetoothGatt$Stub$Proxy.clientConnect(IBluetoothGatt.java:739)
E/BluetoothGatt( 8804):     at android.bluetooth.BluetoothGatt.connect(BluetoothGatt.java:715)
E/BluetoothGatt( 8804):     at com.sonyericsson.extras.liveware.aas.AasGatt.connect(AasGatt.java:144)
E/BluetoothGatt( 8804):     at com.sonyericsson.extras.liveware.aas.AasGatt.onStartCommand(AasGatt.java:105)
E/BluetoothGatt( 8804):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2768)
E/BluetoothGatt( 8804):     at android.app.ActivityThread.access$1900(ActivityThread.java:150)
E/BluetoothGatt( 8804):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1395)
E/BluetoothGatt( 8804):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/BluetoothGatt( 8804):     at android.os.Looper.loop(Looper.java:213)
E/BluetoothGatt( 8804):     at android.app.ActivityThread.main(ActivityThread.java:5225)
E/BluetoothGatt( 8804):     at java.lang.reflect.Method.invokeNative(Native Method)
E/BluetoothGatt( 8804):     at java.lang.reflect.Method.invoke(Method.java:525)
E/BluetoothGatt( 8804):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
E/BluetoothGatt( 8804):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
E/BluetoothGatt( 8804):     at dalvik.system.NativeStart.main(Native Method)
D/BluetoothGatt( 8804): connect() - device: 20:73:20:00:6C:B4, auto: false
D/BluetoothGatt( 8804): registerApp()
D/BluetoothGatt( 8804): registerApp() - UUID=3a123b89-8939-4d5b-ae6b-3ca1d98b4208
D/BtGatt.GattService(10670): registerClient() - UUID=3a123b89-8939-4d5b-ae6b-3ca1d98b4208
D/BtGatt.btif(10670): btif_gattc_register_app
D/BtGatt.btif(10670): btgattc_handle_event: Event 1000
D/BtGatt.btif(10670): btif_gattc_upstreams_evt: Event 0
D/BtGatt.GattService(10670): onClientRegistered() - UUID=3a123b89-8939-4d5b-ae6b-3ca1d98b4208, clientIf=5
E/MP-Decision( 2172): Error setting a sleep mode for secondary cores - -38
D/BluetoothGatt( 8804): onClientRegistered() - status=0 clientIf=5
D/BtGatt.GattService(10670): clientConnect() - address=20:73:20:00:6C:B4, isDirect=true
D/BtGatt.btif(10670): btif_gattc_open
D/BtGatt.btif(10670): btgattc_handle_event: Event 1004
D/BtGatt.btif(10670): btif_gattc_upstreams_evt: Event 2
D/BtGatt.GattService(10670): onConnected() - clientIf=5, connId=5, address=20:73:20:00:6C:B4
D/BluetoothGatt( 8804): onClientConnectionState() - status=0 clientIf=5 device=20:73:20:00:6C:B4
D/BluetoothGatt( 8804): discoverServices() - device: 20:73:20:00:6C:B4
D/BtGatt.GattService(10670): discoverServices() - address=20:73:20:00:6C:B4, connId=5
D/BtGatt.btif(10670): btif_gattc_search_service
D/BtGatt.btif(10670): btgattc_handle_event: Event 1006
V/BluetoothMasReceiver( 8128): BluetoothMasReceiver onReceive :android.bluetooth.device.action.BOND_STATE_CHANGED
V/BluetoothMasService( 8128): Map Service onStartCommand
V/BluetoothMasService( 8128): action: android.bluetooth.device.action.BOND_STATE_CHANGED
W/bt-smp  (10670): local rand(LSB ~ MSB) = 86 fe b8 98 d9 17 ec 85 11 5c 06 18 19 72 99 cf 
W/bt-smp  (10670): P1(LSB ~ MSB) = 00 00 01 04 00 05 10 07 07 02 03 00 01 10 07 07 
W/bt-smp  (10670): P1' = r XOR p1(LSB ~ MSB) = 86 fe b9 9c d9 12 fc 82 16 5e 05 18 18 62 9e c8 
W/bt-smp  (10670): Key(LSB ~ MSB) = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
W/bt-smp  (10670): Plain text(LSB ~ MSB) = 86 fe b9 9c d9 12 fc 82 16 5e 05 18 18 62 9e c8 
W/bt-smp  (10670): Encrypted text(LSB ~ MSB) = 09 51 07 24 65 d0 6d a0 b2 63 57 aa 60 28 29 ba 
W/bt-smp  (10670): C1(LSB ~ MSB) = 09 51 07 24 65 d0 6d a0 b2 63 57 aa 60 28 29 ba 
W/bt-smp  (10670): p2(LSB ~ MSB) = b4 6c 00 20 73 20 c5 93 40 62 51 d0 00 00 00 00 
W/bt-smp  (10670): p2' = C1 xor p2(LSB ~ MSB) = bd 3d 07 04 16 f0 a8 33 f2 01 06 7a 60 28 29 ba 
W/bt-smp  (10670): Key(LSB ~ MSB) = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
W/bt-smp  (10670): Plain text(LSB ~ MSB) = bd 3d 07 04 16 f0 a8 33 f2 01 06 7a 60 28 29 ba 
W/bt-smp  (10670): Encrypted text(LSB ~ MSB) = 20 4b 2b ab 92 cb 79 81 68 de 9e 62 d5 ac 4c 7b 
W/bt-smp  (10670): Confirm(LSB ~ MSB) = 20 4b 2b ab 92 cb 79 81 68 de 9e 62 d5 ac 4c 7b 
D/BluetoothMasService( 8128): device: HelloFH
W/bt-smp  (10670): peer rand(LSB ~ MSB) = 41 8d 98 57 20 2d 8f 51 18 93 4e cf 48 39 ac cf 
W/bt-smp  (10670): P1(LSB ~ MSB) = 00 00 01 04 00 05 10 07 07 02 03 00 01 10 07 07 
W/bt-smp  (10670): P1' = r XOR p1(LSB ~ MSB) = 41 8d 99 53 20 28 9f 56 1f 91 4d cf 49 29 ab c8 
W/bt-smp  (10670): Key(LSB ~ MSB) = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
W/bt-smp  (10670): Plain text(LSB ~ MSB) = 41 8d 99 53 20 28 9f 56 1f 91 4d cf 49 29 ab c8 
W/bt-smp  (10670): Encrypted text(LSB ~ MSB) = b2 4c b9 c1 65 2b ec 8f 86 74 82 4b d3 ca c6 9f 
W/bt-smp  (10670): C1(LSB ~ MSB) = b2 4c b9 c1 65 2b ec 8f 86 74 82 4b d3 ca c6 9f 
W/bt-smp  (10670): p2(LSB ~ MSB) = b4 6c 00 20 73 20 c5 93 40 62 51 d0 00 00 00 00 
W/bt-smp  (10670): p2' = C1 xor p2(LSB ~ MSB) = 06 20 b9 e1 16 0b 29 1c c6 16 d3 9b d3 ca c6 9f 
W/bt-smp  (10670): Key(LSB ~ MSB) = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
W/bt-smp  (10670): Plain text(LSB ~ MSB) = 06 20 b9 e1 16 0b 29 1c c6 16 d3 9b d3 ca c6 9f 
W/bt-smp  (10670): Encrypted text(LSB ~ MSB) = 04 9a 63 ea a0 fb 6d 9e 91 70 65 6d 54 26 d0 e1 
W/bt-smp  (10670): Compare(LSB ~ MSB) = 04 9a 63 ea a0 fb 6d 9e 91 70 65 6d 54 26 d0 e1 
W/bt-smp  (10670): Key(LSB ~ MSB) = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
W/bt-smp  (10670): Plain text(LSB ~ MSB) = 86 fe b8 98 d9 17 ec 85 41 8d 98 57 20 2d 8f 51 
W/bt-smp  (10670): Encrypted text(LSB ~ MSB) = 4a 97 52 da 4f ac 7e 4b 48 f1 ba 33 0d df 6f 72 
E/bt-smp  (10670): STK Generated
W/bt-smp  (10670): Key(LSB ~ MSB) = 5e b8 49 86 94 c9 24 d3 79 10 96 8d f0 fa d6 df 
W/bt-smp  (10670): Plain text(LSB ~ MSB) = c9 51 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
W/bt-smp  (10670): Encrypted text(LSB ~ MSB) = dc 0b 54 34 a5 ff f1 6d 69 91 af f4 77 dd 44 09 
W/bt-smp  (10670): Key(LSB ~ MSB) = 67 e6 91 62 7a 0d 29 34 59 17 92 d8 8d 9b fa 89 
W/bt-smp  (10670): Plain text(LSB ~ MSB) = d6 ec 85 b8 8c 5f 22 32 00 00 00 00 00 00 00 00 
W/bt-smp  (10670): Encrypted text(LSB ~ MSB) = cd 25 9e e0 c7 c3 c2 fe 1a 7e 2c 58 01 fa 97 17 
E/bt-smp  (10670): LTK ready
W/bt-smp  (10670): smp_send_enc_info
W/bt-smp  (10670): smp_send_id_info
W/bt-smp  (10670): Key(LSB ~ MSB) = 5e b8 49 86 94 c9 24 d3 79 10 96 8d f0 fa d6 df 
W/bt-smp  (10670): Plain text(LSB ~ MSB) = c9 51 01 00 00 00 00 00 00 00 00 00 00 00 00 00 
W/bt-smp  (10670): Encrypted text(LSB ~ MSB) = 56 68 2a c9 e3 c8 b8 b8 97 5c fd e8 60 93 76 66 
D/Diag_Lib(  507): [IMS_DEBUG]| 1035 | 535 |qpNetSelect : Select Unblocked , iRtn 0, errno 0, errno [Success]
E/Diag_Lib(  507): [IMS_FATAL]| 251 | 535 |qvp_rtp_handle_signals iRet : 0
E/Diag_Lib(  507): [IMS_FATAL]| 238 | 535 |qvp_rtp_handle_signals qpDplMainLoop: Calling imsSignalHandler
D/Diag_Lib(  507): [IMS_DEBUG]| 144 | 535 |qpDpl:imsSignalHandler
E/Diag_Lib(  507): [IMS_FATAL]| 155 | 535 |qpdpl:imsSignalHandler: GLobal data NULL or Event list size is 0
E/Diag_Lib(  507): [IMS_FATAL]| 243 | 535 |qvp_rtp_handle_signals add read fd : 8
E/bt-btif (10670): No More Service found
D/BtGatt.btif(10670): btif_gattc_upstreams_evt: Event 7
D/BtGatt.GattService(10670): onSearchResult() - address=20:73:20:00:6C:B4, uuid=00001801-0000-1000-8000-00805f9b34fb
D/BluetoothGatt( 8804): onGetService() - Device=20:73:20:00:6C:B4 UUID=00001801-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_upstreams_evt: Event 7
D/BtGatt.GattService(10670): onSearchResult() - address=20:73:20:00:6C:B4, uuid=00001800-0000-1000-8000-00805f9b34fb
D/BluetoothGatt( 8804): onGetService() - Device=20:73:20:00:6C:B4 UUID=00001800-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_upstreams_evt: Event 7
D/BtGatt.GattService(10670): onSearchResult() - address=20:73:20:00:6C:B4, uuid=1b7e8251-2877-41c3-b46e-cf057c562023
D/BluetoothGatt( 8804): onGetService() - Device=20:73:20:00:6C:B4 UUID=1b7e8251-2877-41c3-b46e-cf057c562023
D/BtGatt.btif(10670): btif_gattc_upstreams_evt: Event 7
D/BtGatt.GattService(10670): onSearchResult() - address=20:73:20:00:6C:B4, uuid=0000180a-0000-1000-8000-00805f9b34fb
D/BluetoothGatt( 8804): onGetService() - Device=20:73:20:00:6C:B4 UUID=0000180a-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_upstreams_evt: Event 7
D/BtGatt.GattService(10670): onSearchResult() - address=20:73:20:00:6C:B4, uuid=0000180f-0000-1000-8000-00805f9b34fb
D/BluetoothGatt( 8804): onGetService() - Device=20:73:20:00:6C:B4 UUID=0000180f-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_upstreams_evt: Event 6
D/BtGatt.GattService(10670): onSearchCompleted() - connId=5, status=0
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1007
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=133, charUuid=5e0dae24-5e0d-adf0-409b-a23f420d6580, prop=94
D/BtGatt.btif(10670): btif_gattc_get_included_service
D/BtGatt.btif(10670): btgattc_handle_event: Event 1011
E/BtGatt.btif(10670): bta_to_btif_uuid: Unknown UUID length 24028!
D/BtGatt.GattService(10670): onGetIncludedService() - address=20:73:20:00:6C:B4, status=133, uuid=00001801-0000-1000-8000-00805f9b34fb, inclUuid=5e0dae24-5e0d-adf0-409b-a23f420d6580
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1007
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=00002a00-0000-1000-8000-00805f9b34fb, prop=2
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=00002a00-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=00002a01-0000-1000-8000-00805f9b34fb, prop=2
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=00002a01-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=133, charUuid=00002a01-0000-1000-8000-00805f9b34fb, prop=2
D/BtGatt.btif(10670): btif_gattc_get_included_service
I/bt-hci  (10670): BLE HCI(id=62) event = 0x03)
I/bt-hci  (10670): btu_ble_ll_conn_param_upd_evt
D/BtGatt.btif(10670): btgattc_handle_event: Event 1011
D/BtGatt.GattService(10670): onGetIncludedService() - address=20:73:20:00:6C:B4, status=133, uuid=00001800-0000-1000-8000-00805f9b34fb, inclUuid=00002a01-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1007
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=8ac32d3f-5cb9-4d44-bec2-ee689169f626, prop=50
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=8ac32d3f-5cb9-4d44-bec2-ee689169f626
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=5e9bf2a8-f93f-4481-a67e-3b2f4a07891a, prop=10
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=5e9bf2a8-f93f-4481-a67e-3b2f4a07891a
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=d5b8304c-076a-42ce-9d1e-1c859336363f, prop=50
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=d5b8304c-076a-42ce-9d1e-1c859336363f
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=f4bfe056-701d-4af7-a326-d2ecd00173f4, prop=10
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=f4bfe056-701d-4af7-a326-d2ecd00173f4
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=b1736a91-c910-4ad2-ab3a-fd72cc55903d, prop=10
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=b1736a91-c910-4ad2-ab3a-fd72cc55903d
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=ff890e61-5600-4426-88cf-090abe01d0b8, prop=10
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=ff890e61-5600-4426-88cf-090abe01d0b8
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=74de75ce-03b7-4e62-84d0-0d229d456836, prop=10
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=74de75ce-03b7-4e62-84d0-0d229d456836
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=133, charUuid=74de75ce-03b7-4e62-84d0-0d229d456836, prop=10
D/BtGatt.btif(10670): btif_gattc_get_included_service
D/BtGatt.btif(10670): btgattc_handle_event: Event 1011
D/BtGatt.GattService(10670): onGetIncludedService() - address=20:73:20:00:6C:B4, status=133, uuid=1b7e8251-2877-41c3-b46e-cf057c562023, inclUuid=74de75ce-03b7-4e62-84d0-0d229d456836
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1007
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=00002a29-0000-1000-8000-00805f9b34fb, prop=2
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=00002a29-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=00002a24-0000-1000-8000-00805f9b34fb, prop=2
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=00002a24-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=00002a23-0000-1000-8000-00805f9b34fb, prop=2
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=00002a23-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=133, charUuid=00002a23-0000-1000-8000-00805f9b34fb, prop=2
D/BtGatt.btif(10670): btif_gattc_get_included_service
D/BtGatt.btif(10670): btgattc_handle_event: Event 1011
D/BtGatt.GattService(10670): onGetIncludedService() - address=20:73:20:00:6C:B4, status=133, uuid=0000180a-0000-1000-8000-00805f9b34fb, inclUuid=00002a23-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1007
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=0, charUuid=00002a19-0000-1000-8000-00805f9b34fb, prop=2
D/BluetoothGatt( 8804): onGetCharacteristic() - Device=20:73:20:00:6C:B4 UUID=00002a19-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_characteristic
D/BtGatt.btif(10670): btgattc_handle_event: Event 1008
D/BtGatt.GattService(10670): onGetCharacteristic() - address=20:73:20:00:6C:B4, status=133, charUuid=00002a19-0000-1000-8000-00805f9b34fb, prop=2
D/BtGatt.btif(10670): btif_gattc_get_included_service
D/BtGatt.btif(10670): btgattc_handle_event: Event 1011
D/BtGatt.GattService(10670): onGetIncludedService() - address=20:73:20:00:6C:B4, status=133, uuid=0000180f-0000-1000-8000-00805f9b34fb, inclUuid=00002a19-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_descriptor
D/BtGatt.btif(10670): btgattc_handle_event: Event 1009
D/BtGatt.GattService(10670): onGetDescriptor() - address=20:73:20:00:6C:B4, status=133, descUuid=00002a19-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_descriptor
D/BtGatt.btif(10670): btgattc_handle_event: Event 1009
D/BtGatt.GattService(10670): onGetDescriptor() - address=20:73:20:00:6C:B4, status=133, descUuid=00002a19-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_descriptor
D/BtGatt.btif(10670): btgattc_handle_event: Event 1009
D/BtGatt.GattService(10670): onGetDescriptor() - address=20:73:20:00:6C:B4, status=0, descUuid=00002902-0000-1000-8000-00805f9b34fb
D/BluetoothGatt( 8804): onGetDescriptor() - Device=20:73:20:00:6C:B4 UUID=00002902-0000-1000-8000-00805f9b34fb
D/BtGatt.btif(10670): btif_gattc_get_descriptor
D/BtGatt.btif(10670): btgattc_handle_event: Event 1010

I am not sure why the E/BluetoothGatt( 8804): android.os.DeadObjectException happened. After this error happened , I can not discover the service for BLE device when I run mBluetoothGatt.discoverServices.

I have to disconnect and re-connect to BLE device again. And it work fine.

If I clean the data for Bluetooth in Android setting and connect to the BLE device again. The error happened again...

And Sometime it will disconnect immediately after connect , so I have to connect again.

Does somebody has same error or condition ? Please help me or teach me how to solve this problem , it confused a few day...

Thanks in advance! --------------------------------------------------EDIT-----------------------------------------------

The code of the Service is like the following:

package com.ampak.pace;

public class BluetoothLeService extends Service{

      static final String TAG = "BluetoothLeService";
      private static BluetoothAdapter mBluetoothAdapter = null;
      private static BluetoothManager mBluetoothManager = null;
      private BluetoothGatt mBluetoothGatt = null;
      private String mBluetoothDeviceAddress;
      private BluetoothDevice device;
      public static String address = null;
      public static Handler handler=new Handler(Looper.getMainLooper());
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

        public void onConnectionStateChange(android.bluetooth.BluetoothGatt gatt, int status, int newState) {
            if(mBluetoothGatt == null){
                Log.e(TAG, "mBluetoothGatt not created!");
                return;
            }

            device = gatt.getDevice();
            address = device.getAddress();

            try {
                switch (newState) {
                case BluetoothAdapter.STATE_CONNECTED:

                    Log.i(TAG, "STATE_CONNECTED:");
                    broadcastUpdate(ACTION_GATT_CONNECTED, device, status);     
                    break;
                case BluetoothAdapter.STATE_DISCONNECTED:
                    Log.i(TAG, "STATE_DISCONNECTED:");
                    gatt.close();
                    broadcastUpdate(ACTION_GATT_DISCONNECTED, device, status);
                    break;

                default:
                    Log.i(TAG, "New state not processed: " + newState);
                    break;
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }


        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            if(status == BluetoothGatt.GATT_SUCCESS) {
            }
        };

        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if(status == BluetoothGatt.GATT_SUCCESS){
                Log.i(TAG, "onServicesDiscovered-----GATT_SUCCESS@@@@@@ = ");
            }
        };
    };


    private void broadcastUpdate(final String action, final BluetoothDevice device , final int status) {
        // TODO Auto-generated method stub
        final Intent intent = new Intent(action);
        intent.putExtra(EXTRA_UUID, device);
        intent.putExtra(EXTRA_STATUS, status);

        sendBroadcast(intent);
    }

    public boolean initialize() {
        // TODO Auto-generated method stub
        if(mBluetoothManager == null){
            Log.e(TAG, "BluetoothManager initialize@@@");
            mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
            if(mBluetoothManager == null){
                Log.e(TAG, "Unable to initialize BluetoothManager");
                  return false;
            }
        }

        mBluetoothAdapter = mBluetoothManager.getAdapter();
        if(mBluetoothAdapter == null){
            Log.e(TAG, "Unable to obtain a BluetoothAdapter");
            return false;
        }
        return true;    
    }

    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); 

    }

    public void disconnect(String address) {
        // TODO Auto-generated method stub
        if(mBluetoothAdapter == null){
            Log.w(TAG, "disconnect: BluetoothAdapter not initialized");
              return;
        }

        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);

        if(mBluetoothGatt != null){
            Log.i(TAG, "disconnect");
            if(connectionState != BluetoothProfile.STATE_DISCONNECTED){
                mBluetoothGatt.disconnect();
            }else{
                Log.w(TAG, "Attempt to disconnect in state: " + connectionState);
            }
        }   
    }




    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    //private final IBinder binder = new LocalBinder();
    private final LocalBinder binder = new LocalBinder();
    public class LocalBinder extends Binder{
        public BluetoothLeService getService() {
            return BluetoothLeService.this;
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return binder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onUnbind");
        close();
        return super.onUnbind(intent);
    }

    public void close() {
        // TODO Auto-generated method stub
        if (mBluetoothGatt != null) {
            mBluetoothGatt.close();
            mBluetoothGatt = null;
        }
    }       
}

回答1:

I solved this by disconnecting, closing the Gatt and stopping the service and then restart it again:

            mBluetoothLeService.disconnect();
            mBluetoothLeService.close();
            mBluetoothLeService.stopSelf();
            connectBleservice();


回答2:

My workaround to the problem was to delay the reconnection:

private void connectGatt(final String address) {
    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            bluetoothGatt = bluetoothManager.getAdapter().getRemoteDevice(address).connectGatt(context, true, bluetoothGattCallback);
        }
    }, 500);
}


回答3:

The dreaded DeadObjectException is an unfortunate reality of Android BLE and after 6 months working with the API I never figured out what it is or how to prevent it. It can happen in other circumstances than what you're describing. For example for me it usually happens when calling BluetoothDevice#getName() after discovery.

Anyway, the best thing you can do is wrap the problem call with:

try
{
    // your DeadObjectException inducing code.
}
catch(Exception e)
{
    // Can't actually catch the DeadObjectException itself for some reason...*shrug*.
    if( e instanceof DeadObjectException )
    {
        // notify user through a dialog or something that they should either restart bluetooth or their phone.
        // another option is to reset the stack programmatically.
    }
    else
    {
        // your choice of whether to rethrow it or treat it the same as DeadObjectException.
    }
}

Note that the Bluetooth stack can also throw random NullPointerExceptions that can really only be dealt with the same way - either telling your user how to reset phone or bluetooth, or trying to programmatically restart bluetooth.



回答4:

This problem is very tricky to solve but seems to have to do with some sort of race condition in the android bluetooth stack. Doug's answer of catching Exception e and then checking if its a dead object did not work when I tried. I did notice that when I call

bluetoothGatt.connect()

a return value of false can indicate a dead object exception.