I have a raspberry pi 4 model b running raspbian-lite 64-bit with Operating System: Debian GNU/Linux 12 (bookworm) and Kernel: Linux 6.6.20+rpt-rpi-v8.
I have to control the Bluetooth of the raspberry with a python script.
The script has to be able to enable/disable Bluetooth and rename the raspberry pi.
Currently, I use os.system(f"sudo hostnamectl set-hostname '{name}'")
to rename the device and os.system(f"sudo systemctl restart bluetooth")
to restart bluetooth.
This only works some of the time, and often times I manually have to enter more commands in the console:
pi@One:~ $ bluetoothctl
[bluetooth]# discoverable on
[bluetooth]# exit
Is there a more elegant solution to do this, that may also allow for more functionality?