如何获取从蓝牙传入消息返回的字节数组值并在将其显示在其他设备上之前对其进行编辑?

How can I get the byte array value returned from a bluetooth incoming message and edit it before displaying it on the other device?

所以我几天前问了这个问题,但也许我现在可以详细说明一下,或者现在以不同的方式。我是一个 java 和 android 的大新手,所以我花了很多时间来弄清楚这些东西。我有两个设备之间的蓝牙连接。我尝试使用传感器,一切正常。设备相互连接并相互发送传感器值。 但是,此传感器值是自动生成的。我想要的是从其中一个设备获取 DB 值,将它们转换为字节,将它们添加到字节数组并将该字节数组作为单个消息发送到另一个设备,它将反转该过程。我已经设置好一切,一切都应该如此,只有 1 个例外——我需要以某种方式将 incomingMessage 捕获为字节数组,以便我可以完成该过程。 我怎样才能得到 incomingMessage 的值(它应该是传输一个字节数组)并将它添加到我当时的另一个字节数组 "decoding"?

注释掉的那个是我试过并且正在运行的例子。

 if (mBluetoothConnection.incomingMessage != null) {
            //messageTemp = mBluetoothConnection.incomingMessage;
            msg = mBluetoothConnection.incomingMessage;

        }

没有注释掉的就是我要赋值给字节数组的那个:

  byte[] array = msg;

这是迄今为止我唯一想不通的事情。 我当前的问题是 "array" returns 空对象引用。 请帮我!我觉得我几乎连接了 2 座桥,每座桥上的油漆只相差一厘米就没问题了。

好吧,我设法弄明白了,但忘记更新了,这是我的另一个 post 代码: How can I assing the value of an incoming Bluetooth message to a byte array which I want to decode into integers?

这是我更改的内容: 我只触及了我的 BluetoothConnectionService

中的 运行() 方法

我没有使用 byte[] 缓冲区,正如我在注释代码中提到的那样,我声明了一个 public static byte[] incomingBytes 并将其大小设置为 44,因为这是我的 11 整数阵列将需要。然后我只是将示例代码中的 "buffer" 替换为 "incomingBytes",看起来像这样:

public static byte[] incomingBytes = new byte[44];

public void run(){
            //byte [] buffer replaced with incomingBytes
            byte[] buffer = new byte[44];  // this was in the example, but it is not used. It was replaced by incomingBytes, declared at the start of the class

            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                // Read from the InputStream
                try {
                    bytes = mmInStream.read(incomingBytes);
                    incomingMessage = new String(incomingBytes, 0, bytes);
                    Log.d(TAG, "InputStream: " + incomingMessage);
                } catch (IOException e) {
                    Log.e(TAG, "write: Error reading Input Stream. " + e.getMessage() );
                    break;
                }
            }
        }

然后我只需要在另一个 class 中调用 incomingBytes 进行转换就可以了。