我会写BUG 2014-05-16 15:37 采纳率: 0%
浏览 13970

android蓝牙串口接收数据

最近在做android手机蓝牙和单片机通信,一直被接收数据完整性这个问题困扰。
为了使一次通信的包保持完整,我就构思了发送12位的包,包头(1)+数据(8)+校验(2)+包尾(1),校验为求和校验。单片机发送数据的函数如下

void SendString(uchar *dat)        //发送12位的包 包头(1)+数据(8)+校验(2)+包尾(1)
{   
uchar i,j;
uint check=0;       //求和校验
SendChar(0x01);    //0x01 SOH --> start of heading
i=strlen(dat);
for(j=0;j<i;j++)     //发送数据
{   
    SendChar(dat[j]);
    check+=dat[j];
}
for(j=8-i;j>0;j--)
{
    SendChar(0x20);  //用0x20 space 补齐未满数据位
    check+=0x20;
}
SendChar((uchar)(check>>8));         //校验位高位
SendChar((uchar)(check&0x00ff));         //校验位低位
SendChar(0x04);   //0x04 EOT --> end of transmission

}
这段是单片机间隔一段时间发送包,已在电脑端串口助手里测试过了,发送正常

while(1)
{
    delay_ms(1000);
    SendString("test1234");
}

然后接收包的java代码

    public void run()
    {
        Log.i(TAG, "BEGIN mConnectedThread");
        StringBuffer buffer = new StringBuffer();
        int SumCheck = 0, SumRec;
        int i = 0, j, dat;
        while (true)
        {
            try
            {
                int count = mmInStream.available();
                while (count < 12)
                {
                    count = mmInStream.available();
                }
                int SOH = mmInStream.read();
                Log.i(TAG, "SOH=" + SOH);
                while (SOH != 1)
                {
                    i++;
                    if (i >10)
                    {
                        break;
                    }
                    SOH = mmInStream.read();
                    Log.i(TAG, "SOH=" + SOH);

                }
                count = mmInStream.available();
                if ((i < 11) && (count <=11))
                {
                    while (count < 11)
                    {
                        count = mmInStream.available();
                    }
                } else
                {
                    continue;
                }
                for (j = 0; j < 8; j++)
                {
                    dat = mmInStream.read();
                    buffer.append(dat);
                    SumCheck += dat;
                }
                SumRec = mmInStream.read();
                SumRec = (SumRec << 8) | (mmInStream.read());
                Log.i(TAG, "SumCheck=" + SumCheck + "\nSumRec" + SumRec);
                if ((SumCheck == SumRec) && (mmInStream.read() == 4))
                {
                    String s = buffer.toString();
                    byte[] bits = s.getBytes("UTF-8");
                    mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, 8, -1, bits).sendToTarget(); // 送入消息池
                    Log.i(TAG, "buffer=" + s);
                }
                buffer.delete(0, 7);
                SumCheck = 0;
                i = 0;
            } catch (IOException e)
            {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }

(其他部分的代码参考了android example API-9 bluetoothchat)
但是这段按照包头(1)+数据(8)+校验(2)+包尾(1)来接受校验数据,运行不正常
logcat输出如下

05-16 23:31:21.360: D/BluetoothChatService(17686): create ConnectedThread
05-16 23:31:21.360: I/BluetoothChatService(17686): END mAcceptThread
05-16 23:31:21.400: D/BluetoothChatService(17686): setState() 2 -> 3
05-16 23:31:21.400: I/BluetoothChatService(17686): BEGIN mConnectedThread
05-16 23:31:21.410: I/BluetoothChat(17686): MESSAGE_STATE_CHANGE: 3
05-16 23:31:21.480: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.490: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.490: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.490: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.490: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.490: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.500: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.500: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.500: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.530: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.540: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.550: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.560: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=51
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=52
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=2
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=138
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=4
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=1
05-16 23:31:21.570: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=101
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=115
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=116
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=49
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=50
05-16 23:31:21.580: I/BluetoothChatService(17686): SOH=51

logcat到此停了。。。内容可能有些多,但跪求各位帮忙看看,问题出在哪儿

  • 写回答

13条回答

  • 我会写BUG 2014-05-17 06:04
    关注

    问题解决了,最重要的是被自己误导了。mmInStream.available()获取的可读字节数并不是真实的蓝牙串口端接收到的量,在调试过程中,单片机发12bit的包,而mmInStream.available()只能检测到7bit。

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况