我使用android示例代码进行修改。只想接收包裹但是,我的代码只在这里修改
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
mTitle.setText(R.string.title_connected_to);
mTitle.append(mConnectedDeviceName);
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
mTitle.setText(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
mTitle.setText(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add(writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
//String readMessage = new String(readBuf, 0, msg.arg1);
//String readMessage = BytesTrans_fill.bytesToHexString(readBuf);
Log.d("read", BytesTrans.bytes2HexString(readBuf,msg.arg1));
String readMessage = BytesTrans.bytes2HexString(readBuf,msg.arg1);
ppV.setText(ppV.getText().toString() + readMessage + "★");
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;
}
}
};
和蓝牙聊天服务
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);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
并添加此功能
package com.example.android.BluetoothChat;
public class BytesTrans {
public static String byte2HexString(byte b) {
String ret = "";
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase() + " ";
return ret;
}
public static String bytes2HexString(byte[] b, int count) {
String ret = "";
for (int i = 0; i < count; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase() + " ";
}
return ret;
}
/*public static String String2byte(String b)
{
String[] ttt;
for (int i = 0; i < b.length(); i++)
{
for (int j= i; j<=i+1; j++)
{
ttt[i] = b;
}
}
String ttmp = "";
String tmp = "";
ret += tmp;
}*/
public static int hexToTen(String b) {
int D2 = Integer.parseInt(b,16);
return D2;
}
}
但这个程序有时甚至不会显示给我发送包的帧
我寄这样的包裹:
aa07210820001202140934390000000000000000000000000000000000297c0fe6b
但有时会收到包裹:
aa000297c0fe6b02131452470000000000000000000000000000297c0fe6B
我如何更改代码以接收完整包的帧
您尚未显示任何蓝牙接口代码。然而,如果这在很大程度上基于BluetoothChat示例,那么Bluetooth示例就有一个简单的问题:基本上,当从蓝牙套接字生成read()
并将其放入字节数组时,该数组引用会使用Handler
发送到UI,就像您正在做的那样。实际的问题是,如果BluetoothChat示例用于以比打字速度更快的速度接收数据,那么您会开始看到字符丢失或变得混乱,因为随后的read()
正在覆盖数组,而UI仍在读取数组以提取接收到的最后一组字符。
因此,如果您的MESSAGE_WRITE
对象包含对要对其进行套接字read()
调用的数组的引用,那么这可能就是丢失字符的原因。因此,请尝试在Message
中使用Arrays.copyOf()
发送阵列的副本。或者,也许有一个循环缓冲安排,你可以使用。
当我使用BluetoothChat示例作为我项目的基础时,我就遇到了这个问题。我个人为解决这个问题(并消除复制缓冲区等的需要)所做的是实现一种机制,通过该机制,我可以通过方法调用告诉蓝牙连接线程(包含阻塞套接字.read()
的线程)我期望响应的字节数(幸运的是,我正在处理的协议允许知道响应长度)。然后,连接线程只在收到完整响应时发送一个Message
,而不是向UI发送几个带有响应片段的Message
。
这是我的解决方案,使用Android聊天示例代码。这只是一个可能的解决方案。我收到以n
结尾的消息。我使用这个线程来读取通过蓝牙连接的Arduino的消息。在Arduino上,我可以确保每条消息都以n
结束,使用Serial.println( buffer );
。
我收到的所有内容都被附加到一个现有的字符串(queue
变量)中。当我在这个大字符串上有一整行时,我会将其发送到活动,并将其从queue
变量中删除。
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes, pos;
StringBuilder queue = new StringBuilder("");
String temp;
// Keep listening to the InputStream while connected
while (true) {
try {
bytes = mmInStream.read(buffer);
if (bytes > 0) {
String readMessage = new String(buffer, 0, bytes);
queue.append(readMessage);
pos = queue.indexOf("n");
while (pos >= 0) {
// Log.d("Temperatura", "indexOf() #" + pos);
temp = queue.substring(0, pos - 1);
queue.delete(0, pos + 1);
mHandler.obtainMessage(Constants.MESSAGE_READ, 0, 0, temp).sendToTarget();
pos = queue.indexOf("n");
}
}
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothChatService.this.start();
break;
}
}
}