user2570136
user2570136

Reputation: 261

Programmatically removing all bluetooth devices on the Linux command line

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.

Upvotes: 25

Views: 49601

Answers (7)

renzo_cast
renzo_cast

Reputation: 101

Similar to Jesse de gans' answer here is a one-liner to disconnect all devices for both bt-device and bluetoothctl.

# disconnect all devices using bt-device
for device in `bt-device -l`; do [[ "$device" =~ ([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}) ]] && bt-device -d $(echo $device | sed 's/[()]//g'); done

# disconnect all devices using bluetoothctl
for device in `bluetoothctl paired-devices`; do [[ "$device" =~ ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$ ]] && bluetoothctl disconnect $device; done

It's a little more work for bt-device as the MAC addresses are wrapped in parenthesis () so we need to use sed to strip them.

Upvotes: 0

user15764068
user15764068

Reputation: 219

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

Upvotes: 21

Jesse de gans
Jesse de gans

Reputation: 1654

All these answers don't answer the headline "removing all Bluetooth devices"

I wrote this little bash script to remove all the Bluetooth devices that are listed in the bt-device -l

#!/bin/bash 
for device in $(bt-device -l | grep -o "[[:xdigit:]:]\{11,17\}"); do
    echo "removing bluetooth device: $device | $(bt-device -r $device)"
done

How to run?

  1. Make a new file like <fileName>.sh and paste the code above.
  2. Run chmod +x <fileName> to make the script executable
  3. Run ./<fileName>.sh
  4. Celebrate! All Bluetooth devices are removed now :)

Upvotes: 4

Ariel Dalefe Alves
Ariel Dalefe Alves

Reputation: 61

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

Upvotes: 6

Levent Divilioglu
Levent Divilioglu

Reputation: 11612

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>

Upvotes: 6

BЈовић
BЈовић

Reputation: 64263

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.

Upvotes: 8

ashish
ashish

Reputation: 1381

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

Upvotes: 8

Related Questions