Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent BluetoothGattCallback from being executed multiple times at a time

I have a service that has one instance of BluetoothGattCallback

public class MyService extends Service {

    private BluetoothGattCallback callback;

    @Override
    public void onCreate() {
            super.onCreate();

            callback = new BluetoothGattCallback() {
                      @Override
                      public synchronized void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                              Log.i("onConnectionStateChanged", "Status " + status);                
                              Log.i("onConnectionStateChanged", "New State " + newState);                
                      }
            };
    }

    // registration of bluetooth adapter and blah blah blah


}

When I start the app, it works fine and the callback only gets called once, but after a couple of tries, it gets called twice.

Sample logs

10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 0
10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 2
10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 0
10-22 13:29:18.731 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 2

More Sample logs

10-22 13:29:48.836 26944-26961/redacted.lollipop I/onConnectionStateChange: Status 8
10-22 13:29:48.836 26944-26961/redacted.lollipop I/onConnectionStateChange: New State 0
10-22 13:29:48.850 26944-30763/redacted.lollipop I/onConnectionStateChange: Status 8
10-22 13:29:48.850 26944-30763/redacted.lollipop I/onConnectionStateChange: New State 0

And it gets called a lot more times the longer the app stays active. How do I prevent this?

like image 299
Kevin D. Avatar asked Oct 22 '15 05:10

Kevin D.


1 Answers

One thing to keep in mind is that each time you call

bluetoothDevice.connectGatt(context, true, callback);

It creates a new instance of the bluetoothGatt object. check out the source for this one you will see:

         BluetoothGatt gatt = new BluetoothGatt(context, iGatt, this, transport);
         gatt.connect(autoConnect, callback);

So one tricky thing is that if your device disconnects and you re-connect to it with. connectGatt(context, true, callback); instead of calling connect() on the previous bluetoothGatt instance you will get 2 bluetoothGatt instances that both have a handle to your gatt callback.

Initially I was trying to fix the problem by trying to close and disconnect the bluetoothGatt before reconnecting.

   if (service.bluetoothGatt!=null){
        Log.i("Rides","Closeing bluetooth gatt on disconnect");
        service.bluetoothGatt.close();
        service.bluetoothGatt.disconnect();
        service.bluetoothGatt=null;
    } 

But this did not work well, somehow I would get multiple onConnectionStateChanged callbacks.

I was able to resolve this problem by checking if I has a valid bluetoothGatt object and making sure to call connect() on it if its a reconnection.

---- Updated Answer ----

I have found that its better to call bluetoothGatt.close() inside the onConnectionStateChanged callback. When you issue a disconnect it sends a message to the bluetooth device to request disconnect. Then once it responds you get the callback and close the bluetooth gatt connection. By waiting for the callback and not opening another gatt connection until its fully closed it seems to prevent multiple gatt objects from getting connected to the app.

like image 132
Marc Avatar answered Nov 15 '22 21:11

Marc