bluetoothraspberry-piraspbianraspbian-buster

When you trust a bluetooth device, where is the config file that stores the trust?


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?


Solution

  • 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)