Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth pairing request on notification bar?

Hey everyone!

Started programming with Bluetooth on Android awhile ago. But now I've run into some issues. I'm wondering why the pairing request sometimes shows up in the notification bar and sometimes this is skipped and the dialog is shown directly.

For example: I initiate my pairing request from an embedded device and then there's a notification such as this one:

In english: Pairing request for Bluetooth

And sometimes I don't have to bother with the notification, my dialog just shows up as I intended it to be.

Pairing dialog shows up and there was no notification on the status bar

Is there way to catch that notification and display the dialog or is this a bug in my code when I initiate bluetooth pairing?

EDIT:

UPDATE 1:

Checked out the answer Reno gave me and it actually depends on a variety of things. There are other means of showing the dialog directly. The following method is called when the pairing request arrives. A check is done in order to see if the dialog should be displayed in the foreground (true) or as a notification (false):

public boolean shouldShowDialogInForeground(String deviceAddress) {
    // If Bluetooth Settings is visible
    if (mForegroundActivity != null) return true;

    long currentTimeMillis = System.currentTimeMillis();
    SharedPreferences sharedPreferences = getSharedPreferences();

    // If the device was in discoverABLE mode recently
    long lastDiscoverableEndTime = sharedPreferences.getLong(
            BluetoothDiscoverableEnabler.SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, 0);
    if ((lastDiscoverableEndTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
            > currentTimeMillis) {
        return true;
    }

    // If the device was discoverING recently
    if (mAdapter != null && mAdapter.isDiscovering()) {
        return true;
    } else if ((sharedPreferences.getLong(SHARED_PREFERENCES_KEY_DISCOVERING_TIMESTAMP, 0) +
            GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND) > currentTimeMillis) {
        return true;
    }

    // If the device was picked in the device picker recently
    if (deviceAddress != null) {
        String lastSelectedDevice = sharedPreferences.getString(
                SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE, null);

        if (deviceAddress.equals(lastSelectedDevice)) {
            long lastDeviceSelectedTime = sharedPreferences.getLong(
                    SHARED_PREFERENCES_KEY_LAST_SELECTED_DEVICE_TIME, 0);
            if ((lastDeviceSelectedTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
                    > currentTimeMillis) {
                return true;
            }
        }
    }
    return false;
}

This is a snippet from the source code and as you can see that there are ways of making the dialog show:

  1. If the device was in discoverable mode recently
  2. If the device was discovering recently
  3. If the device was picked in the device picker recently
  4. If Bluetooth Settings is visible
like image 652
Mazze Avatar asked Jul 25 '11 14:07

Mazze


People also ask

Why do I keep getting a Bluetooth pairing request?

But why do you keep receiving Bluetooth pairing requests so often? Well, when a device with Bluetooth on is in close proximity and it detects your device, you receive those maddening request notifications.

How do I get rid of Bluetooth pairing request?

Bluetooth > Advanced > Block pairing requests. Try to turn off Bluetooth when it's not in use. Once the device's Bluetooth has been turned on, other device can request to pair that's how the communication and the protocol works.


1 Answers

As per a comment I saw in the android source code

BluetoothPairingRequest is a receiver for any Bluetooth pairing request. It checks if the Bluetooth Settings is currently visible and brings up the PIN, the passkey or a confirmation entry dialog. Otherwise it puts a Notification in the status bar, which can be clicked to bring up the Pairing entry dialog.

So yeah, depending on the BT visibility, the dialog/notification will be shown.

ninja edit: 

This may vary depending on the hardware used.

  • If the device was in discoverABLE mode recently
  • If the device was discoverING recently
  • If the device was picked in the device picker recently
like image 179
Reno Avatar answered Oct 19 '22 02:10

Reno