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:
And sometimes I don't have to bother with the notification, my dialog just shows up as I intended it to be.
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:
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.