Search code examples
androidsocketsbluetoothpairing

device.createInsecureRfcommSocketToServiceRecord doesn't work properly


I dowloaded the BluetoothChat sample project here : https://android.googlesource.com/platform/development/+/master/samples/BluetoothChat?autodive=0%2F

The fact is, when I'm launching the app on two devices (which are NOT paired), it should connect the two devices WITHOUT asking for pairing the two devices, shouldn't it ? And in fact, when I'm trying to connect the two devices (which are NOT paired), it's asking to pair the devices . I mean, there is this function in BluetoothChatService.java which should create an insecure socket . But it seems, that it doesn't do his job ?

   /**
    * This thread runs while attempting to make an outgoing connection
    * with a device. It runs straight through; the connection either
    * succeeds or fails.
    */
   private class ConnectThread extends Thread {
       private final BluetoothSocket mmSocket;
       private final BluetoothDevice mmDevice;
       private String mSocketType;

       public ConnectThread(BluetoothDevice device, boolean secure) {
           mmDevice = device;
        BluetoothSocket tmp = null;
        mSocketType = secure ? "Secure" : "Insecure";

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            if (secure) {
                tmp = device.createRfcommSocketToServiceRecord(
                        MY_UUID_SECURE);
            } else {
                tmp = device.createInsecureRfcommSocketToServiceRecord(
                        MY_UUID_INSECURE);
            }
        } catch (IOException e) {
            Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
        }
        mmSocket = tmp;
    }

Can somebody explain me why it asks to pair the two devices ? The method createInsecureRfcommSocketToServiceRecord shouldn't ask for pairing unpaired devices , should it ? x) I'm really confused .


Solution

  • createInsecureRfcommSocketToServiceRecord() has of "insecure" the key the devices are using to communicate, which is, below Bluetooth 2.1, not encrypted. That is what is "unsecure".

    But it does not change the fact that if the MAC address is not already in the pairing database there will always be the prompt.

    So yes, it will prompt.