Here is an .aia file for receiving data back from the device. I just tested this with an Arduino Uno and it works. My test phone is a Moto G Play XT1609 running Android 7.1.1. Haven't tested on other devices. I build the app to apk and then download and install onto phone for testing. Not using emulator or companion app.
ArduinoInterface_receive.aia (4.4 KB)
I added an initialize serial command to the initialize screen event to prevent an android "Error 3901: Serial was not initialized" popup. That block should probably be added to the last .aia i sent you too.
I think ideally the serial block should have an event handler for the buffer, i.e., "when buffer has data, do...". My aia checks the buffer once every 10 millisec for data. Probably not ideal, but I was rushing to get you a working example.
Here is my code running on the Arduino:
//variable for Serial input
int input = 0;
//Pins for LED
const int LED = 13;
// the setup function runs once
void setup() {
//Start the serial monitor at 9600 baud
Serial.begin(9600);
//Declare the LEDs to be outputs
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
}
//runs over and over again
void loop() {
//check if there's incoming data,
if (Serial.available() > 0) {
//if so, then read the incoming data.
input = Serial.read();
//if data, send the pin high
if (input == '1') {
digitalWrite(LED, HIGH);
//send back data to phone
Serial.println("4");
}
else if (input == '0') {
digitalWrite(LED, LOW);
Serial.println("5");
}
}
}
This program will send an Arduino pin HIGH/5V/on when "1" is received from serial line, and then send the number "4" back to the phone. It will send the pin LOW/0V/off when a "0" is received, and return "5".
Since you are maybe using some other device, there is also this thread, which further defines the default communication: Testing the next release of MIT App Inventor (nb183) - #6 by ewpatton
Looking at the physicaloid library sources, the defaults are 8 bits, 1 stop bit, no parity.
Also, there is an app in the play store called "Serial USB Terminal" by Kai Morich. With this app you can quickly and easily test your serial parameters from your phone to device to make sure it is working properly. For example, with the arduino connected running the above sketch, entering a "1" into this app returns a "4" on the terminal. That app allows you to fine tune your serial settings, including baud, data bits, parity, stop bits, and new line/carriage return conditions.
Hope this helps.