24小时热门版块排行榜    

查看: 1748  |  回复: 0

冲破迷途

铁虫 (初入文坛)

[求助] 蓝牙串口数据读取不完整怎么解决???

我在bluetoothchat 的demo基础上对程序做了下修改期望完成的功能是接收下位机发送来的16进制数 0F 00 00 00 00 00 00 00 00 6D 3C共11个字节。
                  字节转换16进制在listview上显示出现 :第一行0F
                                                                                第二行00 00 00 00 00 00 00 00 6D 3C或者
                                                                                                                      第一行0F
                                                                                 第二行00 00 00 00 00 00 00 00 6D
                                                                                  第三行3C
                                                                                  或者0F 00 00 00 00 00 00 00 00 6D 3C的情况,
                      我想要的结果是第三种 11字节数据全部显示在一行当中。对程序分析后我认为是程度分多次读取的数据并不是一次将数据读取完,因此对程序做了一下处理:可是运行出错了。。。。。。。叙述很啰嗦求大神帮忙啊!!!各种感谢
   public void run() {
            Log.i(TAG, "BEGIN mConnectedThread";
            byte[] buffer = new byte[2048];
            int bytes;
            int sum=0;
            int bytes1;
            ArrayList al =new ArrayList();
            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                 
                    bytes = mmInStream.read(buffer);
                    //System.out.println("读取字节数"+bytes);
                   // bytes1 = mmInStream.read(buffer);
                    ///System.out.println("111读取字节数"+bytes1);
                   if(bytes!=11)
                    {
                     sum=bytes;
                     al.add(buffer);
                     while(sum!=11)
                     {               
                      bytes1 = mmInStream.read(buffer);
                      al.add(buffer);
                      sum+=bytes1;
                     }
                  
                    mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, 11, -1,al.toArray())
                      .sendToTarget();
                    bytes=0;
                    }
                    if(bytes==11){
                     
                    mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                      .sendToTarget();
                    bytes=0;
                   }
                  
                  
                                    
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 冲破迷途 的主题更新
信息提示
请填处理意见