求助各位大神,蓝牙串口如何完整接收inputstream中的数据

yqdragon 2012-07-06 04:03:47
我手机通过蓝牙和蓝牙内嵌模块连接,当手机发送0X68 0xFF 0xFF 0xFF 0xFF 0x69 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF后,蓝牙内嵌模块连接的设备会自动回0X68 0X00 0X00 0X00 0X69,如何完整接收回我的这个数据,求助各位大神。


if (btAdapt.getState() == BluetoothAdapter.STATE_OFF)
{
// 如果蓝牙还没开启
Toast.makeText(Yepef.this, "请先打开蓝牙", 1000).show();
return;
}
if (connFlag!=1)
{
// 如果蓝牙还没连接
Toast.makeText(Yepef.this, "蓝牙未连接", 1000).show();
return;
}
byte[] outbyte = new byte[]{(byte)0x68,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,
(byte)0x69,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF};

try {
outStream = btSocket.getOutputStream();
outStream.write(outbyte);
outStream.flush();
//延时
inStream = btSocket.getInputStream();
//此处接收的代码如何写


} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}


备注:上面接收的代码我用如下代码,接收不全
byte[] buffer = new byte[1024];
int bytes=-1;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
while (true)
{
try {
// Read from the InputStream
while ((bytes = inStream.read(buffer)) >0)
{
byteStream.write(buffer, 0, bytes-1);
}
//以下代码始终没有执行
byte[] data = byteStream.toByteArray();
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Yxset.MESSAGE_READ, len, -1, data).sendToTarget();
Log.i(TAG, "ok");
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
...全文
683 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
网络咖啡 2014-07-30
  • 打赏
  • 举报
回复
按照协议的数据格式,只有当收到完整的包时才跳出read所在的循环即可,具体逻辑根据协议格式而不同
mayundoyouknow 2014-07-30
  • 打赏
  • 举报
回复
指的是流的分批发送的数据,若对方发送5个字节,可能第一次接收到第一个字节,count = mInputStream.available()返回的是1;第二次获取后面四个字节count = mInputStream.available()返回的是4
bright_2013 2014-07-19
  • 打赏
  • 举报
回复
引用 3 楼 xiaoxiaohude 的回复:
int count = 0; while (count == 0) { count = mmInStream.available();//输入流中的数据个数。 } System.out.println("字符串总长度为:"+count); int readCount = 0; // 已经成功读取的字节的个数 while (readCount < count) { readCount += mmInStream.read(buffer, readCount, count - readCount); } 就是这段代码,如果发送端发送10个字符,我这里有时会先接收一个字符,然后又接收9个。不是整体一下子接收过来。我把上述代码放到了一个线程的while循环中。
想问一下 “count = mmInStream.available(); //输入流中的数据个数” ,这句话中的“输入流中的数据个数”是指输入流的总长度吗,而不是流的分批发送的数据?
bright_2013 2014-07-19
  • 打赏
  • 举报
回复
我也遇到这个问题了... ...同求
程序猿终结者 2014-07-08
  • 打赏
  • 举报
回复
我也遇到这个问题啊 = =
zwh751279833 2014-06-16
  • 打赏
  • 举报
回复
各位是怎么解决的尼 小弟也遇到这样的问题了!!
mingwu_yuan 2014-03-14
  • 打赏
  • 举报
回复
各位是怎么解决的尼 小弟也遇到这样的问题了!!
-SmartTiger- 2013-09-23
  • 打赏
  • 举报
回复
int count = 0; while (count == 0) { count = mmInStream.available();//输入流中的数据个数。 } System.out.println("字符串总长度为:"+count); int readCount = 0; // 已经成功读取的字节的个数 while (readCount < count) { readCount += mmInStream.read(buffer, readCount, count - readCount); } 就是这段代码,如果发送端发送10个字符,我这里有时会先接收一个字符,然后又接收9个。不是整体一下子接收过来。我把上述代码放到了一个线程的while循环中。
-SmartTiger- 2013-09-23
  • 打赏
  • 举报
回复
同求啊,最近也遇到这个问题了。从蓝牙端口接收到的数据是一段一段的,不是连续的,虽然没有丢数据,但不是一块整体接收过来的。
yqdragon 2012-07-06
  • 打赏
  • 举报
回复
咋没人呢?期待中.....

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧