How to setup bluetooth in Linux with bluez and hcitool
Bluetooth devices are quite ubiquitous today, found almost everywhere, simple and cheap way to add wireless connectivity to a device.
A Linux system could use many protocol like OBEX, A2DP, DUN, HID etc. etc. to interact with different devices for different purpose. So how to connect, pair and manage those bluetooth devices from a Linux PC, let’s start.
Part 2 of this tutorial has been published, it’s here Setup bluetooth in Linux part 2
Most bluetoth adapters are USB based and could be configured with HCI utilities, some bluetooth devices like Atheros bluetooth adapters may require device firmware installed in the system.
To install required software packages on Debain or other Debian derivatives like Ubuntu, Linux Mint run the commands below.
sudo apt-get install bluetooth bluez bluez-tools rfkill rfcomm
The next may be necessary for proper functioning of the bluetooth adapter, you may want to replace firmware-atheros package with the firmware package that matches your device like firmware-realtek .
sudo apt-get install bluez-firmware firmware-atheros
Now start the bluetooth service
sudo service bluetooth start
Scanning for bluetooth devices
Before start scanning make sure that your bluetooth device is turned on and not blocked, you can check that with the rfkill command.
sudo rfkill list
If the bluetooth device is blocked (soft or hard blocked), unblock it with the rfkill command again
sudo rfkill unblock bluetooth
Bring up the bluetooth device with hciconfig command and start scanning, make sure the target device’s bluetooth is on and It’s discoverable.
sudo hciconfig hci0 up hcitool scan
Wait few moment to complete the scan, the results will be something like bellow
Scanning ... 83:23:26:15:54:46 Virus_X
Here 83:23:26:15:54:46 is the bluetooth MAC address and Virus_X is the name of the bluetooth device, i.e. a Nokia 5230 phone.
Bluetooth service discovery
Now we have the bluetooth MAC address of the target device, use the sdptool command to know which services (like DUN, Handsfree audio) are available on that target device.
sdptool browse 83:23:26:15:54:46
Check the output of the sdptool command, you may want to filter the result with the grep command.
sdptool browse 83:23:26:15:54:46 | grep 'Service Name:'
You can also use the interactive bluetoothctl tool for this purpose.
$ bluetoothctl [bluetooth]# info 83:23:26:15:54:46 Device 83:23:26:15:54:46 Name: Virus_X Alias: Nokia Class: 0x580204 Icon: phone Paired: yes Trusted: yes Blocked: no Connected: no LegacyPairing: no UUID: OBEX Object Push (00001105-0000-1000-8000-00805f9b34fb) UUID: Audio Source (0000110a-0000-1000-8000-00805f9b34fb) UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb) UUID: Handsfree Audio Gateway (0000111f-0000-1000-8000-00805f9b34fb)
Ping bluetooth devices: If the target device is present, you can ping it with l2ping command, requires root privilege
sudo l2ping <Target device MAC address>
sudo l2ping 83:23:26:15:54:46 #example
Connecting and Pairing the device
Connecting to the bluetooth device with rfcomm, this command requires root privilege, so use sudo .
sudo rfcomm connect <bluetooth host device> <Target bluetooth device MAC address> <channel>
An exaple of connecting bluetooth hot device hci0 to a client device on channel 2
sudo rfcomm connect hci0 83:23:26:15:54:46 2
The device/phone will prompt to accept this connection request. Now the bluetooth client device should be available as /dev/rfcomm0 .
Send file through OBEX/OPP to a remote bluetooth device, the first command is a generic example
sudo bt-obex -p <Bluetooth remote device mac address> /path/to/file
sudo bt-obex -p 83:23:26:15:54:46 ~/img/some_pic.png
Now receive some file from the remote device, an OBEX server example, first start bt-obex in server mode listening for bluetooth connection.
bt-obex -s /path/to/output/folder
Replace /path/to/output/folder with a folder of your choice, like /tmp . Now send some file from the bluetooth client device, i.e. a phone. The sent file should be in the bt-obex output folder.
Start a FTP session with bt-obex
sudo bt-obex -f <Remote device MAC address>
I tested those above commands with a Nokia 5230 and all functions are working fine except Handsfree audio. In the next tutorial about bluetoth I’ll discuss about how to setup a dial-up internet connection via bluetooth in Linux.
Managing bluetooth devices could be even more easier with graphical tools like bluedevil for KDE desktop, Gnome-Bluetooth for Gnome desktop or general purpose Gtk2 application blueman. So when these commands are useful ? For headless servers, machines accessed form SSH servers, or simply knowing what’s going on behind the scene.
If you have any suggestion or question just leave a comment, also don’t forget to share this with your friends.