Set the bt module name with connected device name

I was wondering if someone can please tell me how to set the bt module name to the name od the device connected to it?
I brlirve that in order to get the name of the device, i would do the following:

AT+RNAME

But I don't know how to set the module's name with arduino code.
Can someone please tell me how?
I am using an hm10 bt module.
Thanks.

What research have you done? What code have you tested?
(Read about AT commands and serial ports)

I tried:

#include <SoftwareSerial.h>

SoftwareSerial blueToothSerial(0,1); // RX, TX

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("Serial began");
  blueToothSerial.begin(9600);
}

void loop()
{
  if (blueToothSerial.available())
  {
    blueToothSerial.write("AT+NAME=AT+RNAME");
  }
}

I want to set the name of the bt module equal tp the name of the bt device connected to it.
I am using an arduino nano and an hm 10.

AT+NAME=AT+RNAME

If this works at all it will set the name to "=AT+RNAME" or "AT+RNAME", depending on the BT module you are using.

You need to use the ACTUAL NAME that you want it to be. If you want it to equal the remote name, then you should get the remote name, create a string with the AT command and remote name in it and write that to the BT module.

I found this post in the internet:

And i was wondering if i have to do the same in the circuit and in the code to switch the arduino to at mode, since what they connect is the key pin to pin 9 in the arduino and send a high to change it to At mode.
Since i am using a different module than the one used in the instructions (hm10), I don't know if i have to take the same approach.
Any further help will be greatly appreciated.
Thanks.

There is just one problem with that approach, the thing is that from what I read the hm 10 ble module turns into at mode when disconnected from bt device, so I can't get the name using this AT command:

AT+RNAME

Do you know if there is any other way I can get the name of the device connected to the arduino?
Thanks.

would it be possible to get only the name rom the connected bt device, using the state pin?
Can someone please show me where should I connect the state pin?
Thanks.

HM-10 is in AT command mode when it is disconnected from the master. When it is connected it cannot be wet up.

There is no need for any hardware switching with the HM-10. this is very different from the HC-05.

You should find and download a data sheet for the HM-10 that will give you all the AT commands you can use with the device. Google "HM-10 datasheet pdf".