После игры с моим Arduino Board + Bluetooth-адаптером я попытался реализовать Bluetooth-код из 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: временные метки фактически являются фиксированной версией. Неиспользование этого исправления приведет к спаму каждые 5ms , эффективно блокирующему весь журнал
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
Мое текущее исправление - добавление следующего кода в конце цикла , тогда как приводит к уменьшению количества спама.
try {
Thread.sleep(100);
} catch (Exception e) {
Log.d(TAG,"Exception Thread.sleep()");
}
Надеюсь, это поможет некоторым людям с похожими проблемами.
Редактировать: В настоящее время мне пришлось уменьшить таймер сна до 10 мсек. СПАМ
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