Setup bluetooth in Linux part 2 – hcitool, bluez and rfcomm

You can do much with bluetooth devices in Linux with the hcitool, bluez and rfcomm commands. Such as audio source and sink, serial communication over bluetooth, dial up networking and so on.

In a previous tutorial, I wrote about basic bluetooth setup in linux, like scanning, pinging, connecting and pairing bluetooth devices. Also did some file transfer with the bt-obex command, you can find it here.

So in this tutorial I’ll show you how you can do the things mentioned above, lets get started …

 

Quick check

Assuming you’ve installed everything to get bluetooth working in Linux, if not check the link above.

Just to make sure that the bluetooth device is not blocked by rfkill.

 sudo rfkill unblock bluetooth 

Now be sure that the bluetooth service is running.

 sudo systemctl status bluetooth.service  

Only if bluetooth service is not running, start it.

 sudo systemctl start bluetooth.service 

Now bring up the bluetooth device and start scanning your devices.

 sudo hciconfig hci0 up

hcitool scan 

Note the bluetooth MAC address of the target device and copy it to a easily accessible file, like a text file. You’re going to need that later.

 

Bluetooth service discovery

This step is crucial to know what you can do with your bluetoth enabled devices.

You can do this with the sdptool command, which is used for performing SDP queries on Bluetooth devices.

 sdptool browse E4:5D:75:7C:DC:B3 

The output result is huge, so you’ve to filter the result to easily know available services and their associated bluetooth channels.

 sdptool browse E4:5D:75:7C:DC:B3 | grep -E 'Service Name:|Channel:' 

 

Bluetooth rfcomm audio gateway

If you want to use your Linux laptop/desktop as a bluetooth audio gateway, you’ve to find the correct rfcomm channel with the sdptool command.

In my case, the channel is 9, I can connect to a phone(Samsung Z3) with the rfcomm command, it needs root privilege.

 sudo rfcomm connect E4:5D:75:7C:DC:B3  9 

You can see the phone as input source in PulseAudio Volume Control (pavucontrol)

bluetooth rfcomm audio source pavucontrol

You can also get list of active connection with the hcitool command,

hcitool con 
Connections:
        > ACL E4:5D:75:7C:DC:B3 handle 21 state 1 lm SLAVE AUTH ENCRYPT 

You can connect bluetooth headphones or wireless speakers in the same approach, a more detailed tutorial about this coming soon.

 

Linux Dial-up networking via bluetooth

Though dial-up networking is not very popular these days, but sometime you may need to share your phone’s internet to PC through bluetooth.

The procedure is almost same for all. First you’ve to find out if the device supports bluetoth DUN profile or not, then the correct rfcomm channel.

Many recent phones dropped the DUN protocol and adopted the newer BNEP protocol, which is similar to LAN. So I’m using an old Nokia 5230 phone to test bluetooth DUN.

You have to bind the correct rfcomm channel to cherate the /dev/rfcomm0 device, in my case the channel is 15.

 sudo rfcomm bind 83:23:26:15:54:46 15 > /dev/null 2>&1 & 

Basically the above command is binding the channel 15 of the target device and forking the process to background, all outputs are redirected to /dev/null .

If the connection and password match is successful, you should see a rfcomm0 device under the /dev directory.

More details available here.

 

Serial communication over bluetooth in Linux

It’s exactly same as the Dial-up networking. You’ve to use rfcomm to bind the proper channel, that’s all.

It’s particularly useful for bluetooth serial modules like cc2541 or HM-10. A more detailed tutorials is coming soon about how to interface those BLE modules in Linux.

 

Bluetooth BNEP networking in Linux

As I’ve talked about the BNEP bluetooth protocol before, now a little more details.

Connecting two devices via BNEP doesn’t involves rfcomm, all the protocols are handled by bluez itself over the L2CAP protocol.

All you need to do is install latest bluez and latest NetworkManager, then you can add bluetooth connections.

Some screenshots below, add a bluetooth connection.

A bluetooth LAN connection showing on NetworkManager applet.

How networkmanager displays a bluetoth BNEP connection information.

 

That’s all for this tutorial, hope you liked it !

Share your suggestion and questions below in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *