Skip to content

Instantly share code, notes, and snippets.

@patocarr
Last active July 22, 2018 09:11
Show Gist options
  • Save patocarr/af679f48e476ebf02dcfe1e21bcd040f to your computer and use it in GitHub Desktop.
Save patocarr/af679f48e476ebf02dcfe1e21bcd040f to your computer and use it in GitHub Desktop.
GATT profile + pydbus
<!DOCTYPE busconfig PUBLIC
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<policy context="default">
<allow own="com.example"/>
<allow own="com.example.profile"/>
<allow send_destination="com.example"/>
<allow send_destination="com.example.profile"/>
<allow send_destination="com.example.profile" send_interface="com.example.profile"/>
<allow send_interface="com.example"/>
</policy>
</busconfig>
#!/usr/bin/env python
import time
from gi.repository import GLib
from pydbus import SystemBus, Variant
from pydbus.generic import signal
GATT_PROFILE_IFACE = 'org.bluez.GattProfile1'
GATT_MANAGER_IFACE = 'org.bluez.GattManager1'
class Application(object):
"""
<node>
<interface name="org.freedesktop.DBus.ObjectManager">
<method name="GetManagedObjects">
<arg name="objects" type="a{oa{sa{sv}}}" direction="out"/>
</method>
<signal name="InterfacesAdded">
<arg name="object" type="o"/>
<arg name="interfaces" type="a{sa{sv}}"/>
</signal>
<signal name="InterfacesRemoved">
<arg name="object" type="o"/>
<arg name="interfaces" type="as"/>
</signal>
</interface>
</node>
"""
uuids = ['00001809-0000-1000-8000-00805f9b34fb']
InterfacesAdded = signal()
InterfacesRemoved = signal()
def GetManagedObjects(self):
response = {}
response['/com/example/profile'] = \
{
GATT_PROFILE_IFACE: {
'UUIDs': Variant('as', self.uuids)
}
}
return response
class Profile(object):
"""
<node>
<interface name="org.bluez.GattProfile1">
<method name="Release">
</method>
<property name="UUIDs" type="as" access="read">
</property>
</interface>
</node>
"""
uuids = ['00001809-0000-1000-8000-00805f9b34fb']
def Release(self):
print("Release")
@property
def UUIDs(self):
return self.uuids
bus = SystemBus()
bus.publish('com.example', Application(), \
('profile', Profile()))
manager = bus.get('org.bluez', '/org/bluez/hci0')[GATT_MANAGER_IFACE]
manager.RegisterApplication('/com/example', {})
loop = GLib.MainLoop()
loop.run()
manager.UnRegisterApplication('/com/example')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment