首页 文章

通知栏上的蓝牙配对请求?

提问于
浏览
12

Hey everyone!

在Android之前开始使用蓝牙编程 . 但现在我遇到了一些问题 . 我想知道为什么配对请求有时会出现在通知栏中,有时会跳过此对话框并直接显示对话框 .

例如:我从嵌入式设备发起配对请求,然后会出现如下通知:

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:

检查了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 回答

  • 9

    根据a comment I saw in the android source code

    BluetoothPairingRequest是任何蓝牙配对请求的接收器 . 它会检查蓝牙设置当前是否可见,并显示PIN,密码或确认输入对话框 . 否则,它会在状态栏中放置一个通知,可以单击该状态栏以显示“配对”条目对话框 .

    所以,是的,根据BT的可见性,将显示对话框/通知 .

    ninja edit:
    

    这可能因使用的硬件而异 .

    • 如果设备最近处于可发现模式

    • 如果设备最近发现了

    • 如果最近在设备选择器中选择了设备

相关问题