49

I have an app where I am programmatically controlling Bluetooth pairing and unpairing. I can pair before connection and unpair afterwards. The reason I need to do this is specific to my application and not in the scope of my question.

Basically what I am doing is:

  1. Get a reference ib to IBluetooth object as described in this answer
  2. Register a BroadcastReceiver for android.bluetooth.device.action.PAIRING_REQUEST
  3. Call ib.createBond(address)
  4. Wait for BroadcastReceiver to trigger
  5. Convert user pin into bytes with convertPinToBytes()
  6. Call ib.setPin(address, pinBytes) from within BroadcastReceiver

Anyways, this approach works great, except for the fact that when I do the pairing, I get a notification in the Status bar requesting that the user enter a PIN to complete the pairing. But this is in fact unnecessary, because by the time the user sees this, my app has already used setPin(). I'd really like for that notification to either a) not appear at all, or b) be dismissed automatically somehow.

I realize this may not even be possible, but I thought I would ask in case someone has a creative idea.

Community
  • 1
  • 1
Joel F
  • 2,591
  • 1
  • 19
  • 17
  • 1
    Can you please add some sample code (or a link to your source code). What I want to do is actually the same as you. I want to connect to a device without the password request. Kind regards, Jeroen – Jeroen Oct 03 '11 at 13:39
  • 1
    I can't share my source code unfortunately, but the step-by-step that I have above is very close to the actual code. You can get started with the source code in the answer I link to in Step 1. – Joel F Oct 10 '11 at 20:55
  • @JoelF have you solved your problem? Because I'm not. Really appreciate if you can share how you solve, if that's the case. – Tiago Dec 22 '11 at 16:08
  • 1
    Sorry, I changed jobs and don't work on this anymore. We ended up just living with the pop-up on the first connection and not unpairing afterwards. Subsequent connections do not give the pop-up because the pairing is now in place. – Joel F Apr 11 '12 at 13:20
  • this still works in 4.2.1? Cause they worked on BT and changed things lately.. – Ewoks Dec 19 '12 at 21:34
  • @Ewoks Sorry, this was last tested on 2.3. I never tried anything later. – Joel F Jan 08 '13 at 14:50
  • seems like Google changed something in Bluez starting with 4.1 .. – Ewoks Jan 11 '13 at 13:14

3 Answers3

20

Try setting the confirmation first in the PAIRING_REQUEST

BluetoothDevice device = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");

device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
device.getClass().getMethod("cancelPairingUserInput").invoke(device);

This worked for me between two Android devices using RFCOMM but I'm not entering any PINs

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
dimpey
  • 201
  • 1
  • 3
  • 1
    As a side note, calling cancelPairingUserInput has not been necessary in my tests. – domsom Dec 18 '12 at 17:32
  • 1
    This does not work. If i use this code the pairing process does not start and i get an error message that there is no such method called 'cancelPairingUserInput' – Mulgard Feb 06 '15 at 18:32
  • I just noticed that this also works to prevent annoying pairing request pop-ups when connecting to already paired devices on certain models running recent versions of Android (GT-I9295, Android 4.4.2) – dhakim Mar 12 '15 at 00:29
14

Since Android API 19 Google switched these Methods to public Methods, so there is no need for Reflection any more. :)

Erik Mueller
  • 330
  • 4
  • 10
1

Do this in the PAIRING_REQUEST notification event:

BluetoothDevice localBluetoothDevice = (BluetoothDevice)intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");

Class localClass = localBluetoothDevice.getClass();
Class[] arrayOfClass = new Class[0];
localClass.getMethod("cancelPairingUserInput", arrayOfClass).invoke(paramBluetoothDevice, null)).booleanValue();

But you gotta tell me how did you pair your remote device without the user to enter Passkey/PIN? off course, you know the PIN for the remote device which is trying to pair to your device but how did you provide that PIN to the remote device.

Joel F
  • 2,591
  • 1
  • 19
  • 17
ghost
  • 31
  • 4
  • Interesting, let me give that a try. I've updated my answer to be a little more specific about how I did the programmatic pairing without user interaction. Hope that helps. – Joel F Sep 09 '11 at 12:31
  • Can you explain what is the paramBluetoothDevice exactly? – Tiago Dec 22 '11 at 16:12