了解蓝牙rfcoom原始数据

时间:2016-01-11 17:01:42

标签: android bluetooth rfcomm bluetooth-socket

我正在尝试构建一个应用程序来读取使用rfcomm在蓝牙服务上发送的信息。 该设备是一个硬度测试仪(HT-6510A),不能发现有关设备数据格式的规格,我遇到了一个奇怪的问题,我已经理解了如何阅读这些信息。

01-11 17:47:28.940 11862-13447/joinstore.it.testhardness V/result: ��S
01-11 17:47:29.581 11862-13447/joinstore.it.testhardness V/result: ��S
01-11 17:47:30.211 11862-13447/joinstore.it.testhardness V/result: ��S
01-11 17:47:30.872 11862-13447/joinstore.it.testhardness V/result: ��S
01-11 17:47:31.513 11862-13447/joinstore.it.testhardness V/result: ��S
01-11 17:47:32.143 11862-13447/joinstore.it.testhardness V/result: ��T
01-11 17:47:32.794 11862-13447/joinstore.it.testhardness V/result: ��T

这是我从设备收到的数据,我不认为执行有什么问题,只需在稳定rfcomm连接后使用此线程。

    //After connection, handle data transfer
    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
//            readAndPublishRaw();
            readAndPublishString();
            Log.v("result", "Reading data ended.");
            setStatusText(-1);
        }

        void readAndPublishRaw(){
            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes returned from read()
            Log.v("result", "Start reading...");
            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI activity
                    Log.v("result", bytes + "");
                } catch (IOException e) {
                    break;
                }
            }
        }

        void readAndPublishString(){
            //String method, not useful in this case?
            try {
                BufferedReader r = new BufferedReader(new InputStreamReader(mmInStream));
                StringBuilder total = new StringBuilder();
                String line;
                Log.v("result", "Start reading...");
                while ((line = r.readLine()) != null) {
                    total.append(line);
                    Log.v("result", line);
                }
                Log.v("result", total.toString());
                //TODO publish read string to the view
            } catch (Exception e) {
                //
                try {
                    mmSocket.close();
                }catch (Exception ex){}
                Log.v(TAG, "exception reading data from service");
            }
        }

        /* Call this from the main activity to send data to the remote device */
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) {
            }
        }

        /* Call this from the main activity to shutdown the connection */
        public void cancel() {
            try {
                mmSocket.close();
                setStatusText(-1);
            } catch (IOException e) {
            }
        }
    }

你能告诉我有关如何正确解析这些原始数据的信息吗?我想我应该有一个浮动值的流,但我只是这个随机的东西。

1 个答案:

答案 0 :(得分:2)

建议如何首先获得一些可用的日志输出:

nullable byte

下一步应该是校准数据,即记下哪个输入/显示值产生什么原始数据。从那里,您可能会或可能无法推断实际编码。

除数据中的实际测量外,您的设备可能包含也可能不包含其他信息,例如表示电池电量不足。必须考虑这一点才能获得原始测量值。

如果一个数据值包含多个字节,则需要确定字节顺序(小端或大端)。

小型设备的浮点数据通常以固定点表示形式表示。有时候,尤其是如果还需要负数,则添加偏移量,以便

void readAndPublishRaw(){ byte[] buffer = new byte[1024]; // buffer store for the stream int bytes; // bytes returned from read() Log.v("result", "Start reading..."); // Keep listening to the InputStream until an exception occurs while (true) { try { // Read from the InputStream bytes = mmInStream.read(buffer); // Send the obtained bytes to the UI activity final StringBuilder sb = new StringBuilder(); sb.append("Received ").append(bytes).append(" bytes: "); for ( int i = 0; i < bytes; i++ ) { sb.append( Integer.toHexString(((int)buffer[i]) & 0xff) ).append(", "); } Log.v("result", sb.toString()); } catch (IOException e) { break; } } }

如果您发现realValue = rawValue * a + ca,那么您很好。因此,一旦您只能从上面完成的校准中关联两个不同的c s和相应的realValue s,您就有足够的数据来解决rawValuea的等式。

具有更多&#34; punch&#34;,嵌入式Linux设备的设备也可以使用常规IEEE floating point data。 - 并非小型嵌入式设备不能使用IEEE,但浮点数通常超过了要求,并且浮点仿真代码的复杂性(内存和CPU)更高。