Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BLE Gatt onConnectionStateChange failed, Status 133 and 257

I'm trying to connect my Beacons to the Gattservice. In the Callback onConnectionStateChange, It's always failing and im getting the

statuscodes 133 and 257.

Somehwere was written that 133 stands for to many connections. In my code there are lines with gatt.disconnect(). I don't know how to fix it, because all other gattexamples are the same. I'm working with the Android 6.0.1 Version and the API 23, if it's important to find the error. Here's my code:

public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        if(status == BluetoothGatt.GATT_SUCCESS) {
            switch (newState) {
                case BluetoothProfile.STATE_CONNECTED:
                    mBleDevices.add(gatt.getDevice());
                    Log.i("Beacons", "STATE_CONNECTED");

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBluetoothGatt.discoverServices();
                        }
                    });
                    break;
                case BluetoothProfile.STATE_DISCONNECTED:
                    Log.e("Beacons", "STATE_DISCONNECTED");
                    mBleDevices.remove(gatt.getDevice());
                    mBluetoothGatt.disconnect();
                    mBluetoothGatt = null;
                    break;
                default:
                    Log.e("Beacons", "STATE_OTHER");
            }
        } else {
            Log.e("Beacons", "ERROR WITH CONNECTING " + status);
            mBleDevices.remove(gatt.getDevice());
        }
 }

My ScanCallback looks like this:

 public void onScanResult(int callbackType, ScanResult result) {
    runOnUiThread(new Runnable() {
       @Override
       public void run() {
          BluetoothDevice btDevice = result.getDevice();
          connectToDevice(btDevice);
       }
    });
 }

And starting connection like this:

 runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mBluetoothGatt = btDevice.connectGatt(getApplicationContext(), true, connectCallback);
        }
    });

The connectCallback causes then onConnectionStateChange function. Thank you for your help!

like image 405
Sunny Avatar asked Jan 24 '26 07:01

Sunny


1 Answers

I had to specify the transport parameter in the call to fix this. It's an optional 4th paramater to specify it's a BLE device.

mBluetoothGatt = device.connectGatt(this, false, mGattCallback, 2);

https://developer.android.com/reference/android/bluetooth/BluetoothDevice#connectGatt(android.content.Context,%20boolean,%20android.bluetooth.BluetoothGattCallback,%20int)

like image 146
falon89 Avatar answered Jan 25 '26 21:01

falon89



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!