简体   繁体   中英

How to return data from multiple echos between two activities?

I am writing an android app that communicates via UART with a sensor device. The device sends data to the phone based on a 4-character ASCII command formatted like so:

":"[char1][char2][Carriage_return] (For example, ":AB\\r")

I have two activities, CalculationActivity and UartActivity.

CalculationActivity needs to get three different sensor readings from UartActivity and perform certain calculations with them. For example,

CalculationActivity:

protected void onCreate(Bundle savedInstanceState){
    // blah, blah, blah...
    Intent i = new Intent(this, UartActivity.class);
    startActivityForResult(i, DATA_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == DATA_REQUEST) {
         if (resultCode == RESULT_OK) {
             string sensor_data_distance = data.getStringExtra("distance");
             //need these also:
             //string sensor_data_heading = data.getStringExtra("heading");
             //string sensor_data_elevation = data.getStringExtra("elevation");
             //...
             //parse the strings and perform calculation
             //...
         }
      }
   }
}

UartActivity sends the commands to the device. Upon receiving them, the device echos back the requested data and my RX echo handler catches it. For example,

UartActivity:

protected void onCreate(Bundle savedInstanceState){
    // setup and initialize UART
    String get_distance_command = ":DI\r"; //command for distance
    String get_heading_command = ":HE\r"; //command for heading
    String get_elevation_command = ":EL\r"; //command for elevation

    uartSendData(get_distance_command); //send command to TX handler

    //want to be able to send these other two:
    //uartSendData(get_heading_command);
    //uartSendData(get_elevation_command); 
}

@Override
public synchronized void onDataAvailable(){ //RX echo handler
    //blah, blah, blah...
    //get received bytes
    final String result_string = bytesToText(bytes);
    Intent i = new Intent();
    i.putExtra("distance", result_string);
    //want to be able to do this for the other two:
    //i.putExtra("heading", result_string);
    //i.putExtra("elevation", result_string);
    setResult(UartActivity.RESULT_OK);
    finish();
}

Hopefully, you can infer from the commented out lines of code what I am trying to accomplish here. Note that I am able to successfully get only one reading (distance, in this case), but not more than that (heading and elevation, in this case).

I considered starting the UartActivity three different times with each command but I don't really like that solution. I would rather only run the activity once, send the three commands, catch all of the echo responses, and pass them back to CalculationActivity. Is this even possible?

You are missing returnIntent in setResult.

Try replacing

setResult(UartActivity.RESULT_OK);

with

setResult(UartActivity.RESULT_OK, i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM