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 check that for details.
So in this tutorial I'll show you how you can do the things mentioned above, lets get started.
Contents
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:'
Linux bluetooth rfcomm audio gateway
Here's how you can use your Linux laptop/desktop as a bluetooth audio gateway for your smartphone or mp3 player.
For that 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)
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.
Linux bluetooth Serial communication
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.
Imre Gellert says
Hello Arnab ,
I have "No default controller available ". But I can scan bluetooth devices with hcitool scan :
"""
# bluetoothctl
Agent registered
[bluetooth]# power on
No default controller available
# hcitool scan
Scanning ...
5C:FB:7C:68:6F:2B JBL Flip 3 SE
"""
Andy says
Arnab
Many thanks for your excellent descriptions of bluetooth connectivity. My problem was I couldn't connect a bluetooth GPS dongle. Your guide helped a lot:
To get the MAC address of my GPS dongle:
bluetoothctl
I was then able to bind it to rfcomm0:
sudo rfcomm bind /dev/rfcomm0
I was then able to connect to rfcomm0 in my navigation software.
Blueman was not a lot of help. If only it could bind to a serial port instead of connecting it would be a lot more useful.
RogerCassares says
Hi! I have followed the steps but i need some help, please.
I am trying to communicating serial zenphone3 bluetooth with ubuntu 18.04. Here is y last doubt exit from command to select channel:
ubuntu@ubuntu-Inspiron-5447:/etc/bluetooth$ sdptool browse 10:7B:44:85:34:8A | grep -E 'Service Name:|Channel:'
Service Name: Headset Gateway
Channel: 2
Service Name: Handsfree Gateway
Channel: 3
Service Name: AV Remote Control Target
Service Name: Advanced Audio
Service Name: Android Network Access Point
Service Name: Android Network User
Service Name: SIM Access
Channel: 5
Service Name: OBEX Phonebook Access Server
Channel: 4
Service Name: SMS/MMS
Channel: 7
Service Name: OBEX Object Push
Channel: 6
Service Name: OBEX File Transfer
Channel: 20
Please, which would I use?
Great docs!
Arnab Satapathi says
I suspect direct serial communication to the phone via rfcomm is unavailable.
As smartphones use the BNEP protocol to share the internet.
Anyway you could try to add the SP protocol and scan again.
sdptool add SP
sdptool browse 10:7B:44:85:34:8A
RogerCassares says
Hello @Arnab!
Thanks for the answer!!
My target is to connect Android torque app to Ubuntu and get data from obdsim simulator.
I can do it on windows, and it was very easy. But i really want to use linux ubuntu and achieve the same result.
As ubuntu bluetooth uses RFCOMM /dev/rfcomm0 to listen the bluetooth, how I can redirect port or doing something do my Android Device echo and listening something from ubuntu without directly listen rfcomm?
May there is some docs or you cold knok something to guide my thoughts?
If windows does, I guess linux can do it also!
Please can you tell me something about?
I am for some days trying to find something and nothing works! 🙁
Arnab Satapathi says
Hey Roger, honestly I'm not familiar with the applications you're using.
But I think the the obdsim simulator on Windows provides some information about the bluetooth channel and some other useful information.
Let's say it uses the channel 15, so you could try to bind it with the rfcomm command.
sudo rfcomm connect hci0 [your BT MAC address] 15
hcitool cc [your BT MAC address]
That's the same method I was using few year back to setup dial up networking with old Nokia Symbian phones.
Which was a serial communication, the phone accepting AT cmmands to dial up.
I don't think I could help you more than that. Few links-
https://forums.intel.com/s/question/0D50P0000490KgzSAE/rfcomm-spp-help-needed?language=en_US
https://www.emcraft.com/som/stm32f7-212/using-bluetooth-serial-port-profile
gearhead says
Actually, with the newer versions of bluez (>=5.44), hcitool does not exist and this information is less than awesome, today... Cannot bring up a device which does not enumerate at boot, for example the BT radio on a Pi3 or PiZero (broadcom 43438). How do you do this with btmgmt or bluetoothctl and the later versions of blueZ?
Arnab Satapathi says
With
bluetoothctl
, hope it's still present.Thanks for informing, using Ubuntu LTS currently, packages are somewhat older. It's time to go back Debian testing again.
moha254 says
This is awesome