Basic Methods to Use Bluetooth on Linux Systems

First, ensure that there is a Bluetooth-capable device on the hardware, then run the following command to activate our Bluetooth device:

The code is as follows:

lsusb

Running hciconfig will show:

From the image above, we can see that our Bluetooth device is hci0.

Running hcitool dev will show the hardware address of our Bluetooth device.

Running hcitool --help can provide more related commands.

Then we activate it:

The code is as follows:

sudo hciconfig hci0 up

Note that Bluetooth must be turned on before activation; otherwise, the following error will occur:

Then we start scanning:

The code is as follows:

hcitool scan

We can see that my phone’s Bluetooth has been discovered~~

Next, we need to start connecting. The main command used during the connection phase is rfcomm:

Running rfcomm --help can show the usage.

First, we need to bind the target Bluetooth device:

The code is as follows:

sudo rfcomm bind /dev/rfcomm0 E0:A6:70:8C:A3:02

Note: The address above is the hardware address of the target Bluetooth device.

Then we connect to it:

The code is as follows:

sudo cat >/dev/rfcomm0

This will prompt the target Bluetooth host to display a dialog box asking for a PIN code; you can enter any code. Then the host will pop up a dialog box, and as long as the entered code matches the previous one, it will pass verification. After that, I found that my phone has already displayed a successful pairing mark.

After pairing is complete, we need to unbind (otherwise, it will prompt that the device is busy during the next use). The command is as follows:

The code is as follows:

sudo rfcomm release /dev/rfcomm0

Using rfkill to soft switch Bluetooth and wireless functions in Linux

Many computer systems include radio transmissions, including Wi-Fi, Bluetooth, and 3G devices. These devices consume power, and when not in use, they waste energy.

RFKill is a subsystem in the Linux kernel that provides an interface to query, activate, and deactivate radio transmissions in the computer system. When transmissions are deactivated, they can be placed in a state that can be reactivated by software (soft lock) or in a state that cannot be reactivated by software (hard lock).

RFKill provides an application programming interface (API) for the kernel subsystem. Kernel drivers are designed to support RFKill by using this API to register with the kernel and contain methods to enable and disable the device. Additionally, RFKill provides user-readable notifications and methods for user programs to query the transmission status.

The RFKill interface is located at /dev/rfkill, which contains the current status of all radio transmissions in the system. Each device is registered with the current RFKill status in sysfs. Moreover, whenever the status changes in RFKill-enabled devices, RFKill emits uevents.

rfkill is a command-line tool that you can use to query and change the devices enabled with RFKill in the system. To obtain this tool, please install the rfkill software package.

If at boot time you can search for wireless networks and input the password correctly but still cannot connect, it may be that the rfkill program is blocking access; it is a soft switch used to control the use of wireless networks and Bluetooth.

Use the command rfkill list to get the list of devices, each containing an associated index number starting from 0.

You can use this index number to let rfkill disable or enable a specific device, for example:

The code is as follows:

rfkill block 0

This will disable the first RFKill-enabled device in the system.

You can also use rfkill to block a specific class of devices or all RFKill-enabled devices. For example:

The code is as follows:

rfkill block wifi

This will disable all Wi-Fi devices in the system. To disable all RFKill-enabled devices, run:

The code is as follows:

rfkill block all

To re-enable a device, run rfkill unblock. To get a complete list of device categories that rfkill can disable, run rfkill help.

Leave a Comment