通知栏上的蓝牙配对请求? [英] Bluetooth pairing request on notification bar?

查看:22
本文介绍了通知栏上的蓝牙配对请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

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

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:

有时我不必理会通知,我的对话框会按照我的预期显示.

有没有办法捕获该通知并显示对话框,或者这是我启动蓝牙配对时代码中的错误?

更新 1:

查看 Reno 给我的答案,这实际上取决于多种情况.还有其他方法可以直接显示对话框.当配对请求到达时调用以下方法.进行检查以查看对话框是否应显示在前台 (true) 或作为通知 (false):

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;
}

这是源代码的一个片段,正如您所见,有多种方法可以显示对话框:

  1. 如果设备最近处于可发现模式
  2. 如果设备最近正在发现
  3. 如果最近在设备选择器中选择了该设备
  4. 如果蓝牙设置可见

推荐答案

根据 我在android源代码中看到的一条评论

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

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.

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

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

ninja edit: 

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

This may vary depending on the hardware used.

  • 如果设备最近处于可发现模式
  • 如果设备最近正在发现
  • 如果最近在设备选择器中选择了该设备

这篇关于通知栏上的蓝牙配对请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆