HC-05 serial communication doubt

I have a doubt, when working with serial communication with bluetooth.

Can I set up the module in AT mode, set configurations, and then end that connection and begin new one to work with inputs and do stuff?

What I want to do is to search available devices with AT+INQ command, select a device to link (I dont want to pair the module through my phone, I want to link HC-05 to my phone through serial monitor) , and then read data from BT terminal to turn on leds and other stuff.

Any tip or advice?

Example code to set AT Mode and input commands:

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(0, 1); // RX | TX

int count=0;
char val;

void setup()
{
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(9, HIGH);
  Serial.begin(9600);
  Serial.println("Enter AT commands:");
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
  Serial.println("AT");
  BTSerial.println("AT");
}

void loop()
{
  if ( BTSerial.available() > 0 ) 
  {
    static char input[64];
    static uint8_t i;
    char c = BTSerial.read();

    if ( c != '\r' && c != '\n' && i < sizeof( input ) - 1 )
      input[i++] = c;
   
    else
    {
      input[i] = '\0';
      i = 0;

      if ( !strcmp( input, "OK" ) )
      {
        Serial.println("Received OK");
      }
    }
  }
}

nparafioriti: Any tip or advice?

SoftwareSerial BTSerial(0, 1); // RX | TX

Yes. You are using the hardware serial pins for software serial, and therefore deserve all the grief you get. It sounds rather pointless, and the serial monitor is probably redundant, but you should be able to do what you want to do by controlling the power supply to the HC-05, thereby enabling you to get in and out of the config mode.

Yeah, but if y send digitalwrite LOW to pin 9, do i lost connection to device, or should i do

BTSerial.end(), and then BTSerial.begin(9600) set baudrate to 9600 or 57600 to send data and work with that?

By turning off the power to that pin the HC-05 automatically can send data and be read on the serial monitor? I tried that but after configuration with AT commands is done, the serial monitor shows this inputs:

???? / &%!

And shit like that

nparafioriti: (I dont want to pair the module through my phone, I want to link HC-05 to my phone through serial monitor) ,

How are you going to be able to do anything without pairing?

.

What I meant was that I dont want to manually go to my Phone, search for devices, select HC-05, put the psw, pair with it.

All done programmatically