5

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

MilleB
  • 1,470
  • 2
  • 19
  • 32

2 Answers2

8

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

Community
  • 1
  • 1
Alex Mantaut
  • 3,657
  • 3
  • 35
  • 45
  • Did you run into issues when you tried it with shorter scan durations? – Philipp E. Oct 14 '16 at 11:52
  • 1
    In some cases when I shortened the scan duration the hack didn't work (when the sleep time was around half a second the hack failed some times), I think that it was because the Android OS scheduled the scan but didn't manage to scan... – Alex Mantaut Oct 17 '16 at 07:32
  • 1
    Something better could be implemented maybe catching the discovery intent or something, but I think it would be overkill for this problem (specially since the pairing dialog needs to be displayed only once) – Alex Mantaut Oct 17 '16 at 07:33
  • I have same issue. – Jeffrey Liu Jun 21 '17 at 00:05
  • Which issue exactly? Did you try starting a discovery to force the dialog to the front? – Alex Mantaut Jun 21 '17 at 00:06
2

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();
Alonso
  • 63
  • 6