Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 蓝牙只能单向工作_Java_Android - Fatal编程技术网

Java 蓝牙只能单向工作

Java 蓝牙只能单向工作,java,android,Java,Android,我对android和java非常陌生,所以要温柔:) 我正试图通过布洛托斯连接两部电话。 我通过创建serversocket,然后从一部手机(作为客户端)初始化连接,使两部手机都能监听来电。有趣的是,当我试图让我的LG(安卓2.3.4版)连接HTC(安卓2.2.1版)时,一切都很好,但当我试图让HTC手机作为客户端连接时,我没有得到任何结果。调试器显示HTC在mmSocket.connect()上失败;并执行catch(IOException-connectException)。我的代码基本上是

我对android和java非常陌生,所以要温柔:) 我正试图通过布洛托斯连接两部电话。 我通过创建serversocket,然后从一部手机(作为客户端)初始化连接,使两部手机都能监听来电。有趣的是,当我试图让我的LG(安卓2.3.4版)连接HTC(安卓2.2.1版)时,一切都很好,但当我试图让HTC手机作为客户端连接时,我没有得到任何结果。调试器显示HTC在mmSocket.connect()上失败;并执行catch(IOException-connectException)。我的代码基本上是从android蓝牙教程复制/粘贴的。有什么建议可以解释为什么手机会有不同的行为吗?连接线程:

    private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;
        mmDevice = device;
        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) { }
        mmSocket = tmp;
    }

    public void run() {
        // Cancel discovery because it will slow down the connection
        BtAdapter.cancelDiscovery();
        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect(); **HTC phones fails here and goes to CATCH block**
            // make info message
            Message msg = mainHandler.obtainMessage();
            Bundle bundle = new Bundle();
            String btnTxt = "Connected";
            bundle.putString("myKey", btnTxt);
            msg.setData(bundle);
            mainHandler.sendMessage(msg);
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) { }
            return;
        }
        // Do work to manage the connection (in a separate thread)
        ConnectedThread conThread = new ConnectedThread(mmSocket);
        conThread.start();
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}

可能有问题

tmp=device.createrFComSocketToServiceRecord(我的UUID)

在您的HTC 2.21设备上。检查HTC设备的Bluetoth适配器支持哪些功能,这些功能可能与您的程序失败的原因有关

“仅当可以使用经过身份验证的套接字链接时才使用此套接字。 身份验证是指对链接密钥进行身份验证以防止 中间人攻击类型。例如,蓝牙2.1 设备,如果任何设备没有输入和输出 功能或仅能够显示数字键,安全 套接字连接不可能。在这种情况下,请使用{#link CreateInsurerFComSocketToServiceRecord}。有关更多详细信息,请参阅 蓝牙核心规范第5.2节(第3卷)中的安全模型 版本2.1+EDR。“