2015-10-15 118 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

我的手機距離設備太遠,無法正確連接。