Reputation: 261
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
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
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
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
<fileName>.sh
and paste the code above.chmod +x <fileName>
to make the script executable./<fileName>.sh
Upvotes: 4
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
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
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