2014-05-19 3 views
0

안드로이드 장치에서 내 장치와 원격 장치 (쌍으로 연결)를 연결하고 싶습니다. 원격 장치는 HC-05 모듈입니다. 내 대구는 다음과 같습니다 :안드로이드 블루투스가 원격 장치에 연결

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 = mmDevice.createRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { 
     } 
     mmSocket = tmp; 
    } 

    @Override 
    public void run() { 
     // Cancel discovery because it will slow down the connection 
     ba.cancelDiscovery(); 

     try { 
      mmSocket.connect(); 
     } catch (IOException e) {} 

     // Do work to manage the connection (in a separate thread) 
     // manageConnectedSocket(mmSocket); 
     // connected(); 
     tv1.setText("connect"); 

    } 

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

줄에 mSocket.connect() 오류가 나타납니다. 내 앱을 실행 한 다음 메시지가 표시됩니다. 불행히도 (앱 이름)이 중지되었습니다.

도와주세요.

+0

LogCat에서 오류 메시지와 예외를 살펴 보셨습니까? 이 코드에서는 –

+0

이 아니지만 비슷한 코드에서 오류가 발생합니다. 읽기 실패 소켓이 닫혔거나 시간 초과되었습니다. 읽기 – user3223395

답변

0

IOException을 자동으로 무시하지 마십시오. 너라면 IOException 의미합니다. 어떤 이유로 든 소켓을 만들 수 없습니다. 그러면 mSocket은 null로 남아 있으므로 예외가 발생합니다. 매니페스트에서 블루투스 권한이없는 것일 수 있습니다.

+0

mSocket이 null이 아니며 오류가 발생합니다. 읽기 실패 소켓이 닫혔거나 시간 초과 읽기가 반환됩니다. – user3223395

관련 문제