**bluetoothdatadisplay**
void beginListenForData() {
//final Handler handler = new Handler();
final Handler handler = new Handler(Looper.getMainLooper());
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[2048];//It uses a 1024 bytes long buffer because it doesn't have any way of knowing the length of the input stream.
workerThread = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted() && !stopWorker) {
try {
int bytesAvailable = mmInputStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
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");
readBufferPosition = 0;
handler.post(new Runnable() {
public void run() {
myLabel.setText(data);
//Log.d("MyLabel", data);
}
});
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
} catch (IOException ex) {
stopWorker = true;
}
}
}
});
workerThread.start();
}//end of begindata
嗨我试图从Arduino接收数据,但是当我发送所有数据时,由于只有一个textview使用,所以我的数据闪烁得非常快。如何在Android中将它们分隔成不同的文本框?谢谢!
答案 0 :(得分:0)
使用RecyclerView
,List<String>
代表您的数据集并连接到Adapter
的{{1}} :-)
每次向数据列表添加内容时,请使用
RecyclerView
让RecyclerView更新的方法: - )
编辑:
如 Kernald 所述,您实际应该使用
adapter.notifyDataSetChanged()
方法而不是通知整个数据集更改,如果您想使用100%正确和最新的方法: - )
答案 1 :(得分:0)
一个简单的解决方法是append新收到的数据,而不是替换旧数据。