2017-01-25 3 views
0

두 개의 장치가 이제 Bluetooth를 통해 연결되어 있습니다. 그런 다음 클라이언트 장치의 Bluetooth 연결을 끊었습니다.이 클라이언트 장치의 브로드 캐스트 수신기는 연결 끊김을 감지하고 다시 이전 작업으로 전환 할 수 있습니다. 이런 식으로 뭔가 :BluetoothDocket.ACTION_ACL_DISCONNECTED가 bluetoothsocket이 닫혀 있어도 호출되지 않습니다.

private BroadcastReceiver myReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Message msg = Message.obtain(); 
     String action = intent.getAction(); 
     if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { 
      try { 

       Log.i("Disconnecting3", "Disconectinggg...."); 
       Intent intent1 = new Intent(Main3Activity.this, MainActivity.class); 
       startActivity(intent1); 
       } catch (Exception e) { 
        e.printStackTrace(); 
        } 
     } 
    } 
}; 

어쨌든, 서버 장치 내 다른 장치, 블루투스 소켓에도 불구하고 단선 검출되지 수있는이 장치 휴무입니다! 서버 장치의 브로드 캐스트 리시버가 연결 끊김을 감지 할 수 없습니다. 참고로 아래 코드는 클라이언트 장치가 분리되었을 때 서버 장치의 Bluetooth 소켓을 어떻게 닫을 수 있는지 보여줍니다. 이 때

private boolean CONTINUE_READ_WRITE; CONTINUE_READ_WRITE = true; public void run() { try { while (CONTINUE_READ_WRITE) { try { // Read from the InputStream. numBytes = mmInStream.read(mmBuffer); // Send the obtained bytes to the UI activity. Message readMsg = handleSeacrh.obtainMessage(MessageConstants.MESSAGE_READ, numBytes, -1, mmBuffer); readMsg.sendToTarget(); } catch (IOException e) { //nothing(); CloseConnection closeConnection = new CloseConnection(); closeConnection.start(); break; } } } catch (Exception e) { // Log.d(TAG, "Input stream was disconnected", e); } } public void cancel() { try { Log.i("TAG", "Trying to close the socket"); CONTINUE_READ_WRITE = false; mBluetoothSocket.close(); mmBluetoothSocket.close(); Log.i("TAG", "I thinked its still closing"); } catch (IOException e) { Log.e("TAG", "Could not close the connect socket", e); } } 

그래서 단선는 클라이언트 장치에서 발생, 그동안 (CONTINUE_READ_WRITE) .. 루프를 중단하고 새로운 스레드를 시작합니다 루프. 이런 식으로 뭔가 :

private class CloseConnection extends Thread { 

    public void run(){ 
     Log.i("Running","Runinnggggg"); 

     try { 
      mmInStream.close(); 
      mmOutStream.close(); 
      bluetoothDataTransmission.cancel(); 
      Log.i("Interrupted","InteruppteDDDD"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

답변

0

좋아, 나는 해결책을 발견, 단지 코드 줄을 추가해야

intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);