Is it possible to automatically accept a Bluetooth connection?

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

Is there a way to automate a bluetooth pairing request without user intervention? Or is it not possible due to security concerns? I've been looking online for 2 days now and haven't found much, so if anyone knows please post.

Thank!

+1


source to share


3 answers


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



+3


source


So I had this solution in case anyone needs an answer to this while 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 recipient

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

      

+4


source


I faced the same problem, hope the following code helps: 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>

Second, we need a 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);
    }
 }
}

      

+3


source







All Articles