【问题标题】:HOw to pair the bluetooth in two devices using NFC如何使用 NFC 在两个设备中配对蓝牙
【发布时间】:2011-12-22 06:39:56
【问题描述】:

我正在尝试配对两个设备

一个是Google 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);
        }
    }
}

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

【问题讨论】:

  • 您好,我需要开发相同的功能。请建议我在设备靠近并开始传输 NDEF 消息时如何配对设备。请回复我。紧急请。

标签: bluetooth nfc


【解决方案1】:

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

【讨论】:

  • 感谢您的回复.. 但我正在使用命令 tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);并且命令的解释说它将继续并绕过所需的加密链接进行连接。
  • 你确定吗?我的答案是从在线文档的解释中复制的。请再检查一遍。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-26
  • 1970-01-01
  • 2023-03-19
  • 2013-05-31
  • 1970-01-01
  • 2014-07-14
  • 1970-01-01
相关资源
最近更新 更多