Is there a way to query about the serial baud rate?

I am using an Arduino UNO R3 and i have this line in the setup function:

Serial.begin(9600);

But if the data speed is changed, i'm wondering if there is a function or hidden way of querying the baud rate used in the sketch? Like a standard command, for example:

Serial.baudrate();

which would return the baud rate currently used?

Use a variable instead of hard-coding it and then you can see what you set it to. It’s not going to just mysteriously change without you doing it.

int mySerialBaudRate = 9600;

// in setup

Serial.begin(mySerialBaudRate);

//wherever else

Serial.print(“Baud rate is: ”);
Serial.println(mySerialBaudRate);

If you look at the reference page for Serial, you'll see no mention of any such function, for the reason that Delta_G gave.

Of course, that doesn't mean it doesn't exist. Happily for you, you have the source code so you can go and look. Better yet, if it doesn't exist and you would like it to for some bizarre reason, you can customize your version to add that functionality.

Delta_G:
Use a variable instead of hard-coding it and then you can see what you set it to. It’s not going to just mysteriously change without you doing it.

int mySerialBaudRate = 9600;

// in setup

Serial.begin(mySerialBaudRate);

//wherever else

Serial.print(“Baud rate is: ”);
Serial.println(mySerialBaudRate);

That's useful. Thanks. But in my sketch, i'm trying to run an automated hardware check on the Bluetooth module HC-05 connected to my Arduino UNO R3. I'm trying to find a way of auto-checking if the Bluetooth module is working correctly. My suggested approach is to check if data is being sent and received correctly at the set baud rate. I am using a Serial Monitor app on my Android phone which uses Bluetooth and works just like the Arduino IDE Serial Monitor via USB.

One method could be simply turning on and off an LED?

#define ledPin 13
int state = 0;
void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
  if(Serial.available() > 0){ // Checks whether data is comming from the serial port
    state = Serial.read(); // Reads the data from the serial port
 }
 if (state == '0') {
  digitalWrite(ledPin, LOW); // Turn LED OFF
  Serial.println("LED: OFF"); // Send back, to the phone, the String "LED: ON"
  state = 0;
 }
 else if (state == '1') {
  digitalWrite(ledPin, HIGH);
  Serial.println("LED: ON");;
  state = 0;
 } 
}

The problem is that it requires user intervention to enter a value to toggle the LED, and it also requires the user to check the LED. If possible, i would like to automate this whole process, so that the Serial Monitor simply displays "OK" if the communication is working between the UNO and the HC-05 Bluetooth module. But maybe there is a better way of testing the Bluetooth module to check if it's working properly?
For example, to test my DC motor, i use a sketch which counts the encoder ticks and then displays the value to show that the motors are turning and the hardware check is complete.

Edit: Another method that i found is to bounce the data back and forth between the hardware and software serial ports to verify that the communication is working. But i don't want to have my Arduino UNO connected via USB, since i'm using Bluetooth communication to control the robot. And yet another method could be to use AT commands... But that would need tinkering with the hardware itself.

Oh so you don’t want to see what you set it to in your code you want to see what baud the other device is using. No, you can’t query that. You’ll have to do something like you suggest trying at different speeds until you get a good transmission.

Or just set your baud rate and leave it. It shouldn’t be magically changing itself. If you change it then you should know what you changed it to.

You can disable serial and measure the bits on the RX port.
Or, there are a bunch of "autobaud algorithms" where if you know the character you're expecting, you can derive the remote bitrate based on your own speed and the character you actually get...