将蓝牙配对对话框置于最前面 [英] Bring bluetooth pairing dialogue to the front

查看:137
本文介绍了将蓝牙配对对话框置于最前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配对蓝牙设备的简单服务,它看起来像这样:

I've a simple service to pair bluetooth devices and it look like this:

protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    if(!extras.containsKey("bluetoothAddress"))
        return;
    String bluetoothAddress = extras.getString("bluetoothAddress");
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if(!adapter.isEnabled()) {
        adapter.enable();
    }
    BluetoothDevice device = adapter.getRemoteDevice(bluetoothAddress);
    device.createBond();
}

它工作得很好,除了有时会弹出配对对话框,有时它会显示在我的通知栏中,而我必须手动打开它.有什么方法可以确保它始终弹出到最前面吗?

It works perfectly fine except that sometimes the pair dialogue pop up and sometimes it show up in my notifications bar and I have to open it manually. Is there any way to make sure that it always pop up to the front?

我已经尝试过在Google上进行搜索,唯一能找到的就是,如果您一直处于蓝牙设置中,它总是会弹出,但这似乎是一个丑陋的解决方案.所有这些的原因是我正在使用自动化,并希望确保在运行服务时得到的配对对话框可以单击配对".

I've tried to google on it and only thing I can find is that if you stay in bluetooth settings it always pop up, but that seems like a ugly solution. The reason for all of this is that I'm working with automation and want to make sure that when I run my service I get the pair dialogue can just click "Pair".

推荐答案

我遇到了同样的问题.我发现这篇文章解释了是否显示对话框:通知栏?

I had the same problem. I've found this post that explains when the dialog is shown or not: Bluetooth pairing request on notification bar?

继续,这取决于shouldShowDialogInForeground()方法的结果.

Resuming, it depends on the result of the shouldShowDialogInForeground() method.

从帖子中引用:

... 有使对话框显示的方法:

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

在我的情况下,为了强制显示对话框,我开始尝试取消配对并取消了发现.

In my case to force the dialog to appear, I started and canceled a discovery before trying to pair...

代码/密码

BluetoothAdapter.getDefaultAdapter().startDiscovery();
//Give it some time before cancelling the discovery
Thread.sleep(1000);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
//Then do the LeScan and connect to the device

PS :我知道这是一个可怕的骇客,但这是我能做到这一点的唯一方法,并且配对设备必须只进行一次,所以并不那么糟糕...此外,如果任何人都可以找到一种更好的方式让我接受建议

PS:I know it's a horrible hack but is the only way I got this to work, and the pairing must be done only once for device so it's not so terrible... Also, if anybody finds a better way I'm open to suggestions

这篇关于将蓝牙配对对话框置于最前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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