【问题标题】:Android pairing does not work - "connection reset by peer"Android 配对不起作用 - “对等方重置连接”
【发布时间】:2012-02-21 03:06:52
【问题描述】:

正如标题所解释的,我很难通过蓝牙向我的电脑发送一些数据。我正在尝试与我的 android 手机作为客户端和我的 PC 作为服务器建立连接。当我尝试通过 BluetoothSocket.connect() 实际建立连接时,我的手机会提示输入密码。输入后,我的 PC 也会提示输入相同的引脚,但在我输入之前,connect() - 方法会引发 IOException。我假设 connect() 方法在我可以在我的 PC 上输入正确的 pin 之前超时,但是我怎样才能让它等待足够长的时间让我输入 PIN?

编辑:将 PC 与我的手机重新配对后它工作正常,因为配对对话框不再出现在我的应用程序中。如果我取消配对 PC 并启动我的应用程序,配对对话框会弹出,但会在几秒钟后消失,并且套接字会引发异常(“对等方重置连接”)。显然在配对完成之前连接已重置,但为什么呢?

这是我的代码:

private void connectToDevice(BluetoothDevice device)
{
    mBluetoothAdapter.cancelDiscovery();
    BluetoothSocket socket = null;
    try 
    {
        socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-                        0000-  1000-8000-00805F9B34FB"));
    } 
    catch (IOException e) 
    {
        Log.e("HeliRemote", "Couldn't get socket.");
        return;
    }
    try 
    {
        socket.connect();
    } 
    catch (IOException e) 
    {
        try 
        {
            socket.close();
        } 
        catch (IOException e1) 
        {   
            Log.e("HeliRemote", "Couldn't close connection");
        }
        // That's the message I get in LogCat
        Log.e("HeliRemote", "Couldn't connect to Socket.");
        return;
    }

    Log.i("HeliRemote", "connected.");
}

如果有人能就这个问题给我任何好的建议,我会很高兴。

【问题讨论】:

    标签: android bluetooth timeout


    【解决方案1】:
        Method m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
            mBluetoothSocket = (BluetoothSocket) m.invoke(mBluetoothDevice, 1);
    
    
           // mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(applicationUUID);
            mBluetoothAdapter.cancelDiscovery();
    
            mBluetoothSocket.connect();
    
    
    
    
    
        }
        catch (IOException eConnectException) 
        {
            Log.d(TAG, "CouldNotConnectToSocket", eConnectException);
             closeSocket(mBluetoothSocket);
             return;
        } catch (SecurityException e) {
    
            Log.d(TAG, "CouldNotConnectToSocket", e);
            closeSocket(mBluetoothSocket);
        } catch (NoSuchMethodException e) {
    
            Log.d(TAG, "CouldNotConnectToSocket", e);
            closeSocket(mBluetoothSocket);
        } catch (IllegalArgumentException e) {
            Log.d(TAG, "CouldNotConnectToSocket", e);
            closeSocket(mBluetoothSocket);
        } catch (IllegalAccessException e) {
            Log.d(TAG, "CouldNotConnectToSocket", e);
            closeSocket(mBluetoothSocket);
        } catch (InvocationTargetException e) {
            Log.d(TAG, "CouldNotConnectToSocket", e);
            closeSocket(mBluetoothSocket);
        }
    }
    

    试试这个...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多