2 u014489366 u014489366 于 2014.05.16 23:37 提问

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个回答

u014489366
u014489366   2014.05.17 14:04

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

w593333979
w593333979 楼主 你还有源码吗?可以分享给我吗?593333979@qq.com
一年多之前 回复
aishuqiqi
aishuqiqi   2014.08.08 16:39

有android通讯源码吗?发我份学习下2359596552@qq.com,谢谢

w593333979
w593333979 你们有收到吗?可以发我一份吗?593333979@qq.com
一年多之前 回复
yiqurentianya
yiqurentianya   2015.05.30 22:52

楼主又是怎么解决数据接收的问题,我收到的数据总是乱七八糟的,可以发一份源码吗?

oLuShuiChenZuo
oLuShuiChenZuo   2015.09.06 23:28

楼主能发一份源码吗?最近遇到相同的问题被卡住了,915676313@qq.com

u013804008
u013804008   2015.10.22 10:22

楼主能发一份源码吗?最近做到这布被卡住了,409335447@qq.com

youyou_11
youyou_11   2015.11.24 10:28

楼主能发一份源码吗?最近做到这部分被卡住了,954809535@qq.com

jiangzeyin_
jiangzeyin_   2016.04.07 10:38

大神,可以发一份接收数据的源码吗,1914796119@qq.com,谢谢了

crowncrowncrown
crowncrowncrown   2016.05.18 01:16
qq_34904091
qq_34904091   2016.09.07 17:39

大神,可以发一份接收数据的源码吗,1067738627@qq.com,谢谢了

qq_25168835
qq_25168835   2016.11.03 15:23

有源码的大神,可以发一份吗,709407326@qq.com,谢谢了

共13条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!