请使用NFC将两个设备中的蓝牙配对

时间:2011-12-22 06:39:56

标签: bluetooth nfc

我正在尝试配对两个设备

其中一个是谷歌Nexus S,其运行的应用程序读取写在标签上的mac地址,该地址卡在另一部不是Android的手机上。

现在,当我点击另一部手机时,我正试图配对设备,我的应用程序会读取存储在TAG中的MAC地址并自动创建蓝牙连接。

一切都运转良好,但我收到配对请求或匹配两部不应该来的手机上的按键。

以下是发生连接的蓝牙段

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

我正在尝试不安全的连接,但我不知道为什么它要求两部手机中的配对键。

1 个答案:

答案 0 :(得分:1)

谢谢你的蓝牙版本因为: (原文如此)“对于蓝牙2.1设备,链接密钥将被加密,因为加密强制。对于旧设备(蓝牙2.1设备之前),链接密钥将不会被加密。”