Programmatically removing all bluetooth devices on the Linux command line

35,290

Solution 1

bluez-test-device remove XX:XX:XX:XX:XX:XX

Solution 2

If you install the bluez-tools package, run this to unpair a bluetooth device :

bt-device -r xx:xx:xx:xx:xx:xx

where xx:xx:xx:xx:xx:xx is the address of the paired device.

Solution 3

For those using Ubuntu 20.04, here is the same command using the bluetoothctl command

#!/bin/bash 
for device in $(bluetoothctl devices  | grep -o "[[:xdigit:]:]\{8,17\}"); do
    echo "removing bluetooth device: $device | $(bluetoothctl remove $device)"
done

Solution 4

As it is mentioned above on ashish's answer, you can us bluez-test-device to remove the device which that you already know its mac address. So the problem is to parse the mac address of the added devices.

With python or c or whatever you use,

1) list the devices with;

bluez-test-device list

and parse the output and get all the MAC addresses of the devices, add them to a list.

2) disconnect and remove the devices;

bluez-test-device disconnect <MAC ADDRESS>
bluez-test-device remove <MAC ADDRESS>

Solution 5

Command using bluetoothctl binary: for device in $(bluetoothctl devices | grep -vEi '(o que mais vc quer deixar aqui|samsung|jbl|wireless)' | awk '{print $2}'); do bluetoothctl remove $device; done

Share:
35,290
Admin
Author by

Admin

Updated on July 12, 2022

Comments

  • Admin
    Admin almost 2 years

    I am able to scan for all available bluetooth devices with hcitool or with my C program.

    I can pair the device using it's address with a simple-agent python script.

    I would like to know if I can also remove the paired device using either hcitool, hciconfig or some kind of bluetooth command.

    I know the information of detected devices for the hci0 controller is stored in /var/lib/bluetooth/XX:XX:XX:XX:XX:XX, where XX:XX:XX:XX:XX is the address of the hci controller.

    This would be useful for testing pairing, connecting and disconnecting devices.