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:
Some references:
bluetoothctl program that comes with modern debian linux distros: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/client/main.cEDIT:
There is, of course, the python bluez library. But that's GPL licensed. And there is no way to pair devices with bluez, right?
EDIT: I created a demo for all of this (python + dbus + bluetooth) here: https://github.com/elsampsa/btdemo
Have fun.
no need to go further from here - just use that repo
Some more libraries & tutorials I found on the way:
An article at Medium: https://medium.com/@trstringer/talking-to-systemd-through-dbus-with-python-53b903ee88b1
Bluetool library: https://github.com/emlid/bluetool
dbus specs: https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces
My comments about dbus-python were premature. Bluetool uses it, and by following bluetool's code I was able to go forward a bit:
import dbus
def my_pprint(obj, intend = 0):
if isinstance(obj, dict):
for key, value in obj.items():
print(intend*" "+str(key)+" : ")
my_pprint(value, intend = intend + 4)
print()
elif isinstance(obj, list):
for value in obj:
my_pprint(value, intend = intend + 4)
print()
elif isinstance(obj, dbus.Byte ):
print(intend*" "+ hex(obj) )
else:
print(intend*" "+str(obj))
bus = dbus.SystemBus()
proxy_object = bus.get_object("org.bluez","/")
manager = dbus.Interface(proxy_object, "org.freedesktop.DBus.ObjectManager")
objects = manager.GetManagedObjects()
my_pprint(objects) # see DUMP 1 in the end of this answer
i.e., the bluez dbus interface implements an object manager, giving a nice nested structure of objects (see the dump below)
What is (not) implemented at the bluez dbus interface, can be confirmed with this:
gdbus introspect --system --dest [Service] --object-path [Object path]
where Server & Object path come from the API docs here
For example:
gdbus introspect --system --dest org.bluez --object-path /org/bluez
gdbus introspect --system --dest org.bluez --object-path /
gdbus introspect --system --dest org.bluez --object-path /org/bluez/hci0
I'll update this answer once I achieve pairing..
DUMP1 from the code snippet
/org/bluez :
org.freedesktop.DBus.Introspectable :
org.bluez.AgentManager1 :
org.bluez.ProfileManager1 :
/org/bluez/hci0 :
org.freedesktop.DBus.Introspectable :
org.bluez.Adapter1 :
Address :
9C:B6:D0:8C:5D:D6
AddressType :
public
Name :
sampsa-xps13
...
...
org.freedesktop.DBus.Properties :
org.bluez.GattManager1 :
org.bluez.LEAdvertisingManager1 :
ActiveInstances :
SupportedInstances :
SupportedIncludes :
tx-power
appearance
local-name
org.bluez.Media1 :
org.bluez.NetworkServer1 :
/org/bluez/hci0/dev_58_C9_35_2F_A1_EF :
org.freedesktop.DBus.Introspectable :
org.bluez.Device1 :
Address :
58:C9:35:2F:A1:EF
AddressType :
public
Name :
Nokia 5
Alias :
Nokia 5
Class :
5898764
Icon :
phone
Paired :
1
Trusted :
0
Blocked :
0
...
...
org.freedesktop.DBus.Properties :
org.bluez.Network1 :
Connected :
0
org.bluez.MediaControl1 :
Connected :
0
[any other devices follow]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With