-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvertisement.py
executable file
·113 lines (89 loc) · 4.13 KB
/
advertisement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import dbus
import dbus.service
from bletools import BleTools
BLUEZ_SERVICE_NAME = "org.bluez"
LE_ADVERTISING_MANAGER_IFACE = "org.bluez.LEAdvertisingManager1"
DBUS_OM_IFACE = "org.freedesktop.DBus.ObjectManager"
DBUS_PROP_IFACE = "org.freedesktop.DBus.Properties"
LE_ADVERTISEMENT_IFACE = "org.bluez.LEAdvertisement1"
class Advertisement(dbus.service.Object):
PATH_BASE = "/org/bluez/example/advertisement"
def __init__(self, index, advertising_type):
self.path = self.PATH_BASE + str(index)
self.bus = BleTools.get_bus()
self.ad_type = advertising_type
self.local_name = None
self.service_uuids = None
self.solicit_uuids = None
self.manufacturer_data = None
self.service_data = None
self.include_tx_power = None
dbus.service.Object.__init__(self, self.bus, self.path)
def get_properties(self):
properties = dict()
properties["Type"] = self.ad_type
if self.local_name is not None:
properties["LocalName"] = dbus.String(self.local_name)
if self.service_uuids is not None:
properties["ServiceUUIDs"] = dbus.Array(self.service_uuids,
signature='s')
if self.solicit_uuids is not None:
properties["SolicitUUIDs"] = dbus.Array(self.solicit_uuids,
signature='s')
if self.manufacturer_data is not None:
properties["ManufacturerData"] = dbus.Dictionary(
self.manufacturer_data, signature='qv')
if self.service_data is not None:
properties["ServiceData"] = dbus.Dictionary(self.service_data,
signature='sv')
if self.include_tx_power is not None:
properties["IncludeTxPower"] = dbus.Boolean(self.include_tx_power)
if self.local_name is not None:
properties["LocalName"] = dbus.String(self.local_name)
return {LE_ADVERTISEMENT_IFACE: properties}
def get_path(self):
return dbus.ObjectPath(self.path)
def add_service_uuid(self, uuid):
if not self.service_uuids:
self.service_uuids = []
self.service_uuids.append(uuid)
def add_solicit_uuid(self, uuid):
if not self.solicit_uuids:
self.solicit_uuids = []
self.solicit_uuids.append(uuid)
def add_manufacturer_data(self, manuf_code, data):
if not self.manufacturer_data:
self.manufacturer_data = dbus.Dictionary({}, signature="qv")
self.manufacturer_data[manuf_code] = dbus.Array(data, signature="y")
def add_service_data(self, uuid, data):
if not self.service_data:
self.service_data = dbus.Dictionary({}, signature="sv")
self.service_data[uuid] = dbus.Array(data, signature="y")
def add_local_name(self, name):
if not self.local_name:
self.local_name = ""
self.local_name = dbus.String(name)
@dbus.service.method(DBUS_PROP_IFACE,
in_signature="s",
out_signature="a{sv}")
def GetAll(self, interface):
if interface != LE_ADVERTISEMENT_IFACE:
raise InvalidArgsException()
return self.get_properties()[LE_ADVERTISEMENT_IFACE]
@dbus.service.method(LE_ADVERTISEMENT_IFACE,
in_signature='',
out_signature='')
def Release(self):
print ('%s: Released!' % self.path)
def register_ad_callback(self):
print("GATT advertisement registered")
def register_ad_error_callback(self):
print("Failed to register GATT advertisement")
def register(self):
bus = BleTools.get_bus()
adapter,mac = BleTools.find_adapter(bus)
ad_manager = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, adapter),
LE_ADVERTISING_MANAGER_IFACE)
ad_manager.RegisterAdvertisement(self.get_path(), {},
reply_handler=self.register_ad_callback,
error_handler=self.register_ad_error_callback)