简体   繁体   English

将蓝牙配对对话带到前面

[英]Bring bluetooth pairing dialogue to the front

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? 有没有办法确保它总是弹到前面?

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? 我发现这篇文章解释了对话框何时显示: 通知栏上的蓝牙配对请求?

Resuming, it depends on the result of the shouldShowDialogInForeground() method. 恢复,它取决于shouldShowDialogInForeground()方法的结果。

Quoting from the post: 从帖子引用:

... 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 如果可以看到蓝牙设置

In my case to force the dialog to appear, I started and canceled a discovery before trying to pair... 在我的情况下强制对话框出现,我开始并取消发现之后尝试配对...

Code/Hack 代码/哈克

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 :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 PS :我知道这是一个可怕的黑客,但这是我开始工作的唯一方式,配对必须只对设备进行一次,所以它不是那么可怕......而且,如果有人找到一个更好的方式,我愿意建议

I use following code to resolve the issue 我使用以下代码来解决此问题

if(!BluetoothAdapter.getDefaultAdapter().isDiscovering())
    BluetoothAdapter.getDefaultAdapter().startDiscovery();
//make sure that the device is in discovering
while (!BluetoothAdapter.getDefaultAdapter().isDiscovering());
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM