user1111176
user1111176

Reputation: 49

HOw to pair the bluetooth in two devices using NFC

I am trying to pair two devices

One is Google Nexus S which has the applicaion running which reads the mac address written over the tag which is stuck on the other phone which is not Android one .

Now i am trying to pair the devices as i tap on the other phone , my application reads the MAC address stored in the TAG and automatically creates the Bluetooth connection .

Everything is working fine but i am getting a pairing request or to match the keys on both the phones which should not come.

Here is the Bluetooth Segment where the connection is happening

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);

                Log.d("CHECK", "Sucessfully created insecure socket");
            }
        } catch (IOException e) {
            Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
        }

        mmSocket = tmp;

    }

    public void run() {
        Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
        setName("ConnectThread" + mSocketType);

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        Log.d("CHECK", "Inside RUN");

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception

            Log.d("CHECK", "Trying to connect");

            mmSocket.connect();

            Log.d("CHECK", "Tried to connect");

        } catch (IOException e) {
            // Close the socket
            try {
                mmSocket.close();

                Log.d("CHECK", "Socket closed");

            } catch (IOException e2) {
                Log.e(TAG, "unable to close() " + mSocketType +
                        " socket during connection failure", e2);
            }

            Log.d("CHECK", "Connection failed");

            connectionFailed();
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (BluetoothChatService.this) {
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice, mSocketType);

        Log.d("CHECK RESULT", "Sucessfully connected");

       // Toast.makeText(BluetoothChatService.ConnectThread.this, "Sucessfully connected" , Toast.LENGTH_LONG).show();
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect " + mSocketType + " socket failed", e);
        }
    }
}

I am trying for the insecure connection , but i dont know why it is asking for the pairing keys in both the phones.

Upvotes: 2

Views: 3173

Answers (1)

jlvaquero
jlvaquero

Reputation: 8785

Chek your bluetooth version because: (sic) "For Bluetooth 2.1 devices, the link key will be encrypted, as encryption is mandatory. For legacy devices (pre Bluetooth 2.1 devices) the link key will be not be encrypted."

Upvotes: 1

Related Questions