1

Lets say I run this command

pi@raspberrypi:~ $ bluetoothctl
Agent registered
[bluetooth]# paired-devices
[raspberrypi]# paired-devices
Device XX:XX:XX:XX:XX:XX MyDevice
[raspberrypi]# trust XX:XX:XX:XX:XX:XX
[CHG] Device XX:XX:XX:XX:XX:XX Trusted: yes
Changing XX:XX:XX:XX:XX:XX trust succeeded

Where is the actual file that stores the list of trusted devices?

Yatuz1a
  • 25
  • 3

1 Answers1

4

If you do something like $ sudo grep -Ri trust /var/lib/bluetooth you will see some information.

This does come with a big warning that it is not the intended way to gain access to the information. The intent is that it will be accessed with the BlueZ API's documented at:

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc

And the official examples are at:

https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test

Typically this means using the D-Bus bindings. From the command line you can get a list of everything BlueZ knows about with:

busctl call org.bluez / org.freedesktop.DBus.ObjectManager GetManagedObjects

In a language like python, it would be:

import pydbus

bus = pydbus.SystemBus()
mngr = bus.get('org.bluez', '/')

mngd_objs = mngr.GetManagedObjects()

for path in mngd_objs:
    device_info = mngd_objs[path].get('org.bluez.Device1')
    if device_info:
        print(f'Device: {device_info.get("Address")} is Trusted={device_info.get("Trusted")}')

To extend this to answer the question below about how to remove any trusted devices...

This is controlled by the adapter interface and the RemoveDevice method. We need to know the D-Bus path for the Adapter object. There are many ways you can be find this information, using busctl tree org.bluez on the command line may be the quickest. The path is typically /org/bluez/hci0 and will prepend all your devices. With that assumption, you can extend the above example to delete trusted devices as follows:

import pydbus

bus = pydbus.SystemBus()
mngr = bus.get('org.bluez', '/')

mngd_objs = mngr.GetManagedObjects()
dongle = bus.get('org.bluez', '/org/bluez/hci0')

for path in mngd_objs:
    device_info = mngd_objs[path].get('org.bluez.Device1')
    if device_info:
        trusted = device_info.get('Trusted')
        if trusted:
            print(f'Removing Device: {device_info.get("Address")}') 
            dongle.RemoveDevice(path)
ukBaz
  • 6,985
  • 2
  • 8
  • 31
  • Wow! Thank you so much! I guess i could jump to my second question. how would I able to remove the devices? The idea is, after every boot, a cron script runs and removes all trusted devices. How easy would that be? – Yatuz1a Mar 19 '21 at 19:23
  • I've extended the answer to cover this question. Typically it would be better to update your question or open a new question rather than ask it in the comments. – ukBaz Mar 19 '21 at 20:13