Mor*_*ter 11 android bluetooth rfcomm
我在使用Android上的蓝牙RFCOMM插槽时遇到了数据完整性问题.我连接没有任何问题,但我收到的数据是乱码,与发送的数据不同.数据由RS232设备通过手机连接的蓝牙适配器发送.适配器没有问题,因为如果我连接笔记本电脑,数据会被正确接收.
我的蓝牙连接是基于Android开发人员网站(http://developer.android.com/resources/samples/BluetoothChat/index.html)上的BluetoothChat示例应用程序处理的,没有任何更改.发送的数据是纯文本和控制字符(在显示给用户之前被删除).我遇到的具体问题是缺少一些文本,其中一些是重复的,等等.有趣的是,如果我连接到具有终端应用程序的计算机并输入,那么数据就可以正常传输.此外,如果我使用GetBlue应用程序连接到设备,数据将被正确接收.
所以我想问题是GetBlue可能做些什么来处理其蓝牙数据传输,或者是否有其他方式通过Android上的RFCOMM插槽接收蓝牙数据?
Mor*_*ter 10
对于溶液中的修复是在所连接的螺纹上的InputStream创建字符串,在调用读取后直接(),然后使字符串返回到主线程以供显示.无论出于何种原因,在线程之间传递字节数组会导致重复的重复和数据丢失.
修改后的run()代码:
public void run() {
byte[] buffer = new byte[256]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, readMessage)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
处理程序接收:
case MESSAGE_READ:
// Read in string from message, display to mainText for user
String readMessage = (String) msg.obj;
if (msg.arg1 > 0) {
mainText.append(readMessage);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7896 次 |
| 最近记录: |