Android的蓝牙一次发送的连接数据 [英] Android Bluetooth sending concatenated data at once

查看:514
本文介绍了Android的蓝牙一次发送的连接数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用蓝牙聊天应用code到两款Android设备之间传送数据。
但是,code发送的连接数据。
例如:当我尝试发送数据象下面这样:

I am using Bluetooth Chat App code to transfer data between two android device. But the code is sending concatenated data. For example: When I try to send data like below:




  1. C

  2. D

它的某个时候以这种形式发送数据:

It sometime send data in this form:



  1. BC

  2. D

下面是我写code发送数据:

Here is my write code for sending data:

public synchronized void write(byte[] buffer) {
        try {
            mmOutStream.write(buffer);

            mHandler.obtainMessage(BluetoothHelper.MESSAGE_WRITE, -1, -1, buffer)
                    .sendToTarget();
            mmOutStream.flush();
        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

也是我读code是:

Also my read code is:

if (mmInStream.available()>0){
                    bytes = mmInStream.read(buffer);
                    mHandler.obtainMessage(BluetoothHelper.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                }else {
                    SystemClock.sleep(100);
                }

我试着冲和睡眠,但他们不修复问题。请帮助。

I tried flush and sleep but they dont fix problem. Please help.

推荐答案

级联正在发生是由于两个不同的线程的执行速度差。

The concatenation is taking place due to difference in execution speed of two different threads.

一个是UI线程(你的Andr​​oid应用程序),即,其中超过正在查看蓝牙接收的数据。在UI线程刷新是以下行。

One is the UI thread (you android application), i.e. where the data received over Bluetooth is being viewed. The UI thread refresh is by following line.

mHandler.obtainMessage(BluetoothHelper.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();

和第二个线程是一个收集数据接收通过蓝牙链接。在code接收通过蓝牙的数据是:

And the second thread is the one collecting the data being received over Bluetooth link. the code receiving the data over Bluetooth is:

mmInStream.read(buffer);

这是一个阻塞调用,即只有当收到通过蓝牙东西返回。

This is a blocking call, i.e. returns only when something is received over Bluetooth.

UI线程比较慢于接收通过蓝牙线程的数据刷新视图。这意味着通过UI刷新视图时,下一次,一个以上的字符可能对BT阅读线程已经收到的时间。因此,它显示的数据是级联。而这种串联将在随机时间,用随机数字符为连接在一起。

The UI thread is refreshing the view comparatively slowly than the data being received over Bluetooth thread. It means by the time UI view is refreshed next time, more than one characters may have already received on BT reading thread. Hence it appears the data is concatenated. And this concatenation will be at random times and with random number characters being concatenated.

要解决这个问题:

1)在传输:发送后跟一个分隔符字符(或数据包)

1) While transmitting : Transmit character (or a packet) followed by a delimiter character.

2)虽然接受:只得出结论认为,后跟一个分隔符字符(或数据包)收到后,将消息发送到用户界面。这意味着你将有累积接收字符,每一次验证这些累积的缓冲区中的阻塞调用mmOutStream.write后(缓冲)的回报。

2) And while receiving : Send the message to UI only after concluding that a character (or a packet) followed by a delimiter is received. That means you will have to accumulate the receiving characters and verify these accumulated buffer each time after the blocking call mmOutStream.write(buffer) returns.

<一个href=\"http://stackoverflow.com/questions/24019585/bundling-inputstream-read-messages/24035342#24035342\">Have看看这个问题和答案提供在其中。

这篇关于Android的蓝牙一次发送的连接数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆