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

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

问题描述

嘿大家!

前一段时间开始使用蓝牙程序在Android上。但现在我碰到的一些问题。我不知道为什么配对请求有时会显示在通知栏,有时这被跳过,直接所示的对话框。

例如: 我开始我的配对请求从嵌入式设备,然后有一个通知,像这样的:

有时我没有理会通知,我的对话只是显示了,因为我打算它是。

有没有办法赶上该通知,并显示该对话框或这是我的code的一个错误,当我启动蓝牙配对?

修改

更新1:

经过了答案雷诺给了我,它实际上取决于各种各样的事情。有直接示出的对话框的其他手段。当配对请求到达下面的方法被调用。甲检查是为了看看是否应该显示在前景中的对话框(真)或作为通知(假):

 公共布尔shouldShowDialogInForeground(字符串deviceAddress){
    //如果蓝牙设置可见
    如果(mForegroundActivity!= NULL)返回true;

    长的currentTimeMillis = System.currentTimeMillis的();
    共享preferences共享preferences = getShared preferences();

    //如果该设备是可发现模式最近
    长lastDiscoverableEndTime =共享preferences.getLong(
            BluetoothDiscoverableEnabler.SHARED_ preFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP,0);
    如果((lastDiscoverableEndTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
            >的currentTimeMillis){
        返回true;
    }

    //如果设备最近被发现
    如果(mAdapter =空&安培;!&安培; mAdapter.isDiscovering()){
        返回true;
    }否则,如果((共享preferences.getLong(SHARED_ preFERENCES_KEY_DISCOVERING_TIMESTAMP,0)+
            GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)>的currentTimeMillis){
        返回true;
    }

    //如果设备最近被挑中的设备选择器
    如果(deviceAddress!= NULL){
        字符串lastSelectedDevice =共享preferences.getString(
                SHARED_ preFERENCES_KEY_LAST_SELECTED_DEVICE,NULL);

        如果(deviceAddress.equals(lastSelectedDevice)){
            长lastDeviceSelectedTime =共享preferences.getLong(
                    SHARED_ preFERENCES_KEY_LAST_SELECTED_DEVICE_TIME,0);
            如果((lastDeviceSelectedTime + GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND)
                    >的currentTimeMillis){
                返回true;
            }
        }
    }
    返回false;
}
 

这是从源头code代码片段,正如你可以看到,有使对话框显示方式:

  1. 如果该设备是可发现模式最近
  2. 如果该设备是最近发现
  3. 如果该设备最近被挑中的设备选择器
  4. 如果蓝牙设置可见
解决方案

按<一个href="http://www.netmite.com/android/mydroid/2.0/packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothPairingRequest.java">a发表评论我在Android源$ C ​​$ C 看到

  

BluetoothPairingRequest是接收任何蓝牙配对   请求。它检查蓝牙设置,目前可见的,   可以调出密码,密钥或确认输入对话框。   否则,它把一个通知状态栏,可以是   单击以弹出配对输入对话框。

所以是的,根据BT的知名度,在对话框/通知将显示。

 忍者编辑:
 

根据所使用的硬件,这可能会发生变化。

  • 如果该设备是可发现模式最近
  • 如果该设备是最近发现
  • 如果该设备最近被挑中的设备选择器

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:

  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

解决方案

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

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

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