Search code examples
androidcarduinoadk

invalid conversion from 'byte*' to 'byte'


invalid conversion from 'byte*' to 'byte'

i have write this arduino function

byte receiveMessage(AndroidAccessory acc,boolean accstates){
    if(accstates){
        byte rcvmsg[255];
        int len = acc.read(rcvmsg, sizeof(rcvmsg), 1);
        if (len > 0) {
            if (rcvmsg[0] == COMMAND_TEXT) {
                if (rcvmsg[1] == TARGET_DEFAULT){
                byte textLength = rcvmsg[2];
                int textEndIndex = 3 + textLength;
                byte theMessage[textLength];
                int i=0;
                    for(int x = 3; x < textEndIndex; x++) {
                        theMessage[i]=rcvmsg[x];
                        i++;
                        delay(250);
                    }
                return theMessage;
                delay(250);
                }
            }
        }
    }       
}

this is the error

In function byte receiveMessage(AndroidAccessory, boolean) invalid conversion from byte*' to 'byte"

this function is to receive the data from the android and return it as a byte array


Solution

  • You need to use dynamic allocation, or pass the array to the function as a parameter which is a better solution in your case

    void receiveMessage(AndroidAccessory acc, boolean accstates, byte *theMessage){
        if (theMessage == NULL)
            return;
        if(accstates){
            byte rcvmsg[255];
            int len = acc.read(rcvmsg, sizeof(rcvmsg), 1);
            if (len > 0) {
                if (rcvmsg[0] == COMMAND_TEXT) {
                    if (rcvmsg[1] == TARGET_DEFAULT){
                    byte textLength = rcvmsg[2];
                    int textEndIndex = 3 + textLength;
                    int i=0;
                        for(int x = 3; x < textEndIndex; x++) {
                            theMessage[i]=rcvmsg[x];
                            i++;
                            delay(250);
                        }
                    return;
                    }
                }
            }
        }       
    }
    

    with this, you will call the function passing the array to it, for example

    byte theMessage[255];
    
    receiveMessage(acc, accstates, theMessage);
    /* here the message already contains the data you read in the function */
    

    But you can't return a local variable, because the data is only valid in the scope where the variable is valid, in fact it's invalid right outside the if (rcvmsg[0] == COMMAND_TEXT) block, because you defined it local to that block.

    Note: please read Wimmel's comment, or you could set the last byte to '\0' if it's just text, and then use the array as a string.