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".
Swipe down from the top of the screen. Touch and hold Bluetooth . If your accessory is listed under "Available media devices," next to your device's name, tap Settings . If no accessories are listed under "Previously connected devices," tap See all.
On your phone, open the Settings app and tap Bluetooth (or Settings > Connections > Bluetooth). Make sure Bluetooth is turned on (the button should be blue). Check your Bluetooth device and make sure it's turned on and in discovery mode. Wait for it to show up under Available Devices on your phone.
Establishing a connection between two Bluetooth devices. For example, to pair a headset with a phone, the phone is configured to "Discoverable" mode and the headset is set up to pair by pressing one or more keys for some number of seconds.
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.
Quoting from the post:
... there are ways of making the dialog show:
- If the device was in discoverable mode recently
- If the device was discovering recently
- If the device was picked in the device picker recently
- 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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With