捆绑inputStream.read消息 [英] Bundling inputStream.read messages

查看:195
本文介绍了捆绑inputStream.read消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的<一个href=\"https://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat/src/com/example/android/BluetoothChat\"相对=nofollow> Android的蓝牙聊天样品 code聊天与蓝牙设备(发送命令+接收响应)。它工作正常,当我发出命令,我收到传递给信息处理设备的响应。我遇到的问题是被切成碎片的响应。

I am using the Android Bluetooth Chat sample code to chat (send commands + receive responses) with a bluetooth device. It is working fine when I send a command, and I receive a response from the device that is passed to message handler. The issue I am having is the response is cut up into pieces.

示例:

我发送字符串{灯:ON} \\ n和我的活动中正确显示我:{灯:ON} 。该器件指示灯亮起,它返回它的响应{FLash_Lights:ON} \\ n。但是我的活动显示设备名称:{佛罗里达州,新的生产线,设备名称:sh_Lights:ON} (或一些变化等) 。

I send the string "{Lights:ON}\n" and my activity correctly displays Me: {Lights:ON}. The device lights turn on and it returns it's response "{FLash_Lights:ON}\n". However my activity displays DeviceName: {Fla, new line, DeviceName: sh_Lights:ON} (or some variation ect).

现在我新的线程和一般的蓝牙,但我已经查明的问题倒在连接线类(在bluetoothchatservice),更具体的公共无效的run()听传入字节。

Now I am new to threading and bluetooth in general but I have traced the issue down to the connected thread class (in bluetoothchatservice), more specifically the public void run() to listen to incoming bytes.

     public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[9000];
            int bytes;

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);

                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(BluetoothActivity.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    // Start the service over to restart listening mode
                    BluetoothThreading.this.start();
                    break;
                }
            }
        }

我明白,这是为了监听进来的(作为聊天服务)的任何字节。不过,我确切地知道我写的设备,我知道我应该接收响应,我希望做的是捆绑其发送回mHandler在此之前的回应。

任何帮助,这将是惊人的! 谢谢!

Any help with this would be amazing! Thank you!

注意:我知道,使用此示例作为发送命令到设备和接收响应的方式是有点大材小用(尤其是因为我确切地知道我要送,需要之后直接接收)。这将是巨大的,可以指出你可能知道,以及任何简化样本。在这一天我只需要搜索结束,连接设备,按该按钮,发送字节[]到设备,接收字节[]响应,打印+存储。

Note: I know that using this sample as a way of sending a command to a device and receiving a response is a bit overkill (especially since I know exactly what I'm sending and need to receive directly afterwards). It would be great to be pointed to any simplified sample you may know of as well. At the end of the day I just need to search, connect to device, push button to send byte[] to device, receive byte[] response, print + store it.

推荐答案

在'阅读()时,通过蓝牙接收几个字节阻止呼叫并返回。的'少数'计数是不固定的,这就是为什么进来的反应似乎被打破成片的原因。因此,要获取完整的答复,所有这些作品,必须结合在一起。

The 'read()' is blocking call and returns when few bytes are received over bluetooth. The count of 'few' is not fixed, and that's the reason why incoming response appears to be broken into pieces. Therefore to retrieve complete response, all these pieces must be joined together.

当响应流端接一个特定的字符,然后按照code逻辑可以累积终止字符之前抵达的字节数。一旦结束符被检测,累积的响应被发送到主活性。 (请注意,在下面的code片段,所用的终止字符是0x0A的)。

Where the response stream is terminated with a specific character, then following code logic can accumulate the bytes arriving before the terminating character. And once terminating character is detected, the accumulated response is sent to main activity. (Note that in the code snippet below, the terminating character used is 0x0a).

public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[9000];
int bytes;
ByteArrayOutputStream btInStream = new ByteArrayOutputStream( );

// Keep listening to the InputStream while connected
while (true) {
    try {
        // Read from the InputStream
        bytes = mmInStream.read(buffer);

        btInStream.write(Arrays.copyOf(buffer, bytes) );

        if (buffer[bytes - 1] == 0x0a){
            mHandler.obtainMessage(MainActivity.MESSAGE_READ, btInStream.size(), 0,btInStream.toByteArray());
            .sendToTarget();
            btInStream.reset();
        }
    } catch (IOException e) {
        Log.e(TAG, "disconnected", e);
        connectionLost();
        // Start the service over to restart listening mode
        BluetoothThreading.this.start();
        break;
    }
}
}

另外请注意,code未经测试,但建议作为一种方法来解决问题中提及的问题。垫是它需要在几个地方更正。请测试并告知结果。

Also note that code is not tested but proposed as one approach to resolve the issue mentioned in the question. Mat be it will need corrections at few places. Please test and inform the results.

这篇关于捆绑inputStream.read消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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