Android phone 和蓝牙设备通信错误
Android phone and bluetooth device communication error
我正在修改一个Android蓝牙示例程序,实现我的androidphone和一个ELM327模块通过蓝牙进行通信
该应用程序是使用 Android 工作室从程序构建的。该应用程序在两个 android phone 之间运行良好。
然而,当我 运行 它在我的 android phone 之一中并尝试与 ELM327 模块通信时,来自 ELM327 的输入消息被破坏并且有一段时间了严重延误。你能解释一下为什么会出现不同的现象吗?如能帮忙整改程序,将不胜感激。
以下是此应用程序的部分示例代码:
BluetoothChat.java:处理程序
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
Log.d("ELM327", "message is send:" + writeMessage +"; length is:" + writeMessage.length());
mAdapter.notifyDataSetChanged();
messageList.add(new androidRecyclerView.Message(counter++, writeMessage, "Me"));
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
//Log.d("ELM327", "message is received:" + readBuf + "; length is:" + readBuf.length);
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
Log.d("ELM327", "message is received:" + readMessage +"; length is:" + readMessage.length());
mAdapter.notifyDataSetChanged();
messageList.add(new androidRecyclerView.Message(counter++, readMessage, mConnectedDeviceName));
break;
case MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;
}
}
};
BluetoothChatService.java: 运行 读取输入消息的函数
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 BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
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);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
connectionLost();
break;
}
}
}
我错过了在从 phone 应用程序发送到 ELM327 的每条命令消息末尾添加“\r”。
添加"\r"后,可以收到ELM327的正确反馈
我正在修改一个Android蓝牙示例程序,实现我的androidphone和一个ELM327模块通过蓝牙进行通信
该应用程序是使用 Android 工作室从程序构建的。该应用程序在两个 android phone 之间运行良好。
然而,当我 运行 它在我的 android phone 之一中并尝试与 ELM327 模块通信时,来自 ELM327 的输入消息被破坏并且有一段时间了严重延误。你能解释一下为什么会出现不同的现象吗?如能帮忙整改程序,将不胜感激。
以下是此应用程序的部分示例代码:
BluetoothChat.java:处理程序
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
Log.d("ELM327", "message is send:" + writeMessage +"; length is:" + writeMessage.length());
mAdapter.notifyDataSetChanged();
messageList.add(new androidRecyclerView.Message(counter++, writeMessage, "Me"));
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
//Log.d("ELM327", "message is received:" + readBuf + "; length is:" + readBuf.length);
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
Log.d("ELM327", "message is received:" + readMessage +"; length is:" + readMessage.length());
mAdapter.notifyDataSetChanged();
messageList.add(new androidRecyclerView.Message(counter++, readMessage, mConnectedDeviceName));
break;
case MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;
}
}
};
BluetoothChatService.java: 运行 读取输入消息的函数
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 BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
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);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
connectionLost();
break;
}
}
}
我错过了在从 phone 应用程序发送到 ELM327 的每条命令消息末尾添加“\r”。
添加"\r"后,可以收到ELM327的正确反馈