PCsuggest

  • Quick tip
  • SECURITY
  • NETWORKING
  • OpenWrt
  • HARDWARE

Linux bluetooth setup with bluez and hcitool

Last Updated on December 31, 2017 by Arnab Satapathi 7 Comments

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 is published, Setup bluetooth in Linux part 2

Initial Linux bluetooth setup

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

hcitool scan 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 hcitool 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

hcitool connect and pair devices

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> <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.

Below how to start a FTP session with the bt-obex command.

sudo bt-obex -f <Remote device MAC address>

Conclusion

I tested those above commands with two phones, SANSUNG Z3 and 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, single board computers like Raspberry Pi or machines accessed form a SSH server, 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.

Filed Under: hardware, how to Tagged With: bluetooth, command line, hardware, Ubuntu

Comments

  1. miguipda says

    March 11, 2016 at 6:42 pm

    And howto use Bluetooth on OpenWRT to allow using it as a sound server that use many remote Bluetooth speakers (in many rooms) ?

    And which Bluetooth dungle could be buy on Gearbest.com or Baangood.com ?

    Reply
    • Arnab says

      March 11, 2016 at 7:04 pm

      Great suggestion, I've this in my mind over 1 year, but currently I don't have any bluetooth dongle/bluetooth speakers or the money to buy them. I'll be very happy if you share your current mail address, I never spam and respect everyone's privacy. Just mail to >> as.work0(at)gmail.com

      Reply
  2. Yaser says

    December 13, 2016 at 6:26 am

    In regards of pairing device using rfcomm, it ask to input the passkey at the mobile side, when I tried to input 0000 or 1234 , the results is wrong and unsuccessful connection, how it possible to disable asking to enter the passkey?

    Reply
    • Arnab says

      December 14, 2016 at 8:03 am

      Hello Yaser,

      There's no such option in rfcomm, you've to use bluez and hcitool.
      Try sudo hciconfig noauth
      Hope this helps.
      For details, see man hciconfig and serch for auth and related options.

      Reply
  3. Avi says

    July 7, 2017 at 7:24 pm

    Hello Arnab,
    I want use this hci commands in my python program so that my programs makes bluetooth on and discoverable. Can you please tell me how can I do it ?

    Reply
    • Arnab Satapathi says

      July 7, 2017 at 9:02 pm

      Hi Avi, yeah, you can use the hci and other commands with python.

      Use the os.system function, perhaps that's the easiest way to run system commands with python.

      Reply
  4. Edwin says

    November 24, 2017 at 3:58 pm

    How Can I give Device Permissions to my Debian Laptop's internal Bluetooth as anyone can start/stop the bluetooth ?
    Because I wanted to access bluetooth scan via docker container without privileged mode.
    I am unable to scan, But able to start dbus & bluetooth services using the following command.

    **docker run --cap-add=SYS_ADMIN -v /opt/bluetooth:/var/lib/bluetooth -v /sys/fs/cgroup:/sys/fs/cgroup:ro -v /tmp/$(mktemp -d):/run --net=host -it debian:jessie**

    Anybody have any clue ? Thanks in advance.

    Reply

Leave a Reply Cancel reply

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

Search this site

Fresh Posts

  • 3 ways to create Windows 10 bootable USB
  • GRUB rescue on legacy BIOS systems
  • HDD to SSD cloning on Linux without re-installing
  • Install Phant on Ubuntu server for IoT data logging
  • Install SSH on Ubuntu - Step by Step Guide
  • Install NVIDIA drivers on Ubuntu from the PPA
  • Install NodeJS on Ubuntu from NodeSource repository
  • Unzip Linux command Explained With Examples

Get more ideas

Thank you for subscribing.

Something went wrong.

We're committed to protect your privacy

Recent Comments

  • Arnab Satapathi on NVIDIA Optimus troubleshooting in Debain, Kali Linux
  • Nick on NVIDIA Optimus troubleshooting in Debain, Kali Linux
  • Arnab Satapathi on How to update CPU microcode in Linux
  • John Nisbet on How to update CPU microcode in Linux
  • user on How to use an ISO file as offline repository in Debian

Copyright © PCsuggest.com · All rights reserved.

  • Home Page
  • About PCsuggest
  • Contact Us
  • Privacy Policy
  • Sitemap