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

前端 未结 1 1716
醉梦人生
醉梦人生 2021-01-27 08:57

So I asked this question a few days ago, but maybe I can elaborate a bit more, or in a different way now. I am a big java and android newbie, so it takes a lot of time to figur

1条回答
  •  失恋的感觉
    2021-01-27 09:18

    Alright, I managed to figure it out, but forgot to update, here is my other post with a little bit more code: How can I assing the value of an incoming Bluetooth message to a byte array which I want to decode into integers?

    Here's what I changed: I only touched the run() method in my BluetoothConnectionService

    Instead of using the byte[] buffer, as I mention in the commented code, I declared a public static byte[] incomingBytes and gave it a size of 44, since this is what my 11 integer array going to need. Then I just replaced the "buffer" with "incomingBytes" in the example code, and looks like this:

    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;
                    }
                }
            }
    

    Then I only need to call incomingBytes for my convertion in the other class and it works fine.

    0 讨论(0)
提交回复
热议问题