如何通过Android蓝牙串行RFCOMM将串行通信转储为文件 [英] How do I dump serial communication as file through Android bluetooth serial RFCOMM

查看:94
本文介绍了如何通过Android蓝牙串行RFCOMM将串行通信转储为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用通过桌面应用通过蓝牙接收串行通信.我无法控制该桌面应用程序在做什么.

My Android app receive serial communication over bluetooth from desktop application. I have no control over what that desktop application is doing.

使用Google BluetoothChat示例代码( https://github.com/googlesamples/android-BluetoothChat).我设法使其正常工作,并且在某种程度上,我可以简单地将字节放入我的android应用中并写入文件.

Using Google BluetoothChat sample code (https://github.com/googlesamples/android-BluetoothChat). I manage to make it working and to some extent I can simply took the bytes coming to my android app and write a file.

但是,代码功能类似于

1-Android收听

1 - Android listen

2-PC发送数据,Android接受连接读取数据并写入文件

2 - PC send data, Android accept connection read data and write to file

3-阅读时,Android遇到异常(客户端关闭连接?)并关闭连接

3 - While reading, Android encounter exception (client close connection?) and close the connection

4-回到第一,然后听接下来的几个字节

4 - Back to one and listen for next few bytes

我的问题是我需要知道该过程何时结束以处理文件并在UI中显示一些结果.

My problem is that I need to know when this process ends to process the file and display some result in the UI.

代码是这个文件

特别是

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

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

                // ==> This is where I wrote to file
                // ==> Standard Android Java code to write 
                // ==> to output file stream

                // Send the obtained bytes to the UI Activity
                // ==> Commented out because this can not be done here
                // ==> NEED TO BE DONE WHEN ALL DATA IS READ
                // mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
                //      .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }

预计Android RFCOMM或Bluetooth Communication中应该有一种方法可以表明客户端已经完成了所有通信的发送.

Expected there should be a way in Android RFCOMM or Bluetooth Communication to signify that client already finish sending all the communication.

以前,我尝试使用IoException来向UI发出信号,表明已完成从PC读取数据的操作.那是一场灾难,因为出于某种原因,异常只会在通信开始时发生.而且我不知道为什么我的代码仍然会继续写到文件之外.

Previously I try to use IoException as place to signal the UI that reading data from PC is done. That is a disaster because for some reason, exception only happen at the beginning of communication. And I have no idea why my codes still keep writing to file beyond that.

我要重复一遍,说我对PC/台式机应用程序没有任何控制权.因此,请勿提出任何涉及改变桌面应用程序操作或发送数据方式的建议.

I am going to repeat that I have no control whatsoever on the PC/Desktop application. So please don't suggest anything that involve changing how desktop app operate or send data.

需要在Android端处理此问题.

Need to handle this on Android side.

推荐答案

除非拥有规则和/或某种协议并开始解析,否则您永远无法确定远端已完成要发送的数据的发送接收到的数据就在您身边.

You can never be sure that the far end has finished sending the data it wanted to send unless you have rules and/or some kind of protocol and start to parse the received data on your side.

添加/创建带有标头和长度字段的简单协议,使您可以接收,而且知道在接收循环中会发生什么.

Add/create a simple protocol with a header and a length-field that allow you to receive, and moreover, know what to expect in your receive-loop.

根据经验;永远不要以为您会在一次读取中获得整个数据包.相反,在最终处理数据并提取符合规范的完整数据包"之前,先对接收到的数据进行缓冲并允许多次读取.

As a rule of thumb; never assume that you’ll get an entire packet in one read. Instead, buffer your received data and allow for multiple reads, before you eventually can process it and extract complete "packets" that conform to your specification.

这篇关于如何通过Android蓝牙串行RFCOMM将串行通信转储为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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