3

Turning off the bluetooth in Android system settings gives us the BluetoothGattCallback.onConnectionStateChange() call in Android < 27 (Oreo). However when I try it on my Pixel or Nexus 5X (Android 8.1.0) I don't get that callback. Is there some change to the bluetooth code where we don't get notified of BluetoothProfile.STATE_DISCONNECTED with the bluetooth setting change. I notice the device gets disconnected. Also when turning off the BLE device that callback gets called. Any insight is appreciated!

Filed as an issue here Google Issue

Dhananjay Suresh
  • 1,030
  • 1
  • 14
  • 28
  • You have to check `BluetoothGatt.STATE_DISCONNECTING` or `BluetoothGatt.STATE_DISCONNECTED` in the connection callback instead of `BluetoothProfile` – HawkPriest Jul 18 '18 at 18:55
  • @HawkPriest `BluetoothGatt` and `BlueoothProfile` both use the same constants for `STATE_DISCONNECTED`. `BluetoothGatt` implements `BluetoothProfile`. – Dhananjay Suresh Jul 18 '18 at 19:04
  • Were you able to solve this? I have run into the same problem. – Adrian Feb 18 '20 at 09:30
  • 1
    @Adrian I was not and I don't know if they had any resolutions. I left the project before I was able to come up with a solution. – Dhananjay Suresh Mar 11 '20 at 22:30

1 Answers1

0

I guess this is kind of expected, or at least this has always been the behaviour. When Bluetooth is turned off (or dies for some reason), the Bluetooth daemon process quits. But it's the same process that also sends out your GATT callbacks. If the process is terminated, it can't send any callbacks.

Use https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#ACTION_STATE_CHANGED instead to detect when Bluetooth is turned off, and at that time you should just go and close all BluetoothGatt objects.

Emil
  • 16,784
  • 2
  • 41
  • 52