Auto-accept bluetooth pairing possible?

前端 未结 3 771
长情又很酷
长情又很酷 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

    
    
    

    and the broadcastReceiver

     
                    
                        
                        
                    
    
    

提交回复
热议问题