Hey everyone!
在Android之前开始使用蓝牙编程 . 但现在我遇到了一些问题 . 我想知道为什么配对请求有时会出现在通知栏中,有时会跳过此对话框并直接显示对话框 .
例如:我从嵌入式设备发起配对请求,然后会出现如下通知:
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:
检查了Reno给我的答案,这实际上取决于各种各样的事情 . 还有其他方法可以直接显示对话框 . 当配对请求到达时,调用以下方法 . 进行检查以查看对话框是应该显示在前台(true)还是显示为通知(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 回答
根据a comment I saw in the android source code
所以,是的,根据BT的可见性,将显示对话框/通知 .
这可能因使用的硬件而异 .
如果设备最近处于可发现模式
如果设备最近发现了
如果最近在设备选择器中选择了设备