mach mach - 5 months ago 133
Android Question

Bluetooth GATT - onServicesDiscovered(BluetoothGatt gatt, int status) does not contain all Services

I have code that connects to, and then tries to discover all Services provided by a custom BLE server device advertising custom Services.

The code works on


  • Nexus 5 with Android 5.1

  • Samsung SM-T320 with Android 4.4.2

  • ZTE Blade Vec 4G With Android 4.4.2



Code does not work on


  • Samsung SM-T360 with Android 4.4.4

  • Samsung GT-I9195 with Android 4.4.2



On devices where it fails it often discovers all of the BLE Services on one (of two) devices that advertises the same BLE Services. Restarting the Android device and/or fiddling with wifi/bt makes it discover all the Services on the other BLE device but then not the first (!).

Anyone knows of a outstanding bug with the Samsung devices regarding BLE Service discovery?

Has all the Services:

04-16 13:28:00.999: V/ScanningState(9741): onServicesDiscovered()
04-16 13:28:00.999: I/ScanningState(9741): The device has service 00001800-0000-1000-8000-00805f9b34fb
04-16 13:28:00.999: I/ScanningState(9741): The device has service 0000180a-0000-1000-8000-00805f9b34fb
04-16 13:28:00.999: I/ScanningState(9741): The device has service 0000180f-0000-1000-8000-00805f9b34fb
04-16 13:28:00.999: I/ScanningState(9741): The device has service f0ba0000-c6b5-11e2-8b8b-0800200c9a66
04-16 13:28:00.999: I/ScanningState(9741): The device has service f0ba0100-c6b5-11e2-8b8b-0800200c9a66
04-16 13:28:00.999: I/ScanningState(9741): The device has service f0ba1100-c6b5-11e2-8b8b-0800200c9a66


Missing Services:

04-16 14:05:31.179: V/ScanningState(10710): onServicesDiscovered()
04-16 14:05:31.179: I/ScanningState(10710): The device has service 00001800-0000-1000-8000-00805f9b34fb
04-16 14:05:31.179: I/ScanningState(10710): The device has service 0000180a-0000-1000-8000-00805f9b34fb
04-16 14:05:31.179: I/ScanningState(10710): The device has service 0000180f-0000-1000-8000-00805f9b34fb
04-16 14:05:31.179: I/ScanningState(10710): The device has service f0ba0000-c6b5-11e2-8b8b-0800200c9a66
04-16 14:05:31.179: W/ScanningState(10710): Device is missing sensor service
04-16 14:05:31.179: W/ScanningState(10710): Device is missing storage service
04-16 14:05:31.179: I/ScanningState(10710): Candidate did not support required services


For the second (failing) scan I also see the following logs:

04-16 14:24:26.310: D/BtGatt.GattService(2856): onGetCharacteristic() - address=00:07:80:13:14:33, status=133, charUuid=00000000-0000-0000-0000-000000000000, prop=0
04-16 14:24:26.310: D/BtGatt.btif(2856): btif_gattc_get_included_service
04-16 14:24:26.310: D/BtGatt.btif(2856): btgattc_handle_event: Event 1011
04-16 14:24:26.310: E/bt-btif(2856): No server cache available
04-16 14:24:26.310: E/BtGatt.btif(2856): bta_to_btif_uuid: Unknown UUID length 25104!


Update:

Further investigation shows that the onServicesDiscovered(BluetoothGatt gatt, int status) returns all the BLE Services if only one BLE service device is present at a time. Some kind of resource leak in the lower layers?

Answer

Ok so the short answer is do not call connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback) on any other thread but the main thread.

If I got a onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) while connecting to a device I cached the reference and later called connectGatt() inside the callback onConnectionStateChange(BluetoothGatt gatt, int status, int newState) after a disconnect from the first device. This caused the issue described in this question.