Changing Baud Rate on the fly

Hi,

I'm working with Android Open Accessory Development kit. I'm trying to transmit some data to a receiver node using the Xbee protocol. That works fine. However, when connecting the Android device to the Arduino Mega board, the USB library sends all sorts of messages using Serial.print which the Xbee transmitter relays onto the receiver node, causing all sorts of errors on my command detection algorithm on that end.

This might be a newbie question, but would it be feasible to do something like this in the void loop() method?

void setup() {
  Serial.begin(115200);
  acc.powerOn(); 
  Serial.print("program starting\n");
  delay(2000);
}
void loop() {
  
  byte msg[4];
  if (acc.isConnected()) {
    int len = acc.read(msg, sizeof(msg), 1);
    if (len >= 5) {
      Serial.begin(9600);
      Serial.write(msg[1]); // messageType
      Serial.write(msg[2]); // no of parameters
      Serial.write(msg[3]); // parameter 1
      Serial.write(msg[4]); // parameter 2
      Serial.begin(115200);
    }
  }
}

That is, to change the baud rate on the fly to Xbee's baud rate, to avoid other messages being sent over the Xbee except those issued by my (Android) program?

That is, to change the baud rate on the fly to Xbee's baud rate, to avoid other messages being sent over the Xbee except those issued by my (Android) program?

Changing the baud rate will not prevent messages being sent of the XBee except those issued by my (Android) program. Doing so will prevent the other receivers from properly understanding the serial data.

You need to connect the XBee to a different set of (TX/RX) pins and use NewSoftSerial to talk to it.