Save Android Studio java Array

Asked 2 years ago, Updated 2 years ago, 45 views

public void handleMessage(android.os.Message msg){
                Timer buttonTimer = new Timer();

                if(msg.what == BT_MESSAGE_READ){
                    String readMessage = null;
                        try {
                        readMessage = new String((byte[]) msg.obj, "UTF-8");
                    } } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    String[] readMessage1;
                    String[] array_word = new String[64];
                    readMessage1 = readMessage.split("");
                    int j;

                    for(int i=0;i<readMessage1.length;) {
                        if(readMessage1[i]=="7") {
                            i++;
                        }
                        j=0;
                        array_word[j]=readMessage1[i];
                        j++;
                        i++;
                    }
    ~~~~~~~~

You want to create a source that separates 64 values sent from Arduino with a String variable called readMessage.

Since we don't know the beginning and the end, we send '7' when we randomly start at Arduino, and '5' when we finish. So there are 64 values of 1 and 0 between 7 and 5. It's fixed.

To judge this, we created an array called readMessage1 and tore the readMessage first.

Then we create an array called array_word, and in readMessage1, we first increase the i value to find 7. If you find 7, you want to create a function that increases the i value by 1 and puts the value from array_word[0].

But when I did that, it bounced...

Please give me some advice.

android studio java string

2022-09-21 22:53

1 Answers

CAUTION: When communicating with an embedded, you should calculate when no data comes at once.

Start byte + endpoint byte + 64 data, or 66 bytes, can come in two or three separate ways.

    List<Byte> readMessageFromAduino = new ArrayList<>();
    private static final byte START_OF_FRAME = '7';
    private static final byte END_OF_FRAME = '5';

    @Nullable
    public List<Byte> parseByteArr(byte[] inputData){
        List<Byte> parsingByteData = new ArrayList<>();
        int startOfFrameIndex = -1;

        for (byte b : inputData) {
            // Consolidate incoming data into one place.
            readMessageFromAduino.add(b);
        }

        if (readMessageFromAduino.size() < 66) {
            // Not gathering enough data.
            return null;
        }

        for(int i = 0; i < readMessageFromAduino.size(); i++) {
            if (readMessageFromAduino.get(i) == START_OF_FRAME) {
                // Search for StartOfFrame (SOF).
                startOfFrameIndex = i;
                break;
            }
        }

        if (startOfFrameIndex == -1) {
            // SOF not found
            return null;
        }

        if (readMessageFromAduino.size() < startOfFrameIndex + 66) {
            // No data collected from SOF to EOF.
            return null;
        }

        if (readMessageFromAduino.get(startOfFrameIndex + 65) != END_OF_FRAME) {
            // Sufficient number of data collected and SOF was detected, but no EOF came out
            // Debugging required, additional action required for the current readMessageFromAduino list
            // // log(readMessageFromAduino)
            return null;
        }

        // All data validated //

        for (int i = 0; i < 64; i++) {
            // Data Replication
            parsingByteData.add(readMessageFromAduino.get(i + startOfFrameIndex + 1));
        }

        // Delete replicated data
        readMessageFromAduino.removeAll(parsingByteData);

        return parsingByteData;
    }
 // Use it like this
    public void handleMessage(android.os.Message msg){
                Timer buttonTimer = new Timer();
                List<Byte> parsedByteArr = parseByteArr(msg.obj);
                if (parsedByteArr != null) {
                    // // Validated data is available.

                }
    }
    ~~~~~~~~

Additionally


2022-09-21 22:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.