簡體   English   中英

Android藍牙日志記錄填滿了logcat

[英]Android Bluetooth logging filling up logcat

我使用藍牙聊天示例作為實現從手機到嵌入式設備的BT連接的起點。 我能夠成功連接到設備但是一旦建立連接,logcat就會因大量的日志記錄而被覆蓋。 我第一次做BT聊天應用手機打電話時沒有看到這種類型的日志記錄。

這是一遍又一遍的重復。 它基本上使logcat無法使用。 到目前為止,我還沒有找到一種方法來配置日志記錄或為什么它記錄這么多。 任何見解將不勝感激。

03-08 14:29:04.941: DEBUG/BluetoothSocket.cpp(11422): availableNative
03-08 14:29:04.957: DEBUG/BluetoothSocket(11422): available
03-08 14:29:04.957: DEBUG/BluetoothSocket.cpp(11422): availableNative
03-08 14:29:04.971: DEBUG/BluetoothSocket(11422): available
03-08 14:29:04.976: DEBUG/BluetoothSocket.cpp(11422): availableNative
03-08 14:29:04.989: DEBUG/BluetoothSocket(11422): available
03-08 14:29:04.991: DEBUG/BluetoothSocket.cpp(11422): availableNative
03-08 14:29:05.016: DEBUG/BluetoothSocket(11422): available
03-08 14:29:05.016: DEBUG/BluetoothSocket.cpp(11422): availableNative
03-08 14:29:05.034: DEBUG/BluetoothSocket(11422): available
03-08 14:29:05.036: DEBUG/BluetoothSocket.cpp(11422): availableNative
03-08 14:29:05.050: DEBUG/BluetoothSocket(11422): available
03-08 14:29:05.051: DEBUG/BluetoothSocket.cpp(11422): availableNative
03-08 14:29:05.066: DEBUG/BluetoothSocket(11422): available
03-08 14:29:05.066: DEBUG/BluetoothSocket.cpp(11422): availableNative
03-08 14:29:05.081: DEBUG/BluetoothSocket(11422): available
03-08 14:29:05.081: DEBUG/BluetoothSocket.cpp(11422): availableNative
03-08 14:29:05.086: DEBUG/(2419): jw_if_rfcomm_cl_cback: jw_if_rfcomm_cl_cback event=BTA_JV_RFCOMM_READ_EVT
03-08 14:29:05.086: DEBUG/(2419): jv_forward_data_to_jni: BTA_JV_RFCOMM_DATA_IND_EVT bta hdl 2
03-08 14:29:05.086: DEBUG/(2419): bts_log_tstamps_us: [update stats] ts    1263504, bta hdl 2, diff 01263504, tx_q 1 (1), rx_q 0 (0)
03-08 14:29:05.086: DEBUG/BLZ20_WRAPPER(11422): blz20_wrp_poll: transp poll : (fd 41) returned r_ev [POLLIN ] (0x1)
03-08 14:29:05.086: DEBUG/BLZ20_WRAPPER(11422): blz20_wrp_poll: return 1
03-08 14:29:05.086: DEBUG/BLZ20_WRAPPER(11422): blz20_wrp_read: read 122 bytes out of 1024 on fd 41
03-08 14:29:05.101: DEBUG/BluetoothSocket(11422): read
03-08 14:29:05.101: DEBUG/BluetoothSocket.cpp(11422): readNative
03-08 14:29:05.101: DEBUG/ASOCKWRP(11422): asocket_read
03-08 14:29:05.106: INFO/BLZ20_WRAPPER(11422): blz20_wrp_poll: nfds 2, timeout -1 ms
03-08 14:29:05.117: DEBUG/BluetoothSocket(11422): available
03-08 14:29:05.121: DEBUG/BluetoothSocket.cpp(11422): availableNative

在玩了我的Arduino Board + Bluetooth-Adapter之后,我嘗試用MATT BELL'S BLOG實現藍牙代碼。 問題是以下代碼:

//final Handler handler = new Handler();
workerThread = new Thread(new Runnable()
{
    public void run()
    {
        while(!Thread.currentThread().isInterrupted() && !stopWorker)
        {
            try {
                int bytesAvailable = mmInputStream.available();
                if(bytesAvailable > 0)
                {
                    //Log.d(TAG,"bytesAvailable: "+bytesAvailable + " readBufferPosition: "+readBufferPosition);
                    byte[] packetBytes = new byte[bytesAvailable];
                    mmInputStream.read(packetBytes);

                    for(int i=0;i<bytesAvailable;i++)
                    {
                        byte delimiter = 0x0A;     // /n bzw. LF
                        byte b = packetBytes[i];
                        if(b == delimiter)
                        {
                            byte[] encodedBytes = new byte[readBufferPosition];
                            System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                            final String data = new String(encodedBytes, "US-ASCII");
                            //final String data = new String(readBuffer);
                            readBufferPosition = 0;

                            Log.d(TAG,""+data);

//                                            //The variable data now contains our full command
//                                            handler.post(new Runnable()
//                                            {
//                                                public void run()
//                                                {
//                                                    //myLabel.setText(data);
//                                                    Log.d(TAG,""+data);
//                                                }
//                                            });
                        }
                        else
                        {
                            readBuffer[readBufferPosition++] = b;
                        }
                    }
                }
            } catch (Exception e) {
                Log.d(TAG,"Exeption 2: "+e.getMessage());
                stopWorker = true;
            }
        }
    }
});
workerThread.start();

調用以下函數會導致logCat中出現巨大的垃圾郵件

int bytesAvailable = mmInputStream.available();

logcat的:

PS:時間戳實際上是固定版本。 不使用此修復程序將導致垃圾郵件每5 毫秒有效地阻止我的整個日志

03-17 18:43:06.615: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:06.715: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:06.820: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:06.920: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:07.020: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:07.120: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:07.220: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:07.320: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:07.420: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:07.520: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:07.620: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:07.725: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:07.825: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:07.925: VERBOSE/BluetoothSocket.cpp(8871): availableNative
03-17 18:43:08.025: VERBOSE/BluetoothSocket.cpp(8871): availableNative

我目前的修復是在while循環結束時添加以下代碼,從而減少垃圾郵件。

try {
Thread.sleep(100);
} catch (Exception e) {
Log.d(TAG,"Exception Thread.sleep()");
}

我希望這能幫助一些有類似問題的人。

編輯:目前我不得不將睡眠時間減少到10毫秒bam ..垃圾郵件

03-18 15:50:18.470: VERBOSE/BluetoothSocket.cpp(3482): availableNative
03-18 15:50:18.470: VERBOSE/BluetoothSocket.cpp(3482): availableNative
03-18 15:50:18.470: VERBOSE/BluetoothSocket.cpp(3482): availableNative
03-18 15:50:18.470: VERBOSE/BluetoothSocket.cpp(3482): availableNative
03-18 15:50:18.470: VERBOSE/BluetoothSocket.cpp(3482): availableNative
03-18 15:50:18.470: VERBOSE/BluetoothSocket.cpp(3482): availableNative
03-18 15:50:18.470: VERBOSE/BluetoothSocket.cpp(3482): availableNative
03-18 15:50:18.470: VERBOSE/BluetoothSocket.cpp(3482): availableNative
03-18 15:50:18.470: VERBOSE/BluetoothSocket.cpp(3482): availableNative
03-18 15:50:18.470: VERBOSE/BluetoothSocket.cpp(3482): availableNative

這一定是一個錯誤,一個嚴重的錯誤:

  • Logcat有時只顯示此調試打印 - 盡管我的代碼總是遇到它

  • 或者藍牙芯片有時會松動......(更有可能)。

只是將日志級別從verbose轉換為調試不會改變傳輸到Logcat的大量數據,阻止所有正常日志......

我偶然發現了這個:3年后這個日志堵塞仍然存在 - 至少在我的三星GT-I8190(4.1.2)上。

罪魁禍首是mmInputStream.available()

解決這個問題的一種方法是不使用它的available()方法。 代替:

int bytesAvailable = mmInputStream.available();

if(bytesAvailable > 0)
{...

您可以使用類似下面的代碼來解決日志堵塞問題......

int i = -1;
while((i=mmInputStream.read())!=-1)
{                           
  char c=(char)i;

  // you can do your buffering here...
  Log.i("readBT","have char: "+c);
}

下面的代碼也適用於有這個討厭的bug的手機。 它可能應該修改,因為它只是一個快速和骯臟的修復:

void beginListenForData()
    {
        final Handler handler = new Handler(); 
        final byte delimiter = 10; //This is the ASCII code for a newline character

        stopWorker = false;
        readBufferPosition = 0;
        readBuffer = new byte[1024];
        workerThread = new Thread(new Runnable()
        {
            public void run()
            {                
               while(!Thread.currentThread().isInterrupted() && !stopWorker)
               {
                    try 
                    {
                        int i = -1;
                        while((i=mmInputStream.read())!=-1)
                        {
                           byte b=(byte)i;

                           Log.i("readBT","have byte: " + b);

                           if ( (readBufferPosition >= 1023) || (b == delimiter) )
                           {
                               byte[] encodedBytes = new byte[readBufferPosition];
                               System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                               final String data = new String(encodedBytes, "UTF-8");
                               readBufferPosition = 0;

                               handler.post(new Runnable()
                               {
                                   public void run()
                                   {
                                       myLabel.setText(data);
                                   }
                               });                             
                           }
                           else
                           {
                               readBuffer[readBufferPosition++] = b;
                           }                           
                        }                       
                    } 
                    catch (IOException ex) 
                    {
                        stopWorker = true;
                    }
               }
            }
        });

        workerThread.start();
    }

“avaliableNative”具有詳細的優先級。 要刪除消息(以及所有詳細消息),請使用“*:D”參數作為最后一個logcat參數。 通過在軟盤圖標旁邊的組合框中使用“debug”優先級,可以在Eclipse中實現類似的功能。

在DDMS中,您可以使用調試,信息,錯誤,警告按鈕來過濾掉內容,您還可以創建一個特殊的過濾器,只顯示您感興趣的內容。不要認為有任何藍牙設置可以關閉日志記錄你可以用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM