As the title suggests.
I am having hard time in finding a decent python dbus module for this one. There was a lot of smoke, mirrors & traps on the way.
First, let' see what I did.
The first booby-trap was dbus-python. Couldn't make it to work at all. Even the examples are broken.
It seems to be outdated, and replaced by pydbus
(The copyright owners / authors for both of them seem to be the same)
Let's see pydbus then. Also there, the examples don't work that great:
from pydbus import SystemBus
bus = SystemBus()
dev = bus.get('.NetworkManager', 'Devices/0')
gives
KeyError: 'object does not export any interfaces; you might need to pass object path as the 2nd argument for get()'
However, if we try something completely contrary to that error message..
dev = bus.get('.NetworkManager')
we get a nice object with lots of methods:
dir(dev)
['ActivateConnection',
'ActivatingConnection',
'ActiveConnections',
'AddAndActivateConnection',
'AllDevices',
'Capabilities',
'CheckConnectivity',
'CheckPermissions',
'CheckpointCreate',
'CheckpointDestroy',
'CheckpointRollback',
'Connectivity',
....
]
So far so good. Let's see if we can get our hands on bluetooth with that one:
dev = bus.get('org.bluez')
dir(dev)
['Introspect',
'RegisterAgent',
'RegisterProfile',
'RequestDefaultAgent',
'UnregisterAgent',
'UnregisterProfile'
]
So, again a nice object with some methods. However most of the things are missing. Let's take a look for example at this:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt
Can't get that mentioned "Connect" method with pydbus..?
Please don't suggest me to use subprocess.Popen with bluetoothctl - how lame is that. Besides, I want to learn dbus.
Questions:
- How to get access to all bluetooth methods?
- What would be the correct dbus python binding to use (that actually works)?
- Any working example would be highly appreciated.
- I want to achieve bt pairing using python
Some references:
- Another SO question: Bluez Programming
- Dbus specs: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc
- Source code for the celebrated
bluetoothctl
program that comes with modern debian linux distros: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/client/main.c - Nice dbus article: https://medium.com/cesar-update/exposing-a-d-bus-interface-in-linux-part-2-90374a9e381b
EDIT:
There is, of course, the python bluez library. But that's GPL licensed. And there is no way to pair devices with bluez, right?