[英]Wrong format while i getInputStream() on Android Studio from HC-06 BL Device
I use this AsyncTask to get lot of string from the HC-06. 我使用此AsyncTask从HC-06中获取了很多字符串。 I correct receive them but sometimes the string appear on different lines on a ListView.
我正确地收到了它们,但有时该字符串出现在ListView的不同行上。
For example if i send on arduino Ide jghv i got j on first line and ghv on the second D/BLE_Activity: j D/BLE_Activity: ghv 例如,如果我在arduino Ide jghv上发送,则我在第一行得到j,在第二行得到ghv D / BLE_Activity:j D / BLE_Activity:ghv
Can someone please tell me what i'm doing wrong? 有人可以告诉我我在做什么错吗?
private class Stream extends AsyncTask<Void, Void, Void>
{
protected Void doInBackground(Void... devices)
{
try {
while (btSocket.isConnected()) {
char[] buffer = new char[256];
int bytes;
InputStream tmpIn = btSocket.getInputStream();
InputStreamReader red = new InputStreamReader(tmpIn);
bytes = red.read(buffer);
String readMessage = new String(buffer, 0, bytes);
Log.d(TAG, readMessage);
Array.add(readMessage);
if (isCancelled()) break;
}
} catch (IOException e) {
}
return null;
}
}
This is how I create the ListView 这就是我创建ListView的方式
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, Array);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
sItems = (ListView) findViewById(R.id.Stream);
sItems.setAdapter(adapter);
And this is the declaration of Array and adapter 这是数组和适配器的声明
List<String> Array = new ArrayList<String>();
ArrayAdapter<String> adapter;
Thank to all of you 谢谢大家
Try to read text from inputstream line by line 尝试逐行从inputstream中读取文本
BufferedReader br = new BufferedReader(red);
String line;
while ((line = br.readLine()) != null) {
Array.add(line);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.