Android蓝牙将连续消息作为单独的对象发送

时间:2016-06-24 03:07:22

标签: java android bluetooth inputstream outputstream

我试图通过蓝牙从一台设备向另一台设备发送进度值(实际上包含进度值的哈希图),这样两台设备都可以显示相同的进度对话框值

因此,在发送方设备上,我将值包装在散列图中(因此我可以通过检查第一个元素来区分消息类型):

public static void sendProgress(Integer progress){
        HashMap<String,String> saveProgress = new HashMap<>();
        saveProgress.put("type", "saveProgress");
        saveProgress.put("progressValue", Integer.toString(progress));
        BTService.connectedThread.write(MainActivity.gson.toJson(saveProgress).getBytes());
    }

在接收方,我正在阅读消息流,如下所示:

        while (!this.isInterrupted()) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
                mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
        }

问题在于,因为它实际上并没有发送一个对象,它只是发送一个字节流,所以接收设备并不知道何时一个对象&#34;结束,下一个开始。

有没有办法重写我的读取输入流代码,以便它可以将每个哈希映射视为对象?

我知道在将json字符串转换为字节之前,我可以在json字符串的末尾添加某种字符,然后检查接收端的字符,但是我发送了大量数据整个我的应用程序,而不是必须重构一切

我尝试过的一件事是每当我在发送方设备上写字时添加一个终结符:

            //Set the termination character (#)
            byte[] stopBytes = "#".getBytes();
            byte[] tempArray = new byte[bytes.length + stopBytes.length];

            //Attach a terminator to the message
            //copy bytes into start of destination (from pos 0, copy bytes.length bytes)
            System.arraycopy(bytes, 0, tempArray, 0, bytes.length);
            //copy stopBytes into end of destination (from pos bytes.length, copy stopBytes.length bytes)
            System.arraycopy(stopBytes, 0, tempArray, bytes.length, stopBytes.length);

            Log.d(TAG, new String(tempArray));

            //Write the object with terminator attached
            mmOutStream.write(tempArray);

如果在hashmap中发送传感器数据之类的数据,我可以确认它是这样发送的:

{"gravX":"0.39","accZ":"9.929751","magZ":"-27.125","accY":"0.60594","magY":"10.5","gravY":"0.7","accX":"0.44547364","magX":"23.3125","gravZ":"9.7699995","type":"accData"}#

然后在接收端,我读取输入流的每个字节并检查终结符。如果找到了,则发送消息,否则继续阅读:

                // Read from the InputStream
                buffer[bytes] = (byte) mmInStream.read();
                // Send the obtained bytes to the UI activity
                if ((buffer[bytes] == '#')){
                    //Check for terminator (#) before sending the message/object
                    mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
                    bytes=0;
                } else{
                    bytes++;
                }

我在这个方法上遇到了一些错误,因为在某些情况下我收到的对象是这样的:

{"gravX":"0.39","accZ":"9.903406","magZ":"-26.3125","accY":"0.6394702","magY":"9.8125","gravY":"0.62","accX":"0.41912845","magX":"25.5625","gravZ":"9.78","type":"accData"}#}#ata"}

正如你所看到的那样,它没有正确地被终结器分裂,并且还会在最后添加一些其他垃圾。显然,当我尝试将其转换回接收端的可用hashmap时,这是一个问题。这些额外的角色来自哪里?

0 个答案:

没有答案