forked from Jumperr-labs/python-gatt-server
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ad_template.py
76 lines (58 loc) · 2.75 KB
/
ad_template.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
#!/usr/bin/env python
"""Provides simple ways to define and start BLE advertisements.
"""
import dbus
import dbus.exceptions
import dbus.mainloop.glib
import dbus.service
import functools
import adapters
import advertising
from advertising import Advertisement
__author__ = "Kasidit Yusuf"
__copyright__ = "bluez-gatt-server 1.0 Copyright (C) 2018 Kasidit Yusuf."
__credits__ = ["Kasidit Yusuf"]
__license__ = "GPL"
__version__ = "1.0"
__maintainer__ = "Kasidit Yusuf"
__email__ = "[email protected]"
__status__ = "Production"
class AdTemplate(Advertisement):
def __init__(self, bus, index,
service_assigned_number_list,
manuf_code=0xffff,
manuf_data=[0x00, 0x01, 0x02, 0x03, 0x04],
service_assigned_number='9999',
service_data=[0x00, 0x01, 0x02, 0x03, 0x04],
include_tx_power=True
):
Advertisement.__init__(self, bus, index, 'peripheral')
if not isinstance(service_assigned_number_list, list):
raise Exception("service_assigned_number_list must be a list - ABORT")
if len(service_assigned_number_list) == 0:
raise Exception("service_assigned_number_list must not be an empty list - ABORT")
for service_assigned_number in service_assigned_number_list:
print("adding service_assigned_number:", service_assigned_number)
self.add_service_uuid(service_assigned_number)
self.add_manufacturer_data(manuf_code, manuf_data)
self.add_service_data(service_assigned_number, service_data)
self.include_tx_power = include_tx_power
def register_ad_cb():
print('Advertisement registered')
def register_ad_error_cb(mainloop, error):
print('Failed to register advertisement: ' + str(error))
mainloop.quit()
def start_ad(mainloop, bus, adapter_name, service_assigned_number_list):
adapter = adapters.find_adapter(bus, advertising.LE_ADVERTISING_MANAGER_IFACE, adapter_name)
print('adapter: %s' % (adapter,))
if not adapter:
raise Exception('LEAdvertisingManager1 interface not found')
adapter_props = dbus.Interface(bus.get_object(advertising.BLUEZ_SERVICE_NAME, adapter),
"org.freedesktop.DBus.Properties")
adapter_props.Set("org.bluez.Adapter1", "Powered", dbus.Boolean(1))
ad_manager = dbus.Interface(bus.get_object(advertising.BLUEZ_SERVICE_NAME, adapter),
advertising.LE_ADVERTISING_MANAGER_IFACE)
ad = AdTemplate(bus, 0, service_assigned_number_list)
ad_manager.RegisterAdvertisement(ad.get_path(), {},
reply_handler=register_ad_cb,
error_handler=functools.partial(register_ad_error_cb, mainloop))