6

I'm trying to implement my own timeout on my bluetooth GATT services by schedule a timer and call BluetoothGatt.disconnect() manually. But the callback is not called like what usually happen if the disconnect is triggered from the remote devices. There is also a log from the BluetoothGatt that the disconnect function is called

D/BluetoothGatt﹕ cancelOpen() - device: 00:07:80:04:1A:5A

and this is my code to disconnect

private void scheduleDisconnect() {
    isTimerRunning = true;
    disconnectTimer = new Timer();
    disconnectTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            isTimerRunning = false;
            disconnect();
        }
    }, 2000);
}

Why is onConnectionStateChange not called? It's working well for another callback and action

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
  • I faced to the same issue. It is so random. I call both disconnect() and then close() method. The onConnectionStateChange called for sometime but not always. – Paul Jan 08 '15 at 09:19
  • See also here: http://stackoverflow.com/questions/23110295/difference-between-close-and-disconnect-in-android-bluetooth-4-0-api – RenniePet Jun 04 '15 at 00:46

2 Answers2

2

Is your disconnect() method closing the connection as well? Only call BluetoothGatt.close() when you are done with the device, or else your callbacks will be unregistered.

Ron
  • 228
  • 3
  • 9
  • I could kiss you! This was my problem. Thanks a bunch! I was calling my close method inside of my disconnect method so it "unregistered" before it made the call. – luckyging3r Mar 21 '18 at 14:46
0

To disconnect my device, I use the gatt link of the device and I use the methods BluetoothGatt.disconnect() and BluetoothGatt.close().

This worked, but didn't call onConnectionStateChange(), because I close the flux.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Flatch
  • 36
  • 1
  • 4