0

This problem bothers me for a few days. The code for bluetooth connection work fine on the first kind of devices (heart rate detector) with other phones. For the second kind of devices (also heart rate detector) they work fine with mobile such as htc, asus. However, most of the second kind of devices can not connect well to sony xperia Z1~Z3, samsung N5(I'm not sure the actual model).

To observe the logcat, the error occurred while calling gatt.discoverServices(). I call this method in BluetoothGattCallback -> onConnectionStateChange -> newState == STATE_CONNECTED.

It's normal to get services like this:

05-04 12:08:15.678: D/BtGatt.btif(23132): btif_gattc_upstreams_evt: Event 7
05-04 12:08:15.678: D/BtGatt.GattService(23132): onSearchResult() - address=F9:CE:5F:A3:69:CF, uuid=00001800-0000-1000-8000-00805f9b34fb
05-04 12:08:15.678: D/BluetoothGatt(22365): onGetService() - Device=F9:CE:5F:A3:69:CF UUID=00001800-0000-1000-8000-00805f9b34fb

But here I got six services, in other device I got seven services. (lose one customize UUID)

service:

onGetService() - Device=F9:CE:5F:A3:69:CF UUID=00001800-0000-1000-8000-00805f9b34fb
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=00001801-0000-1000-8000-00805f9b34fb
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=0000180a-0000-1000-8000-00805f9b34fb
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=0000180d-0000-1000-8000-00805f9b34fb
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=6c721838-5bf1-4f64-9170-381c08ec57ee
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=6c721550-5bf1-4f64-9170-381c08ec57ee

The following is the excerpts of logcat that I thought is an incorrect result:

05-04 12:08:15.678: D/BtGatt.btif(23132): btif_gattc_get_characteristic
05-04 12:08:15.678: D/BtGatt.btif(23132): btgattc_handle_event: Event 1007
05-04 12:08:15.678: E/bt-btif(23132): No server cache available
05-04 12:08:15.678: D/BtGatt.GattService(23132): onGetCharacteristic() - address=F9:CE:5F:A3:69:CF, status=133, charUuid=449e4528-7633-6ccc-8000-00805f9b34fb, prop=2
05-04 12:08:15.678: D/BtGatt.btif(23132): btif_gattc_get_included_service
05-04 12:08:15.678: D/BtGatt.btif(23132): btgattc_handle_event: Event 1011
05-04 12:08:15.678: E/bt-btif(23132): No server cache available
05-04 12:08:15.678: E/BtGatt.btif(23132): bta_to_btif_uuid: Unknown UUID length 30209!
05-04 12:08:15.688: D/BtGatt.GattService(23132): onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=00001800-0000-1000-8000-00805f9b34fb, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
05-04 12:08:15.688: D/BtGatt.GattService(23132): continueSearch() - connid=5, status=0

These logcat repeat six times for every service in the beginning with the same charateristic UUID = 449e4528-7633-6ccc-8000-00805f9b34fb. And this UUID is not appeared in other successful case.

characteristic: (just paste the different part)

onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=00001800-0000-1000-8000-00805f9b34fb, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=00001801-0000-1000-8000-00805f9b34fb, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=0000180a-0000-1000-8000-00805f9b34fb, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=0000180d-0000-1000-8000-00805f9b34fb, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=6c721838-5bf1-4f64-9170-381c08ec57ee, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=6c721550-5bf1-4f64-9170-381c08ec57ee, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb

I've found a similar question: onServiceDiscoverd does not contain all service

But I call every device.connectGatt() in runOnUiThread after onLeScan(), I also tried to call a specific address for the method while click button(be sure to runOnUiThread).

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("F4:36:8C:9B:87:87");
device.connectGatt(this, false, mGattCallback);

This solution is not work.

I also tried to close the wifi and restart the bluetooth. This works for mi2, but not xperia Z1.

Does anyone have any idea? Thanks a lot! :)

Community
  • 1
  • 1
ginnyhuang
  • 63
  • 6
  • Do you bond (pair) to the devices? – Alexander Farber May 09 '15 at 09:27
  • @Alexander Farber :Thanks for your comment. Hmm...Do you mean to pair the bluetooth device in the setting page first? What I do is to do **connectGatt()** first in **onLeScan()**, than do **discoverServices()** in **gattCallback**. Cause for the first heartrate detector there is no need to do any action for pair (with xperia Z1). And for second detector with other mobile there is no need to pair, either. – ginnyhuang May 12 '15 at 02:56
  • Not manually in "Settings page" )) There is a method for that: [BluetoothDevice.createBond()](https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createBond%28%29) – Alexander Farber May 12 '15 at 08:15
  • Thanks, I'll check this method :). Maybe I need to create a receiver to listen for the bond state. I just consider that BLE services will bond by itself before, won't it? – ginnyhuang May 12 '15 at 10:40
  • Actually there is already a bonded receiver be registered. I just add a log to obtain the bonded list and device.getBondState(). It doesn't have any effect first. But after I connect to another second type heartrate detector success, I connect to this device again and it success. I even don't know why. Orz The bonded list has 8 addresses but doesn't include the device a tried. The bondState for the device I tried is 10. The bonded list doesn't contain any address of the device I've tried. – ginnyhuang May 13 '15 at 10:03
  • Did you find a solution to this? – KVISH Sep 12 '15 at 18:49
  • I've got an idea a few days ago from that day. I thought the code I add make a time interval between getting gatt connect callback and command to do discover service. So I send discoverServices() delay about 5 seconds after get gattCallback. Now it works find almost every time. But now I need at least 5 seconds to do BLE connection...LOL (Actually I use 10 seconds in the beginning) This make my code ugly and a little bit stupid. QAQ I've not sure there is any better way to solve this problem. – ginnyhuang Sep 12 '15 at 19:50

0 Answers0