4
  • After doing disconnecte ble device i am getting disconnect callback . but some time still it is not disconnected . in some layer connection state is maintaining . so that i am not able to do reconnect.

i have tested in android 5 & android 6. in HTC One A9, Moto x play, Moto G4

  • If i do bluetooth turn on off. then again disconnect callback is coming and device is disconnecting actually. -Please give some suggestion for resolve issue.
  • I am doing below steps for ble operation
  • 1.Discover ble device.
    1. Connect to device.
    2. onConnectionStateChange (connected) i am doing gatt.discoverServices()
    3. onServicesDiscovered callback i am reading characteristics 5.onCharacteristicRead callback i am doing write characteristics. 6.onCharacteristicWrite call back i am doing gatt.disconnect()
    4. onConnectionStateChange (disconnected) i am doing gatt.close()

In this full process in background device scanning is going on.

Parth
  • 1,908
  • 1
  • 19
  • 37
  • If you certainly call gatt.disconnect() then it will disconnect. If it doesn't there is a bug in the Android BLE stack. – Emil Jun 07 '16 at 22:16
  • 2
    I am having the same issue... :( Any updates on this? Any luck? (Using API 21) – Tanasis Jun 25 '16 at 10:42
  • - for me its working by considering things 1) do not scan while doing operation on connected device. – Parth Jun 27 '16 at 06:55
  • 1
    I'm having the same problem. Now I just close and null the BluetoothGatt. That works except on a Samsung Galaxy S4. On that device, I have to wait 15-20 seconds after disconnecting/closing before I connect again or I will have to turn Bluetooth off and on again to get it to work. – James H Oct 23 '16 at 03:47
  • see this answer, it may help fix your issue https://stackoverflow.com/a/63187218/2296798 – Nick Jul 31 '20 at 07:11

2 Answers2

4

You need to ensure that you connect to your device no more than once. I found that without adding your own protection, you can inadvertently have multiple connections with one device (that is multiple BluetoothGatt objects in existence) simultaneously. You can communicate with the device via any of these BluetoothGatt objects, so you don't notice the problem at that point. But when you try to disconnect you (erroneously) leave connections open.

To remove this risk you need code roughly like this:

BluetoothGatt mBluetoothGatt;
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {            
        if (device != null) {
            Log.d(TAG, "LeScanCallback!!!!  device " + device.getAddress());

            boolean foundDevice = false;

            if (device.getName() != null) {
                Log.d(TAG, "LeScanCallback!!!!  " + device.getName());
                // Put your logic here!
                if (device.getName().compareTo("YOUR_DEVICE") == 0) {
                    Log.d(TAG, "Found device by name");
                    foundDevice = true;
                }
                else {
                    Log.d(TAG,"Found " + device.getName());
                }
            }

            if(mBluetoothGatt == null && foundDevice) {
                mBluetoothGatt = device.connectGatt(getApplicationContext(), false, mGattCallback);
                // Make sure to handle failure cases in your callback!

                Log.d(TAG, "Stopping scan."); //Appropriate only if you want to find and connect just one device.
                mBluetoothAdapter.stopLeScan(this);
            }
        }
    }
};
ianinini
  • 630
  • 6
  • 13
3

this problem can be connected with not calling stopScan() method. see comment from SoroushA Totally Disconnect a Bluetooth Low Energy Device

user2474486
  • 213
  • 3
  • 8