Bluetooth Pairing

So my Khadas running Android is a silent device. What I want is that when I send a pairing request to khadas from a different device, Khadas automatically pairs. The problem is when a bluetooth pairing request is sent, a pairing confirmation prompt with pin comes up. I have setup a Broadcast Receiver by googling what can I do, but nothing seems to work the code is : >

private final BroadcastReceiver mPairingRequestReceiver = new BroadcastReceiver()
    {
        public void onReceive(Context context, Intent intent)
        {
            Log.v("BDE", "recieved something!");
            String action = intent.getAction();
            if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST))
            {
                try
                {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int pin = intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 1234);
                    //the pin in case you need to accept for an specific pin
                    Log.d(TAG, "Start Auto Pairing. PIN = " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 1234));
                    byte[] pinBytes;
                    pinBytes = ("" + pin).getBytes("UTF-8");
                    device.setPin(pinBytes);
                    //setPairing confirmation if needed
                    device.setPairingConfirmation(true);
                } catch (Exception e)
                {
                    e.printStackTrace();
                    Log.e(TAG, "Error occurs when trying to auto pair");
                    e.printStackTrace();
                }
            }
        }
    };

When I run this code, I get a error “Caused by: java.lang.SecurityException: Need BLUETOOTH PRIVILEGED permission: Neither user 10068 nor current process has android.permission.BLUETOOTH_PRIVILEGED.”

I have added the permission in Android Manifest too and have installed the app as system app too, still it does not work.

Found the Solution, if you have root access and you are installing the app as system app to get the bluetooth_privileged permission. Copy the apk into /system/priv-app instead of /system/app

2 Likes

@chetu97 Did you find the way to have proper debugging experience without this workaround ?