2015-10-15 162 views
0

所以我得到这个错误:W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1蓝牙插座:读失败,插座可能关闭或超时

我想在三星Galaxy S4(API 21)和S2(API 16)连接到自定义设备通过蓝牙。有时它有效。我不知道会发生什么。

我知道还有其他问题的答案,但我觉得我已经看到了他们。

  • 我不能使用BluetoothDevice的“createRfCommSocket”方法。
  • 我的UUID是正确的,因为我知道我的自定义设备的UUID。
  • 我无法以编程方式启用和禁用BluetoothAdapter。
  • 我试着清除App Cache。
  • 清除蓝牙缓存后有时会起作用。

所以我的连接代码:发生在mBluetoothSocket.connect()

private class ConnectAsyncTask extends AsyncTask<String, Void, Boolean> { 

    @Override 
    protected Boolean doInBackground(String... params) { 
     try { 
      Thread.sleep(5000); 
      UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
      mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(params[0]); 
      mBluetoothSocket = mBluetoothDevice 
        .createRfcommSocketToServiceRecord(uuid); 
      mBluetoothSocket.connect(); 
      mOutputStream = mBluetoothSocket.getOutputStream(); 
      mInputStream = mBluetoothSocket.getInputStream(); 
      return true; 
     } catch (Exception e) { 
      e.printStackTrace(); 
      try { 
       mBluetoothSocket.close(); 
      } catch (Exception e1) { 
       e1.printStackTrace(); 
      } 
      return false; 
     } 
    } 

    @Override 
    protected void onPostExecute(Boolean aBoolean) { 
     if (aBoolean) { 
      connectDeviceCallback.onDeviceConnected(null); 
      startReceiverThread(); 
      startSendingThread(); 
     } else { 
      connectDeviceCallback.onError("An error occured"); 
     } 
    } 
} 

错误。

+0

谷歌真的需要修复这个愚蠢的消息。 '套接字关闭'与'套接字超时'完全不同,它们都不同于'连接关闭','连接超时'和'连接被拒绝'。 – EJP

回答

0

我的手机距离设备太远,无法正确连接。