3

In the Android 2.3.3 BluetoothChat example with with createInsecureRfcommSocketToServiceRecord() API, users are still prompted to accept the pairing request, even though no PIN code is presented.

Is there a way to automate Bluetooth pairing request without user intervention? Or is this never possible due to security concerns? I have been looking online for 2 days now and haven't really found much, so if anybody knows, please post.

Thanks!

anticafe
  • 6,816
  • 9
  • 43
  • 74
Lily
  • 197
  • 2
  • 2
  • 10

3 Answers3

6

So, I had this cuestion, if some one needs the answer to this working in android 4.4.2

 IntentFilter filter = new IntentFilter(
                "android.bluetooth.device.action.PAIRING_REQUEST");


        /*
         * Registering a new BTBroadcast receiver from the Main Activity context
         * with pairing request event
         */
        registerReceiver(
                new PairingRequest(), filter);

and the code for the Receiver

  public static class PairingRequest extends BroadcastReceiver {
        public PairingRequest() {
            super();
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
                try {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0);
                    //the pin in case you need to accept for an specific pin
                    Log.d("PIN", " " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0));
                    //maybe you look for a name or address
                    Log.d("Bonded", device.getName());
                    byte[] pinBytes;
                    pinBytes = (""+pin).getBytes("UTF-8");
                    device.setPin(pinBytes);
                    //setPairing confirmation if neeeded
                    device.setPairingConfirmation(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

and in the manifest file

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

and the broadcastReceiver

 <receiver android:name=".MainActivity$PairingRequest">
                <intent-filter>
                    <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
                    <action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
                </intent-filter>
</receiver>
Rodolfo Abarca
  • 565
  • 7
  • 15
3

Not with the standard API, no: if the MAC address is not already in the pairing database there will always be the prompt. I'm told that if you have a device that has been rooted and have public read/write access to the bluetooth service's DBus endpoint you can work around that but I've never seen that actually implemented.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Thanks! That explains a lot. I even looked at the source of BluetoothAdapter.java class to see if there's any unofficial APIs I can leverage. Is it then possible to add MAC address into the pairing database ahead of time to allow pairing to occur automatically when the devices are in range? – Lily Sep 24 '11 at 17:36
  • Not that I'm aware of: that's a BlueZ level thing, so you might have some more luck looking in the android BlueZ git. – Femi Sep 25 '11 at 03:28
3

i came across the same problem, i hope the following code will help: firsly we need:

<receiver android:name=".broadcast.PairingRequest"> <intent-filter> <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> <action android:name="android.bluetooth.device.action.PAIRING_CANCEL" /> </intent-filter></receiver>

secondly we need the BluetoothDevice class, and:

public class PairingRequest extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals("ACTION_PAIRING_REQUEST")) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
        device.setPin(pinBytes);
    }
 }
}
  • What version of Android are you using?? – MQS Nov 02 '11 at 22:44
  • Check this http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/bluetooth/BluetoothDevice.java – Tiago Dec 22 '11 at 15:39
  • Despite the grepcode, these two methods do not appear exist in standard Android distributions, nor in [the BluetoothDevice javadoc](http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html) While it would certainly be nice, it's not there. – idarwin Jul 25 '12 at 17:46
  • This code is extremely helpful. Just use a little of reflection and beware of hidden string constants and you will be fine. There is definitely possible to inject PIN and connect but the target device remains unpaired. OK for me... I can provide a full source code used in my own project. – OGP Feb 08 '13 at 17:26
  • Another problem (sort of) found. When using the above algorithm the first time an Android device connects to a target it does connect but still shows the PIN Dialog. Pressing leaves the connection intact! The target is never paired. Next time the device connects it never shows the dialog again. Either some kind of a race or Android intimate internals problem. Verified with 2.3.5 and 4.1.2 Androids. – OGP Feb 10 '13 at 14:11