Is there a way that we can know if a BLE device is trying to connect to me before it actually gets connected?

Viewed 87

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:

  1. STATE_CONNECTED
  2. STATE_DISCONNECTED
  3. 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.

1 Answers

For a BLE peripheral's point of view, there is no such state as the remote central is "connecting". There is just disconnected and suddenly connected. On the link layer, there is just one packet the central sends to the peripheral: "hey you're now connected", in response to an advertisement sent by the peripheral.

If you read Android's documentation at https://developer.android.com/reference/android/bluetooth/BluetoothGattServerCallback#onConnectionStateChange(android.bluetooth.BluetoothDevice,%20int,%20int), you will see that CONNECTING is not one of the possible states for this callback.

Related