Android 请帮助:从缓冲区提取数据(蓝牙和处理程序与EditText有关的问题)?
我正在使用蓝牙串行数据 信息被“捕获”在缓冲区中,我无法用编辑文本在屏幕上显示它Android 请帮助:从缓冲区提取数据(蓝牙和处理程序与EditText有关的问题)?,android,bluetooth,serial-port,handler,Android,Bluetooth,Serial Port,Handler,我正在使用蓝牙串行数据 信息被“捕获”在缓冲区中,我无法用编辑文本在屏幕上显示它 04-16 13:58:28.483: I/debugging(2380): Receiving Data 04-16 13:58:28.484: I/debugging(2380): run success 04-16 13:58:28.487: I/debugging(2380): buffer read OK 六角形 ASCII码: 04-16 13:58:28.492: I/debugging(2380)
04-16 13:58:28.483: I/debugging(2380): Receiving Data
04-16 13:58:28.484: I/debugging(2380): run success
04-16 13:58:28.487: I/debugging(2380): buffer read OK
六角形
ASCII码:
04-16 13:58:28.492: I/debugging(2380): ??0183600NEWDEVICE
04-16 13:58:28.492: I/debugging(2380): 0147900:4239
04-16 13:58:28.492: I/debugging(2380): 0147901:0200
04-16 13:58:28.492: I/debugging(2380): 0147902:2971
04-16 13:58:28.492: I/debugging(2380): 0157903:01598
04-16 13:58:28.492: I/debugging(2380): 0157904:02754
04-16 13:58:28.492: I/debugging(2380): 0157905:03383
04-16 13:58:28.492: I/debugging(2380): 0157906:02590
04-16 13:58:28.492: I/debugging(2380): 0187908:04.02.01
04-16 13:58:28.492: I/debugging(2380): 0157909:19.45
04-16 13:58:28.492: I/debugging(2380): 0117911:0
04-16 13:58:28.492: I/debugging(2380): 0098540
04-16 13:58:28.492: I/debugging(2380): ??????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????
之后,应用程序崩溃,显示奇怪的消息(我摘录):
我使用的是典型的处理程序:
Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
Log.i(tag,"in handler");
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
mTextView.setText("PACO",TextView.BufferType.EDITABLE);
//DO SOMETHING
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
Toast.makeText(getApplicationContext(), "Conectar", Toast.LENGTH_SHORT).show();
String s = "conectado con Éxito";
connectedThread.write(s.getBytes());
Log.i(tag,"connected");
byte[] iniciar = new byte[]{(byte)0x05};
// B01/n en Hexadecimal (Introducir para que Espirómetro envíe datos
byte [] datos = new byte[] {(byte)0x42, (byte)0x30, (byte)0x31, (byte)0x0D};
//byte [] datos = new byte[] {(byte)0x43, (byte)0x0D};
connectedThread.write(iniciar);
Log.i(tag, "waiting for commands");
Log.i(tag, convertToHex(datos));
//Get Data
connectedThread.write (datos);
Log.i(tag,"Receiving Data");
connectedThread.run();
//connectedThread.cancel();
break;
case MESSAGE_READ:
Log.i(tag,"He estado aquí2");
byte[] readBuf = (byte[])msg.obj;
/*
String string = new String(readBuf);
Log.i(tag, string);
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
*/
Log.i(tag,"He estado aquí");
String readMessage = new String(readBuf, 0, msg.arg1);
mTextView.setText(readMessage);
//added by AMJ in attempt to display variable in textview
break;
}
}
};
以及典型的ConnectedThread:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(tag, "run success");
byte[] buffer;
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
buffer = new byte [256];
bytes = mmInStream.read(buffer);
Log.i(tag, "buffer read OK");
// Send the obtained bytes to the UI activity
Log.i(tag, convertToHex(buffer));
Log.i(tag, hexToAscii(convertToHex(buffer)));
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
}catch (IOException e) {
e.printStackTrace();
Log.i(tag, "buffer read failed");
System.out.print("read error");
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
}
谢谢你的帮助 每次开始读取时,我都会停止分配缓冲区。 如果您以前的邮件被随机垃圾收集怎么办 所以删除
buffer = new byte [256];
然后,要使其在连续读取期间仍能工作,请更换
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
with
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer.clone()).sendToTarget();
你能把你的全部源代码寄给我吗?我可以看一看。我该怎么办?我是否在私人信息中向您发送我的代码?或者在这里发布?给我发电子邮件@rsavutiu@gmail.com. 同时发送有关应用程序、如何测试等的说明。问题是,要测试应用程序,您需要“Ashtma Monitor”肺活量计:(别担心,我们可以找到其他设备来传输数据。事实上,我建议您再次阅读BluetoothChat,尝试执行他们的操作。但是,如果您愿意,请通过Github或其他方式将代码发送给我,我可以调试它。
buffer = new byte [256];
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
with
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer.clone()).sendToTarget();