Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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".

like image 455
MilleB Avatar asked Oct 01 '14 14:10

MilleB


People also ask

Where is my Bluetooth setting menu?

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.

How do you get something to show up on Bluetooth?

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.

What is pairing mode?

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.


2 Answers

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:

  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

like image 73
Alex Mantaut Avatar answered Sep 22 '22 19:09

Alex Mantaut


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();
like image 23
Alonso Avatar answered Sep 21 '22 19:09

Alonso