Java inputstream read() 没有得到完整的字节数组数据
Java inputstream read() not get full byte array data
嵌入式系统项目,在该项目中,我将使用蓝牙模块从我的微控制器获得对 android 设备的一些响应,但我无法从该行 bytes = "mmInStream.read(buffer)"
获取字节。
当我使用这个将 byte[] 缓冲区转换为 String 时
String data=new String(bytes)
我没有正确获取从我的微控制器发送的数据。有时字符丢失..
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
String data=new String(bytes);
System.out.println(data);
// Send the obtained bytes to the UI Activity
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
请帮帮我
请尝试使用 BufferedReader。
It reads text from a character-input stream, buffering characters so
as to provide for the efficient reading of characters, arrays, and
lines.
如果您使用 Java 7 或更早版本,以下代码将有所帮助:
try (BufferedReader reader = new BufferedReader(new InputStreamReader(mmInStream))){
String line = null;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
connectionLost();
} catch(IOException e) {
e.printStackTrace();
}
如果您使用 Java 6 或更小的版本,请使用此代码:
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(mmInStream));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connectionLost();
}
但是这种方法有缺点。您可以阅读有关它们的信息,例如here
嵌入式系统项目,在该项目中,我将使用蓝牙模块从我的微控制器获得对 android 设备的一些响应,但我无法从该行 bytes = "mmInStream.read(buffer)"
获取字节。
当我使用这个将 byte[] 缓冲区转换为 String 时
String data=new String(bytes)
我没有正确获取从我的微控制器发送的数据。有时字符丢失..
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
String data=new String(bytes);
System.out.println(data);
// Send the obtained bytes to the UI Activity
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
请帮帮我
请尝试使用 BufferedReader。
It reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
如果您使用 Java 7 或更早版本,以下代码将有所帮助:
try (BufferedReader reader = new BufferedReader(new InputStreamReader(mmInStream))){
String line = null;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
connectionLost();
} catch(IOException e) {
e.printStackTrace();
}
如果您使用 Java 6 或更小的版本,请使用此代码:
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(mmInStream));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connectionLost();
}
但是这种方法有缺点。您可以阅读有关它们的信息,例如here