Bluetooth Bluetooth connection without user Enter PIN and confirmation using Android API

I am starting to program on Android since I just started working 3 months ago. I am making a project that connects an Android application with arduino using bluetooth. I already have the code for the Android application (bluetooth.adapter, sockets, .etc.). The connection code is already working. One of the goals is that the Android application automatically enters a password when connecting to a Bluetooth device without prompting the user to enter a PIN code.

Old posts on this forum do not really help. (many suggested using unsafe mode, but I need safe mode, also in my case, arduino is the server, while the cell phone application is the client, so the createInsecureRfcommSocketToServiceRecord () method does not work for me)

I searched and found this on the android developer site for the bluetoothdevice class:

setPairingConfirmation (logical confirmation) Confirm the password for the connection PAIRING_VARIANT_PASSKEY_CONFIRMATION.

PAIRING_VARIANT_PIN = "The user will be prompted to enter a PIN, or the application will enter a PIN for the user."

PAIRING_VARIANT_PASSKEY_CONFIRMATION = "The user will be asked to confirm the password displayed on the screen, or the application will confirm the access key for the user"

, , " ", android , . - ? !

+4
1

, , API (15 ?)

, (. Roldofo ). .

, ACTION_PAIRING_REQUEST, PIN- .

:

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    getActivity().registerReceiver(mPairingRequestReceiver, filter);

:

private final BroadcastReceiver mPairingRequestReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        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 neeeded
                    device.setPairingConfirmation(true);
            } catch (Exception e) {
                Log.e(TAG, "Error occurs when trying to auto pair");
                e.printStackTrace();
            }
        }
    }
};

(, ), coupleDevice() ( ACTION_PAIRING_REQUEST)

private void pairDevice(BluetoothDevice device) {
    try {
        Log.d(TAG, "Start Pairing... with: " + device.getName());
        device.createBond();
        Log.d(TAG, "Pairing finished.");
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}
+10

Source: https://habr.com/ru/post/1629655/


All Articles