Management of  Bluetooth devices in Linux

Management of Bluetooth devices in Linux

Here I will talk about how to connect with a Bluetooth device in Linux using a command-line utility called bluetoothctl.

Alternatively, we can use Graphical Tools like GNOME Bluetooth, Bluedevil, Blueman. But like all other Graphical Tools, they are bloated, they lack the ability of scripting, and they cannot be used without X11 or Wayland. These restrictions are mitigated by bluetoothctl.

Steps to install bluetoothctl:

~$ sudo apt install bluez*        

Now, enable the bluetooth.service using following systemd‘s command

~$ sudo systemctl enable bluetooth.service        

The above is to enable the Bluetooth service in the background. But it requires restarting your system. If you do not want to restart, execute the following command as well:

~$ sudo systemctl start bluetooth.service        

Note: Make sure that kernel module btusb is loaded into the system. Kernel modules are equivalent to drivers in Windows OS. If it is not loaded, load it using

~$ modprobe btusb        

If you followed all the above steps you will have bluetoothctl and now we will take in our consideration the interactive mode of bluetoothctl

To get into interactive mode, use

~$ bluetoothctl        

Output may look like this:

Agent registered
[CHG] Controller F8:89:D6:C8:2A:53 Pairable: yes
[bluetooth]#         

Notice, how the prompt is changing from ~$ to [bluetooth]#. Now, you can execute any command like help after the # symbol. Use help to find all existing commands and their one-liner explanations.

[bluetooth]# help        

After following all the steps, you can quit using

[bluetooth]# quit        

Turn on Bluetooth in Linux

To turn on the Bluetooth, use power on command:

[bluetooth]# power on        

In my system, I get the following output:

[CHG] Controller F8:89:D2:C8:2A:53 Class: 0x006c010c
Changing power on succeeded        

Similarly, to turn it off, you can use the power off command.

Scan for Available Bluetooth Devices

First list available devices using

[bluetooth]# devices        

Output:

[bluetooth]# devices
Device 40:45:DA:B8:AB:BB 34SUPER
Device 04:C8:07:12:D0:2D LG Q3        

If you cannot find your device in the output, use the following command:

[bluetooth]# Scan on        

Output on my system:

As you turn on your Bluetooth device (in the above example, my new smartphone), bluetoothctl will list it in the output. After you have found your device, copy its address for future use.

If you want your Linux computer to be discoverable by other devices, use the command given below. But for my tutorial, it is not necessary.

[bluetooth]# discoverable on        

Step 4: Connect to Your Bluetooth Device

We can do it in two ways: 1. Pair and then Connect. 2. Trust and then Connect. I find it simpler, less confusing and it works for me with all kinds of devices. Hence, I will be describing this in detail.

1. Pair and Connect

Turn on the pairing in your Bluetooth device (ex – smartphone). Then, Turn on the pairing in your Linux computer using

[bluetooth]# pair 90:78:B2:C7:8F:A8        

Now connect using

[bluetooth]# connect 90:78:B2:C7:8F:A8        

Now, you might get a prompt to accept the connection. Just say yes.

2. Trust and Connect

First, you need to trust your device using the following :

[bluetooth]# trust 90:78:B2:C7:8F:A8        

Output:

[CHG] Device 90:78:B2:C7:8F:A8 Trusted: yes
Changing 90:78:B2:C7:8F:A8 trust succeeded        

Now, bluetoothctl saves your device’s address on your computer. In the future, you will not be needing to trust the device anymore. This saving is preserved even over system-restart. Now, you can connect with the device using:

[bluetooth]# connect 90:78:B2:C7:8F:A8        

Now, you might get a prompt to accept the connection. Just say yes. It might even ask you to match passkey/pin. For my smartphone, the output is

Request confirmation
[agent] Confirm passkey 989960 (yes/no): yes
[CHG] Device 90:78:B2:C7:8F:A8 Paired: yes
Connection successful        

Conclusion

There are many more options to explore in bluetoothctl, just read them in bluetoothctl help.

Now you can use Bluetooth for sharing files/internet/audio.

要查看或添加评论,请登录

Mohamed Salem的更多文章

  • Concurrent Queue

    Concurrent Queue

    Did you hear about the queue as a data structure ? ---- of course yes, But what is the concurrent queue, in nutshell…

    1 条评论
  • Priority Queue in C++

    Priority Queue in C++

    A priority queue in c++ is a type of container adapter, which processes only the highest priority element, i.e.

  • Bubble Sort in CPP

    Bubble Sort in CPP

    We will explain the simple sorting Algorithm which (Bubble Sort) Bubble Sort is a simple algorithm which is used to…

  • Global variables with the same name

    Global variables with the same name

    You know from the Build process that the compiler will export the global variables to the assembler and the assembler…

  • Function before and after main()

    Function before and after main()

    Did you start to execute functions before and after the main() function ? you have two solutions to do that : first…

  • Command_line_arguments

    Command_line_arguments

    ???? ???? ?????? ???? ???????? ?? ?? command line arguments ???? ???? ???? ???????? ????? ????????? ???? ???? :O ????…

  • C_prototypes

    C_prototypes

    ??? ???? ?????? ????? ??????? ?? ????? ??? 1) int main(){} void main(){} ?????? ??????? ??? ??????? ????????? ??????…

  • Data Structures

    Data Structures

    https://drive.google.

  • Most Common Questions in Embedded C

    Most Common Questions in Embedded C

    https://drive.google.

社区洞察

其他会员也浏览了