我正在使用蓝牙串行数据。
信息是"被困"在缓冲区中,我无法使用我的EditText ...
在屏幕上显示它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
HEX
04-16 13:58:28.489: I/debugging(2380): 00050505050606303138333630304e45574445564943450d0a303134373930303a343233390d0a303134373930313a303230300d0a303134373930323a323937310d0a303135373930333a30313539380d0a303135373930343a30323735340d0a303135373930353a30333338330d0a303135373930363a30323539300d0a303138373930383a30342e30322e30310d0a303135373930393a31392e34350d0a303131373931313a300d0a303039383534300d0a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
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): ??????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????
之后应用程序崩溃显示WEIRD消息(我提取了一个摘录):
04-03 22:23:37.503: I/ViewRootImpl(13520): No key event currently.
04-03 22:23:37.510: I/ViewRootImpl(13520): No motion event currently.
04-03 22:23:37.510: I/ViewRootImpl(13520): The current processed event of VRI is none
04-03 22:23:37.510: I/ViewRootImpl(13520): notify IMS Dump
04-03 22:23:37.545: D/ANRAppManager(13520): MSG HISTORY IN MAIN THREAD:
04-03 22:23:37.545: D/ANRAppManager(13520): Current kernel time : 28387613ms
04-03 22:23:37.545: D/ANRAppManager(13520): === LONGER MSG HISTORY IN MAIN THREAD ===
04-03 22:23:37.545: D/MessageQueue(13520): Dump first 20 messages in Queue:
我是典型的处理程序:
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) { }
}
}
}
感谢帮助!!
答案 0 :(得分:0)
每次开始阅读时,我都会停止分配缓冲区。 如果您之前的邮件被随机收集垃圾怎么办?
删除
buffer = new byte [256];
然后在连续读取期间仍然使其工作,替换
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
with
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer.clone()).sendToTarget();
您能将完整的源代码发送给我吗?我可以看看。