In this article we will look at how to establish a serial connection with devices via bluetooth .
Device Pairing
We need to pair the device before starting the communication
You can pair the device using standard bluetooth GUI utilities or command line utilties.In this article we will look at command-line utilities to do so.
[BlueZ](https://apps.ubuntu.com/cat/applications/bluez/)
is the official Linux Bluetooth protocol stack
This package contains tools and system daemons for using Bluetooth devices.
- Install this package on your system.
sudo apt-get install bluez bluez-tools
- Get the Bluetooth adapter details on linux
The hciconfig command gives the Bluetooth adapter details.
#hciconfig hci0: Type: BR/EDR Bus: USB BD Address: 18:F4:6A:DE:EB:9D ACL MTU: 1021:8 SCO MTU: 64:1 UP RUNNING PSCAN ISCAN RX bytes:2492 acl:3 sco:0 events:99 errors:0 TX bytes:1421 acl:2 sco:0 commands:82 errors:0
hci0
is out bluetooth adapter name .
- Get the Bluetooth device Mac Address
The hcitool scan
command gives us the bluetooth device mac address and name
hcitool scan Scanning ... 98:D3:31:30:1A:BA HC-05
98:D3:31:30:1A:BA
is the mac address of HC-05 bluetooth module
- Pairing the devices
bluez-simple-agent
utility can be used to pair to the device with mac address 98:D3:31:30:1A:BA
using the adapter hci0
bluez-simple-agent hci0 98:D3:31:30:1A:BA RequestPinCode (/org/bluez/3772/hci0/dev_98_D3_31_30_1A_BA) Enter PIN Code: 1234 Release New device (/org/bluez/3772/hci0/dev_98_D3_31_30_1A_BA)
To remove a paired device enter the command
bluez-simple-agent hci0 98:D3:31:30:1A:BA remove
RFComm communication protocol
RF Comm protocol handles the transmission of information and control commands from one communication device to another communication device (such as from a computer to an accessory device) that is performed serially (one bit at a time).
The Bluetooth system uses radio frequency communication (RFComm) protocol to setup and coordinate the transfer of serial data.
RFCOMM is intended to cover applications that make use of the serial ports of the devices in which they reside.In the present application the Bluetooth Device used is HC-05 which communicates using UART protocol with devices.
RFCOMM only allows one connected client per channel and supports up to 60 simultaneous connections between two BT device
- Configure Rfcomm
First Step is to configure the
rfcomm
module on linux to communicate with our Bluetooth module
In /etc/bluetooth/rfcomm.conf
file make the following entry
rfcomm0 { bind no; device 98:D3:31:30:1A:BA; channel 1; comment "Serial Port"; }
98:D3:31:30:1A:BA
is the mac address of our device.
The “bind no” is important, otherwise it will try to automatically bind to device and not when the user wants.
- Start the connection
Type the command sudo rfcomm connect 0
to start the communication
#sudo rfcomm connect 0 Connected /dev/rfcomm0 to 98:D3:31:30:1A:BA on channel 1 Press CTRL-C for hangup
This will also create a serial device file /dev/rfcomm0
,though which we can communicate with the bluetooth device.
If you have errors, restart the bluetooth service and enter the above command again
Communication with Bluetooth Device
User graphical serial terminal like CuteCom
use the /dev/rfcomm0
device to communicate with the bluetooth device.
You can send and receive commands in ASCII or HEX format via serial terminal interface.
Various bluetooth devices will correspond to different serial device files though which we can communicate with them.