How to clear bluetooth cache?

The cache data is in /var/lib/bluetooth/[adapter]

the problem is i can’t bind the folder to container

is there any other way to clear it?

Hello @benjboy could you please confirm what device type are you using? and balenaOS version?

Do you think this can help you to bind bluetooth into your container? bluetooth no devices inside container - #4 by Hades32

Let me know if that works!

Hello,
i’m using balena os 2.95.8 on rpi4.

i do have access to bluetooth and i tried clearing cache using this code:

    fmt.Println("Clearing bluetooth cache")
	conn, err := dbus.SystemBus()
	if err != nil {
		fmt.Printf("Failed to connect to system bus: %v\n", err)
		return err
	}

	objManager := conn.Object("org.bluez", "/")
	var managedObjects map[dbus.ObjectPath]map[string]map[string]dbus.Variant
	err = objManager.Call("org.freedesktop.DBus.ObjectManager.GetManagedObjects", 0).Store(&managedObjects)
	if err != nil {
		fmt.Printf("Failed to get managed objects: %v\n", err)
		return err
	}

	for path, interfaces := range managedObjects {
		if _, ok := interfaces["org.bluez.Device1"]; ok {
			fmt.Printf("Found device: %s\n", path)

			adapter := conn.Object("org.bluez", "/org/bluez/hci0")
			err := adapter.Call("org.bluez.Adapter1.RemoveDevice", 0, path).Store()
			if err != nil {
				fmt.Printf("Failed to remove device %s: %v\n", path, err)
			} else {
				fmt.Printf("Successfully removed device: %s\n", path)
			}
		}
	}

but it did not fix the issue i’m having.
The only way is to delete the /var/lib/[adapter]/* content

But I cant’t find a way.

Best regards

Hello, the root filesystem for balenaOS is read-only and cannot be modified persistently. All services running in the hostOS are meant to be used by the hostOS, and your containerized application is meant to run anything it requires by itself.

With that in mind, you’ll want to run your own dbus and BlueZ services in your container and modify them per your needs. In the case of BlueZ, you may also stop the one running in the host OS if it will not be used.

We have some examples that may help you form a solution: a DBUS block, GitHub - balena-labs-projects/dbus and a Bluetooth block, GitHub - balena-labs-projects/bluetooth: Optimized bluetooth agent for balenaOS. Based on BlueZ 5.0.

Thank you this is the solution I needed.
I’m just having a problem connecting to dbus block:
running

conn, err = dbus.SystemBus()

in go throws read tcp 172.18.0.5:40220->172.18.0.3:55884: read: connection reset by peer

while running this succeeds:

cmd := exec.Command("dbus-send", "--system", "--print-reply", "--dest=org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus.ListNames")
		err := cmd.Run()
		if err == nil {
			appLogger.Info("DBus is now accepting connections")
			break
		}

compose file:

version: "2"
services:
 ble:
        build: ./ble
        privileged: true
        restart: always
        cap_add:
            - NET_ADMIN
        environment:
            APP_ENV: development
            DBUS_SYSTEM_BUS_ADDRESS: "tcp:host=dbus-system,port=55884"
        labels:
            io.balena.features.dbus: 1
        depends_on:
            - redis
            - bluez
            - dbus-system
    bluez:
        build: ./bluetooth
        restart: always
        privileged: true
        network_mode: host
        cap_add:
            - NET_ADMIN
        environment:
            DBUS_SYSTEM_BUS_ADDRESS: "tcp:host=localhost,port=55884"
        labels:
            io.balena.features.dbus: "1" # required for stopping host OS bluetooth.service
        depends_on:
            - dbus-system
        devices:
            - "/dev/rfkill:/dev/rfkill" # required for `rfkill unblock`
    dbus-system:
        build: ./dbus
        restart: always
        ports:
            - "55884:55884" # expose bus to containers with network_mode: host
            - "55887:55887" # expose bus to containers with network_mode: host
        environment:
            DBUS_CONFIG: session.conf # allow owning anything without explicit policies

hasanyone have expirienced similiar issue?

best regards

Out of curiosity, can you share what you’re building for your dbus-system service? I assume it’s based on the image listed in GitHub - balena-labs-projects/dbus

The README in that repo also describes connecting via sockets for some possible debugging. I would try simplifying things a bit starting from scratch using the example in the repo and see how that goes. If that works, then you can probably figure out what the problem was.

2 Likes

Yes thats right I’m using balena-labs-projects/dbus. For some time i was using the latest and later the version used in balena-io-experimental/balena-gateway-config. Neither worked

In the end I switched to using unix domain sockets which worked.

Maybe the go library for bluetooth that I’m using doesn’t support dbus over tcp.
Not sure glad it works now.