Summary of Bluetooth Development Experience - Automatic Pairing

Keywords: Android Mac Mobile

Scene: Control side - ordinary mobile phone; Controlled side - XX device (no screen, no user operation, system privileges)

There are two ways to find a device when Bluetooth is on: search and specify a mac.Searching for Bluetooth devices with the search function involves the concept of "visibility". Bluetooth visibility (that is, it can be searched by other Bluetooth devices) can be set to a maximum of 300 s (that is, 5 minutes), whereas there is no time limit for specifying a mac. A device can be found as long as Bluetooth is on.Because the XX device and the control end need to be able to connect (pair) at any time, it is obvious that the search and find method is not applicable to XX devices.Therefore, you can only choose how to specify the MAC address.

Pairing is the basis for establishing a connection, and any device that has never been paired (or has been paired but one of them has deleted the pairing) cannot establish a connection, so pairing cannot be bypassed.There are many ways to pair, such as input pin code, input password, random authentication code, etc.The current way of pairing XX devices with the control side is to avoid entering a random verification code. Confirm that the 6-digit password is the same on both sides. Click Confirm Pairing to complete the pairing.However, since XX devices cannot interact with users, automatic pairing can only be achieved by program completion confirmation - of course, XX devices only agree to and accept pairing by default for requests to determine the source (such as XX Assistant).

Implementation of automatic pairing (requires system privileges):
Permission Statement:

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

Registered broadcasts:

IntentFilter filter = new IntentFilter();
filter.setPriority(1000);//Be careful to set priority, otherwise you can't intercept
context.registerReceiver(mReceiver, filter);

Check pairing by consent:

case BluetoothDevice.ACTION_PAIRING_REQUEST:
    BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    if (btDevice.getName().contains("You agree to the name of the Bluetooth pairing")) {
        Log.e("here", "OKOKOK");
        try {
            //1.Confirm Pairing
            ClsUtils.setPairingConfirmation(btDevice.getClass(), btDevice, true);
            //2.Terminate Ordered Broadcasting
            Log.i("order...", "isOrderedBroadcast:" + isOrderedBroadcast() + ",isInitialStickyBroadcast:" + isInitialStickyBroadcast());
            abortBroadcast();//If the broadcast is not terminated, a blinking pairing box appears.
            // setPin Method Pairing...
            //boolean ret = ClsUtils.setPin(btDevice.getClass(), btDevice, pin);
        } catch (Exception e) {
            // TODO Auto-generated catch block
    } else {
        Log.e("Prompt message", "This device is not a target Bluetooth device");

Note, there are templates for ClsUtils, so you can search a lot of them on the Internet, so I won't repeat them here.

Posted by grga on Tue, 14 Jul 2020 08:32:35 -0700