2012-02-01 5 views
2

私が電話を受けると、通話中に、私はアプリに戻る 画面暗くなると、アプリケーションがバック 応答しない場合は、非同期タスク 経由のBluetoothデバイスと通信するアプリケーションを持っていますボタンは機能しません...そしてANRダイアログは表示されません アイデアはありますか?ここアンドロイドのBluetoothアプリケーション

は、接続を処理するコードです:

@Override 
protected Object doInBackground(Object... params) { 
    //boolean protocolUpdated; 
    int read = 0;          // The amount of bytes read from the socket. 
    byte[] buff = new byte[MessageHandler.BUFFERSIZE]; // The data buffer. 
    byte[] tmpSend = null;        // Misc bytes arrays returned from ProtocolParser as answers to send after decoding calls. 
    in = null; 
    out = null; 

    try { 
     if (Float.parseFloat(version) > 2.2){ 
      Method m = dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
      sock = (BluetoothSocket) m.invoke(dev, 1); 
     } 

     else sock = dev.createRfcommSocketToServiceRecord(UUID_RFCOMM_GENERIC); // UUID is constant for serial BT devices. 
     sock.connect(); // connect to the BT device. This is rather heavy, may take 3 secs. 
     sendMessage(MESSAGE_CONNECTION_ESTABLISHED); 
     in = sock.getInputStream(); 
     out = sock.getOutputStream(); 

     timer = new Timer(); 
     startFinishTimer();   //initialize finish timer 

     while(read != -1) {  // read = -1 means EOF. 
      do {    // as long as there is anything to send in the send queue - send it. 
       tmpSend = parser.nextSend(); 
       if(tmpSend != null){ 
        String msg = parseMessage(tmpSend); 
        Log.d("Writing:",msg); 
        out.write(tmpSend); 
       } 
      } while(tmpSend != null); 
      read = in.read(buff);  // read. This is a blocking call, to break this, interrupt the thread. 
      timer.cancel();    
      startFinishTimer();   //read is a blocking call so timer should be restarted only after read bytes. 
      parser.parse(buff,read); // parse the read message using the logic in the ProtocolParser derived class. 
      tmpSend = parser.getPool(); // if pool ack is required - send it. 
      if (tmpSend != null){ 

       Log.d("Writing:",parseMessage(tmpSend)); 
       out.write(tmpSend); 

      } 
      if (read != 0){ 
       Log.d("Read:",parseMessage(buff)); 
       tmpSend = parser.getAnswer(); // if answer is required (based on message) - send it. 
       if(tmpSend != null){ 
        out.write(tmpSend); 
       } 
      } 
      else { 
       Exception e = new IOException(); 
       throw e; 
      } 
     } 
    }catch (IOException e){ 
     e.printStackTrace(); 
     Log.d("Connection: ", "Bluetooth Connection CRASHED!"); 
     sendMessage(MESSAGE_CONNECTION_LOST); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
    return null; 
} 

答えて

0

は実際にあなたの問題を発見するのに十分なコンテキストがありません。

メインスレッドからこのタスクを起動するようにしてください。それ以外の場合は、PostExecuteが間違ったスレッドにアタッチされるため、競合が発生する可能性があります。

コード内の複数のハンドラに同じメッセージを送信しないように注意してください。 メッセージはリンクされたリストなので、その場合はANRを取得できます。

/data/anr/traces.txtを取得して、ANRでないことを確認してください。

ファイルの先頭に時間を置いて確認することができます。

+0

セルゲイの返事をありがとう。あなたが気づいたかどうかはわかりませんが、この質問は2年前に尋ねられました。悲しいことに、プロジェクトはいつか終了しました。私は本当に最後に何が問題だったかを覚えていません。 – zwebie

関連する問題