我有一个简单的服务配对蓝牙设备,它看起来像这样:
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();
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好,除了有时会弹出对对话框,有时它会显示在我的通知栏中,我必须手动打开它.有没有办法确保它总是弹到前面?
我试图谷歌上面,我唯一能找到的是,如果你留在蓝牙设置,它总是弹出,但这似乎是一个丑陋的解决方案.所有这一切的原因是我正在使用自动化,并希望确保当我运行我的服务时,我得到对对话只需单击"配对".
我有同样的问题.我发现这篇文章解释了对话框何时显示:通知栏上的蓝牙配对请求?
恢复,它取决于shouldShowDialogInForeground()方法的结果.
从帖子引用:
... 有方法可以显示对话框:
- 如果设备最近处于可发现模式
- 如果该设备最近发现
- 如果最近在设备选择器中拾取了设备
- 如果可以看到蓝牙设置
在我的情况下强制对话框出现,我开始并取消发现之后尝试配对...
代码/哈克
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
Run Code Online (Sandbox Code Playgroud)
PS:我知道这是一个可怕的黑客,但这是我开始工作的唯一方式,配对必须只对设备进行一次,所以它不是那么可怕......而且,如果有人找到一个更好的方式,我愿意建议
| 归档时间: |
|
| 查看次数: |
2508 次 |
| 最近记录: |