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!
On some Android devices, I see a 133 error on connect if the Android phone has not been paired (bonded) with the peripheral before. If I go to Settings -> Bluetooth at the same time as the app is trying to connect in the background, the connection works. (Strangely, you do not need to do anything on the screen, it just needs to be up.) Once this succeeds the first time, the error is gone forever.
I'm guessing this is a security restriction, but I have not figured the proper way to elegantly address it.