2

I am developping a Android app that connects to multiple BLE devices at the same time, after that i read characteristic permanently from those devices but after a while, I am getting a status 257 in the onConnectionStateChanged() function, the android documentation doesn't explain what is the reason of the error, or how to fix it.

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            Log.i("TAG","onConnectionStateChange, status : "+status+" parseConnection : "+ GattStatusParse.parseConnection(status)+"  or "+ GattStatusParse.parseConnection(status));
            isConnected = BluetoothProfile.STATE_CONNECTED == newState;
            if (status == BluetoothGatt.GATT_SUCCESS) {
                   if (isConnected) {
                    Log.i(TAG, "GATT connected." + connectedBluethoothDevice.toString());

                    gatt.discoverServices();
              } else {
                     Log.i("TAG"," GATT disconnected " + device.getAddress() + " state of the opération : " + status + " connexion state : " + newState);
                    if (connectedBluethoothDevice.contains(device)) {
                        connectedBluethoothDevice.remove(device);
                    }
             }else{
                if (connectedBluethoothDevice.contains(device)) {                     
                    int mConnectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
                    if(mConnectionState==BluetoothProfile.STATE_DISCONNECTED || mConnectionState==BluetoothProfile.STATE_DISCONNECTING){
                        connectedBluethoothDevice.remove(device);
                    }
                }
            }
    }

could anyone help me to fix this problem, thanks.

matdev
  • 4,115
  • 6
  • 35
  • 56
lotfi Raghib
  • 434
  • 1
  • 6
  • 17
  • Do you call close() on your BluetoothGatt objects when you are done with them? – Emil Dec 20 '18 at 20:53
  • no I don't call close(), because if i call close() the Gatt will diconnect from the devices, and i want stay connected to all the devices in the same time – lotfi Raghib Dec 21 '18 at 08:58
  • How many devices do you try to connect to? – Emil Dec 21 '18 at 09:48
  • i try to connect to three devices – lotfi Raghib Dec 21 '18 at 10:31
  • Does logcat print anything interesting? And with closing the BluetoothGatt objects I meant when you are done with them, not before that. Because you can't have more than 32 non-closed BluetoothGatt objects on the system at the same time. So make sure you don't by mistake create more than 1 BluetoothGatt object per device unless you close it. – Emil Dec 21 '18 at 13:55
  • the Logcat shows that the operation failed with status 257 when i call device.connectGatt(this, false, mbluetoothGattCallback), i think it create every time a BluetoothGatt, this is why if failed, because i call connectGatt() function even if the device is already connected, what i want to do is to connect to the first device and read data, then switch to the second device to connect and read data, then wait for a while and redo the same thing and if the device is already connected read just the data with create another BluetoothGatt – lotfi Raghib Dec 21 '18 at 14:23

2 Answers2

0

I also didn't find error code 257 in the documentation, but my observation is that this code means that you are trying to connect to a Bluetooth device too many times in too short time. Reconnecting bluetooth on your phone should resolve this issue.

kanoe
  • 21
  • 3
  • There is an error code with the value 257 -> [BluetoothGatt#GATT_FAILURE](https://developer.android.com/reference/android/bluetooth/BluetoothGatt#GATT_FAILURE) but the description is not very helpful: _A GATT operation failed, errors other than the above_ – Risto Dec 15 '22 at 16:25
0

I found this happened after 58-59 requests when the Gatt client callback is not closed. In my case this was caused by calling not calling close after finishing (even though disconnect was called).

You need to make sure to call close() after you have disconnected the gatt callback. Normally this means calling disconnect, and then calling close in onConnectionStateChange when the disconnect is done. You also need to look out for when the connection itself fails.

As per the documentation - close needs to be called "as early as possible after it is done with this GATT client" https://developer.android.com/reference/android/bluetooth/BluetoothGatt#close()

On your code: I think you need to make sure that you differentiate between success/fail and connect/disconnect. Calling disconnect will result in a callback with status=success and newState=disconnected.

I know this is an old question, but it is the top Google result for error code 257 and there don't seem to be many answers to this.