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?
connectGatt
method, which is synchronized & locked by the callback itself, e.g. synchronized(callback) { device.connectGatt(getBaseContext(), true, callback, 2); } – Tronna