How does the bluetooth module work

Bluetooth modules

Bluetooth module HC-05

← Home The Bluetooth modules presented here enable serial devices to be connected wirelessly to a PC or smartphone. The RFCOMM protocol from the SPP profile is used for this.

HC-05

The HC-05 from China is intended for 3.3V, but you can also buy it together with an adapter board for 5V. In my tests, it achieved a range of less than 5 meters with largely error-free and rapid data transmission. Even at a short distance, the connection to the PC or smartphone fails sporadically.

As long as the module has no radio connection, it can be configured (at the Rxd and TxD connections) with AT commands. After the connection has been established, it transmits serial data transparently to the connection partner.

The pin assignment is as follows:

  • 1 = TxD (output)
  • 2 = RxD (input)
  • 11 = input low = reset (not connected = high)
  • 12 = supply normal 3.3V 50mA (3..4.2V, short-term 250mA)
  • 13 = GND
  • 31 = LED 1, flashes continuously
  • 32 = LED 2, low when ready and high when connected.
  • 34 = input low = normal operation, high = command mode (not connected = low)

Pin 34 can be used to switch to command mode (even during a connection):

  • Low: The module can establish connections and execute some AT commands as long as it is not connected via radio. The baud rate set with AT + UART applies.
  • Change from low to high: The module goes into command mode. The baud rate set with AT + UART applies.
  • High while the power supply is switched on: The module starts in configuration mode, where it supports all commands. The baud rate in this mode is always 38400. To leave this mode, the power supply must be switched off and on again.

By default, the serial port is 9600 baud and the module is ready to accept connections from any master (PC or smartphone). The module can be configured with commands on the serial port. These are the most important commands for the slave mode:

  • AT + ORGL Reset all settings to the default values
  • AT + PSWD = 1234 Change password (default 1234)
  • AT + NAME = HC-05 Change name (default HC-05)
  • AT + UART = 9600,0,0 Change baud rate to 4800 to 1300000 (default 9600)
  • AT + CMODE = 1 Allow connection with any partner (default)
  • AT + CMODE = 0 Allow connection only with a specific partner (either the last known partner or specify with AT + BIND)
  • AT + BIND = A854, B2,3FB035 Link the module to the specified partner (parameter is the address but with a comma instead of colons)

Other commands are:

  • AT + ROLE? Query master / slave mode
  • AT + ADDR? Query your own address
  • AT + PSWD? Request password
  • AT + NAME? Request your own name
  • AT + CMODE? Query connection mode
  • AT + RMAAD Terminate current connection and paring (if available)
All commands are terminated with a line break (CRLF or \ r \ n). If you send several commands to the module one after the other, you have to give it enough time to execute the command after each individual command. The module cannot receive the next command until the previous command has been processed.

To set the module to master mode and connect it to a slave, use the following commands:

  • AT + ORGL Reset all settings to the default values
  • AT + ROLE = 1 Master mode
  • AT + PSWD = 1234 Set the same password as for the slave
  • AT + RMAAD End current paring (if available)
  • AT + CMODE = 0 Allow connection only with a specific partner
  • AT + BIND = A854, B2,3FB035 Link the module to the specified partner (parameter is the address but with a comma instead of colons)
  • AT + RESET Restart, the module searches for the slave and tries to connect.
  • AT + LINK = A854, B2,3FB035 Connect to the specified slave
After these commands, the module will automatically connect to the slave in the future. With AT + ROLE = 0 followed by AT + RESET you come back to the slave mode.

The following commands can be used to search for accessible slaves:

  • AT + CMODE = 1 Allow every connection partner
  • AT + INQM = 0.5.9 Search up to 5 devices in 9 seconds
  • AT + INIT If the module answers with ERROR (17): ignore and continue
  • AT + INQ? If the search starts, the address A854 returns, for example: B2: 3FB035
  • AT + RNAME? A854, B2,3FB035 Shows the name of a reachable partner

In some documentation commands are given that can be used to control individual I / O pins directly. However, this feature is obviously optional and not all firmware supports it.

Here is an English description of the HC-05 and HC-06, as well as the instruction set.

Bluetooth module HC-06

The little brother HC-06

The HC-06 module looks exactly like the HC-05. It is sold in two variants: master and slave, with very few dealers selling the master variant.
  • The slave variant HC-06-S waits for a master (computer or smartphone) to contact it.
  • The master variant HC-06-M automatically establishes a connection to any accessible slave and then remembers this for later connections.
The operating mode cannot be reconfigured. Externally, the modules look identical. However, the pin assignment of the HC-06 is slightly different than that of the HC-05:
  • 1 = TxD (output)
  • 2 = RxD (input)
  • 11 = input low = reset (not connected = high)
  • 12 = normal supply 3.3V 50mA (3..4.2V, short-term 250mA)
  • 13 = GND
  • 24 = Status LED, flashes when ready, high when connected.
  • 26 = input low = normal operation, high = deletion of the saved connection partner (only HC-06-M, not connected = low)

And the instruction set is also different. The following applies to firmware versions up to 2.x:

  • AT + BAUD1 Set the baud rate to 1200
  • AT + BAUD2 Set the baud rate to 2400
  • AT + BAUD3 Set the baud rate to 4800
  • AT + BAUD4 Set the baud rate to 9600 (= default)
  • AT + BAUD5 Set the baud rate to 19200
  • AT + BAUD6 Set the baud rate to 38400
  • AT + BAUD7 Set the baud rate to 57600
  • AT + BAUD8 Set the baud rate to 115200
  • AT + NAMExxxxxx Change device name to xxxxxx (default linvor)
  • AT + PINxxxx Set password (default 1234)
With the HC-06 the commands Not be terminated with a line break, otherwise it will simply ignore them!

From firmware version 3.0, however, the lines must be terminated with a line break (CRLF or \ r \ n). And the commands are different:

  • AT + BAUD = 9600,0,0 Set the baud rate to 9600
  • AT + NAME = xxxxxx Change device name to xxxxxx (default linvor)
  • AT + PSWD = "1234" Set password (default 1234)

Here is an English description of the HC-05 and HC-06, as well as the instruction set.

Bluetooth module BTM-222

BTM-222 and BTM-238

The module BTM-222 and its successor BTM-238 from Rayson both have a closed housing and create a good range with a simple piece of wire as an antenna. They work much more reliably than the HC-05 and HC-06 modules.

As long as the modules have no radio connection, they can be configured (at the RxD and TxD connections) with AT commands. After the connection has been established, they transmit serial data transparently to the connection partner.

You only connect a few pins:

  • 1 = GND
  • 2 and 17 = supply 3.3V 100mA (3..3.3V, short-term 300mA)
  • 13 = Status LED, flashes when ready and is high when connected.
  • 26 = RxD (input)
  • 27 = TxD (output)
  • 37 = antenna: 3cm or 12cm wire

The most important commands, they only work on the serial port:

  • ATP? Request password
  • ATP = 1234 Set password (default 1234)
  • ATP0 Deactivate password
  • ATN? Query device name
  • ATN = BTM-222 Change device name
  • ATL0 4800 baud
  • ATL1 9600 baud
  • ATL2 19200 baud (default)
  • ATL3 38400 baud
  • ATL4 57600 baud
  • ATL5 115200 baud
  • ATL6 230400 baud
  • ATL7 460800 baud
  • ATD? Query connection partner
  • ATD0 Allow any partner (default)
  • ATD = A854-B2-3FB035 Define connection partner
  • ATF? List accessible devices
  • ATR0 Master mode
  • ATR1 Slave mode (default)

Commands are terminated with a line break (CRLF or \ r \ n). If you send several commands to a module one after the other, you have to give it enough time to execute the command after each individual command. The modules can only receive the next command after the previous command has been processed.

Status messages such as CONNECT '001D-D9-F71C7E', DISCONNECT '001D-D9-F71C7E' and AT + GCAP.

Data sheets: BTM222 pins and instruction set, BTM238 pins, BTM238 instruction set

Numerous exciting features are mentioned in the data sheets, such as programmable I / O pins, USB interface, voice functions, etc. However, all these functions are not supported by the normal "SPP" firmware.

Programming notes

Use on Windows

Switch on the power supply, click on the Bluetooth symbol in the start bar. You have to enter the password of the module and a virtual serial port will be installed.

Then you can use the connection, e.g. with a terminal program. Windows does not establish the wireless connection until the virtual serial port is opened, and the connection is disconnected again when the serial port is closed.

Unfortunately, the Windows Bluetooth drivers are often inadequate. It happens again and again that the virtual COM port is missing or cannot be opened. It only worked for me after I installed the Widcomm driver, which I could download from the support page of my notebook manufacturer. Interestingly, all the other drivers were preinstalled, except this one.

To address the module with a Java program, you need the RxTx Library, preferably version 2.1.7 (alternative download).

For initial tests, I recommend the Hammer Terminal, Putty or Cutecom programs.

Use on Linux

After you have set up the pairing with the Bluetooth device in the control panel, use the command hcitool scanto find out its device address and then rfcomm bind 0
to create a virtual serial port. Because of the 0, the serial port is called / dev / rfcomm0.

To address the module with a Java program, you need the RxTx Library, preferably version 2.1.7 (alternative download). First tests can be carried out very well with Cutecom.

Linux without a graphical user interface

Since I don't have a desktop installed on my Raspberry Pi, I have to perform the pairing on the command line. It works like this:
$ sudo su - $ systemctl start hciuart (does not seem to be necessary for all devices) $ hcitool scan Scanning ... 20: 13: 11: 15: 16: 08 HC-06 $ bluetoothctl [bluetooth] # power on [bluetooth] # agent on [bluetooth] # pair 20: 13: 11: 15: 16: 08 Attempting to pair with 20: 13: 11: 15: 16: 08 [CHG] Device 20: 13: 11: 15: 16: 08 Connected: yes Request PIN code [agent] Enter PIN code: 1234 [CHG] Device 20: 13: 11: 15: 16: 08 UUIDs: 00001101-0000-1000-8000-00805f9b34fb [CHG] Device 20: 13: 11: 15: 16: 08 ServicesResolved: yes [CHG] Device 20: 13: 11: 15: 16: 08 Paired: yes Pairing successful [bluetooth] # exit $ rfcomm bind 0 20: 13: 11: 15: 16: 08 $ picocom -b115200 / dev / rfcomm0

Use on Android

The Android SDK contains special classes for Bluetooth communication, which provide an InputStream and an OutputStream after the connection has been established. You don't get a virtual serial port with Android.

To establish a connection you need the UUID "00001101-0000-1000-8000-00805F9B34FB". The sample program "Bluetooth Chat" from the Android SDK only works after adjusting the UUID in the source code.

You are welcome to copy from my minimal sample app BtTest, how to use the Bluetooth classes of the Android SDK.

Those who program apps in C ++ can address the Bluetooth API via the Java Native Interface (JNI). The Qt application AndroidBluetooth demonstrates how this is done. From version 5.3 Qt Bluetooth supports the same technology. Still, some programmers prefer my example because it supposedly works more reliably (I couldn't understand).

Use on iOS

Unfortunately, Apple's iOS devices do not support the RFCOMM protocol at all. Here you have to switch to Bluetooth Low Energy (BLE) products or WLAN, which, however, are programmed completely differently.