5

Also, If I get a notification, is there a way to click it programmatically and bring the pairing request to to front?

Martin
  • 171
  • 2
  • 12
  • Not sure if I got the question right but if it's about automatic pairing you could check the following link (device.createInsecureRfcommSocketToServiceRecord worked for me): http://stackoverflow.com/questions/7360534/accept-pairing-request-programatically-android-bluetooth/7361152#7361152 – Andrew Terekhine Sep 08 '15 at 22:40
  • I am actually looking for manually entering the key and not doing it programatically. I just want to make sure I get the key in dialog every time i have pairing request. – Martin Sep 08 '15 at 22:43
  • 2 and a half years ago and still no one knows. I would like to do the same thing. SOMEtimes Android places the dialog in the foreground and sometimes it comes as a notification. Depends upon platform, too. – Brian Reinhold Mar 15 '18 at 14:35
  • Martin, I know EXACTLY what you want and I would like to be able to do that too! – Brian Reinhold Mar 15 '18 at 14:38

2 Answers2

2

for reference to explain why and what, please have a look here: Bluetooth pairing request on notification bar?

The solution is quite easy if you know it and if it fits into your application:

private void feintBluetoothDeviceDiscovery() {
    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
    btAdapter.startDiscovery();
    btAdapter.cancelDiscovery();
}

Call feintBluetoothDeviceDiscovery() before you try to pair or connect your bluetooth device. The popup should appear in the front.

We also had this issue in our automated tests. A pairing request only showing as notifications are a pain there. Thank to a colleague for sharing the code.

maze
  • 789
  • 1
  • 7
  • 31
0

Use uuid cache followed by uuid in the following order.

private void SOKETHAZIRLA() {

    Log.i("ZZZZZZ", String.valueOf(btDevice.getBondState()));


    Thread connectThread = new Thread(() -> {
        try {
            if (btDevice != null) {

                Log.i("BTDevice", "Cihaz Name: " + btDevice.getName());
                Log.i("BTDevice", "Cihaz UUID: " + btDevice.getUuids()[0].getUuid());
                mbtSocket = btDevice.createRfcommSocketToServiceRecord(btDevice.getUuids()[0].getUuid());
                Log.d("BTDevice", "1. connect.");
                mbtSocket.connect();

            } else {
                Log.d("BTDevice", "Device is null.");
            }
        } catch (Exception ex) {
            try {

                mBluetoothAdapter.startDiscovery();

                if (btDevice != null) {
                    mbtSocket = btDevice.createRfcommSocketToServiceRecord(uuid);
                    Log.d("BTDevice", "2. connect.");
                    mbtSocket.connect();
                }

            } catch (IOException e) {
                e.printStackTrace();
                try {
                    mbtSocket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

                runOnUiThread(socketErrorRunnable);
                mbtSocket = null;
            }
        } finally {
            runOnUiThread(this::finish);
        }
    });
    connectThread.start();

}
mbarut
  • 1