Auto-accept bluetooth pairing possible?

前端 未结 3 767
长情又很酷
长情又很酷 2020-12-06 03:39

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

相关标签:
3条回答
  • 2020-12-06 04:28

    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>
    
    0 讨论(0)
  • 2020-12-06 04:29

    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.

    0 讨论(0)
  • 2020-12-06 04:36

    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);
        }
     }
    }
    
    0 讨论(0)
提交回复
热议问题