A BLE Tracker Device tries to connect to my BLE Android app. I would like to know about the event when the device tries to connect to me (means before it actually gets connected to me) and in case the device fails to connect to me i would like to know the reason why device could not connect to me.
private BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
Log.d(TAG, "onConnectionStateChange Status: " + status);
// logs += "\n\nonConnectionStateChange Status: " + status;
// updateHomeUI();
if (newState == BluetoothProfile.STATE_CONNECTED) {
connectedDeviceAddress = device.getAddress().toString();
Log.i(TAG, "BluetoothDevice CONNECTED:\n" + device);
FragmentManager fragmentManager = getSupportFragmentManager();
try {
logs += "\n\nBluetoothDevice CONNECTED: \n" + device;
updateHomeUI();
} catch (Exception e) {
e.printStackTrace();
}
mRegisteredDevices.add(device);
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Device CONNECTED: " + connectedDeviceAddress, Toast.LENGTH_SHORT).show();
}
});
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "BluetoothDevice DISCONNECTED: " + device);
//Remove device from any active subscriptions
logs += "\n\nBluetooth Device DISCONNECTED:\n" + device;
updateHomeUI();
mRegisteredDevices.remove(device);
//restarting everything -- test
// bluetoothAdapter.disable();
// bluetoothAdapter.enable();
// startAdvertising();
// startServer();
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Device DISCONNECTED", Toast.LENGTH_SHORT).show();
}
});
}
if (newState == BluetoothProfile.STATE_CONNECTING) {
Log.d(TAG, "Connecting...Status: " + status);
logs += "\n\nConnecting...Status: " + status;
updateHomeUI();
}
}
};
Above is the piece of code that i am using. You can see i have implemented the callback method onConnectionStateChange. I am also checking for the following values:
- STATE_CONNECTED
- STATE_DISCONNECTED
- STATE_CONNECTING
Even though i am checking for all these, i realised that my code never gets inside STATE_CONNECTING check, thus i am not able to know when the device tries to connect to me and i cant find a way to know the error if in case the device fails to connect to my app.
Any help would be apprecicated.