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! :)