0
我想在从设备读取数据后(通过蓝牙插座) 问题是,当(套接字关闭时,while(true)完成,所以我不能再次写入。在阅读蓝牙插座后写一个输出流android
这是代码:
try {
// This will block until it succeeds in connecting to the device
// through the bluetoothSocket or throws an exception
bluetoothSocket.connect();
try {
connectingThread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
outstream = bluetoothSocket.getOutputStream();
outstream.write("P".getBytes());
instream = bluetoothSocket.getInputStream();
byte[] buffer = new byte[128]; // buffer store for the stream
int bytes; // bytes returned from read()
String readMessage = "";
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = instream.read(buffer);
if(bytes!= -1){
// Send the obtained bytes to the UI activity
readMessage = new String(buffer, 0, bytes);
arrayList.add(readMessage);
biodyMsg = TextUtils.join("", arrayList);
}
} catch (Exception e) {
Log.d("Read Error", e.toString());
}finally {
outstream = bluetoothSocket.getOutputStream();
outstream.write("F".getBytes());
}
break;
}
} catch (IOException connectException) {
connectException.printStackTrace();
try {
bluetoothSocket.close();
} catch (IOException closeException) {
closeException.printStackTrace();
}
}
你能帮我
对不起,没有把异常:java.io.IOException:损坏的管道 –
因此,对端已重置连接。你有什么问题? – EJP
在完成阅读后我可以在连接重置之前写入我想发送“F”的信息 –