2017-01-25 12 views
0

私は2つのデバイスがBluetooth経由で接続されています。その後、クライアントデバイスのBluetooth接続を切断しました。このクライアントデバイスのブロードキャスト受信者は切断を検出し、それを前の動作に戻すことができます。このような何か:BluetoothDocket.ACTION_ACL_DISCONNECTEDは、Bluetoothソケットが閉じているにもかかわらず呼び出されません。

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ソケットにもかかわらず、断線を検出することはできませんが閉じています!サーバー装置のブロードキャスト受信者が切断を検出できません。以下のコードでは、クライアントデバイスが接続されていないときに、サーバーデバイスの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); 
関連する問題