About Me

My photo
I know the last digit of PI

Friday, January 02, 2015

Bluetooth COM port in Linux

Following tools are needed for dealing with bluetooth devices in Linux: bluetoothctl, hcitool, rfcomm, minicom

Pairing

Powering the bluetooth adapter and making it pairable
user@workstation:~$ bluetoothctl
[bluetooth]# power on
Changing power on succeeded

[bluetooth]# pairable on 
Changing pairable on succeeded
Search for new bluetooth devices - note the btaddress of the device (00:06:66:04:9E:4A). It is needed for all bluetooth commands
[bluetooth]# scan on
Discovery started
[CHG] Controller aa:bb:cc:dd:ee:ff Discovering: yes
[NEW] Device 00:06:66:04:9E:4A FireFly-9E4A

[bluetooth]# scan off
Discovery stopped
[CHG] Controller aa:bb:cc:dd:ee:ff Discovering: no
Retrieve some information about the device
[bluetooth]# info 00:06:66:04:9E:4A
Device 00:06:66:04:9E:4A
 Name: FireFly-9E4A
 Alias: FireFly-9E4A
 Class: 0x001f00
 Paired: no
 Trusted: no
 Blocked: no
 Connected: no
 LegacyPairing: yes
Optionally configure the device to be trusted - so if it automatically accepts generated PINs. However for legacy pairing manually entered PIN is required.
[bluetooth]# trust 00:06:66:04:9E:4A
[CHG] Device 00:06:66:04:9E:4A Trusted: yes
Changing 00:06:66:04:9E:4A trust succeeded
Configure the bluetoothctl to be default agent for dealing with PIN code.
[bluetooth]# agent on
Agent registered

[bluetooth]# default-agent 
Default agent request successful
Now pair with the device
[bluetooth]# pair 00:06:66:04:9E:4A
Attempting to pair with 00:06:66:04:9E:4A
[CHG] Device 00:06:66:04:9E:4A Connected: yes
Request PIN code
[agent] Enter PIN code: 1234
[CHG] Device 00:06:66:04:9E:4A UUIDs:
 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device 00:06:66:04:9E:4A Paired: yes
Pairing successful
Double check the pairing and quit
[bluetooth]# paired-devices 
Device 00:06:66:04:9E:4A FireFly-9E4A

[bluetooth]# quit

Discovering services and RFCOMM channels

For devices that support SDP - Service Discovery Protocol (UUID: 00000001-0000-1000-8000-00805F9B34FB) the following command will show available protocols and RFCOMM channels. The XX:XX:XX:XX:XX:XX is the btaddress of the device. Note that in the previos example the device 00:06:66:04:9E:4A do not support SDP protocol, so the sdptool command will produce empty result.
user@workstation:~$ sdptool browse XX:XX:XX:XX:XX:XX
Service Name: Object Push Profile
Service RecHandle: 0x1000f
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 2
  "OBEX" (0x0008)
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100

Creating links to the remote COM port

Retrieving the bluetooth adapter name on the local machine.
user@workstation:~$ hcitool dev
Devices
        hci0    aa:bb:cc:dd:ee:ff
The adapter is called hci0 Optionally the connection could be tested with the following command
user@workstation:~$ sudo rfcomm connect hci0 00:06:66:04:9E:4A 1
Creating symlink to bluetooth serial port
user@workstation:~$ sudo rfcomm bind hci0 00:06:66:04:9E:4A 1
Verifying that the connection is established
user@workstation:~$ sudo rfcomm -a
rfcomm0: 00:06:66:04:9E:4A channel 1 connected [tty-attached]
The serial (COM) port is available as /dev/rcomm0 Any terminal program can use it. With following command will configure rootooth bluetooth device to communicate with Roomba 520 at baud rate 115200
user@workstation:~$ sudo minicom -D /dev/rfcomm0
$$$
>CMD
U,115K,N
>AOK
---
>END

Removing the symlink

In order to remove the symbolic link to the serial port the following command must be executed:
user@workstation:~$ sudo rfcomm release hci0

Unpairing the device and removing pairing configuration

user@workstation:~$ bluetoothctl

[bluetooth]# disconnect 00:06:66:04:9E:4A
Attempting to disconnect from 00:06:66:04:9E:4A
Successful disconnected

[bluetooth]# remove 00:06:66:04:9E:4A
[DEL] Device 00:06:66:04:9E:4A FireFly-9E4A
Device has been removed

[bluetooth]# power off
Changing power off succeeded