2012-02-01 77 views
2

我有通過異步任務 藍牙設備,如果我收到一個電話,在通話過程中我回應用 屏幕變暗和應用程序沒有響應 回通信的應用按鈕不起作用...並沒有顯示ANR對話框 有什麼想法?Android的藍牙應用程序反應遲鈍

這裏是處理連接的代碼:

@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

謝謝你的答覆謝爾蓋。我不確定你是否注意到,但這個問題在兩年前接近問。不幸的是,這個項目在一段時間前完成了,告訴你實話,我甚至不記得最後有什麼問題...... – zwebie 2013-11-06 16:22:10