├── .idea ├── modules.xml └── Bluetooth-LE-LED.iml ├── README.md ├── .gitignore ├── src ├── led_matrix.py └── bluez_components.py └── COPYING.txt /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/Bluetooth-LE-LED.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bluetooth Low Energy LED Matrix 2 | © 2016 [Tobias Trumm](mailto:tobiastrumm@uni-muenster.de) licensed under GNU GPLv2 3 | 4 | This project uses BlueZ's D-Bus API to turn your Raspberry Pi 3 into a Low Energy peripheral. It provides a GATT service that allows you to control the [Adafruit Bicolor LED Square Pixel Matrix](https://www.adafruit.com/products/902). 5 | 6 | ## Dependencies 7 | - [BlueZ](http://www.bluez.org/) 5.41 or newer. Older versions might also work, but I have not tested it. See this [tutorial from Adafruit](https://learn.adafruit.com/install-bluez-on-the-raspberry-pi/installation) on how to install the latest Bluez version on your Raspberry Pi. 8 | - [dbus-python](https://pypi.python.org/pypi/dbus-python/). Needed to access the D-Bus API. 9 | - [PyGObject](https://wiki.gnome.org/action/show/Projects/PyGObject). Needed to access the D-Bus API. 10 | - [Adafruit Python LED Backpack](https://github.com/adafruit/Adafruit_Python_LED_Backpack). Needed to control the LED matrix. 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/python,pycharm 2 | 3 | ### Python ### 4 | # Byte-compiled / optimized / DLL files 5 | __pycache__/ 6 | *.py[cod] 7 | *$py.class 8 | 9 | # C extensions 10 | *.so 11 | 12 | # Distribution / packaging 13 | .Python 14 | env/ 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | .eggs/ 21 | lib/ 22 | lib64/ 23 | parts/ 24 | sdist/ 25 | var/ 26 | *.egg-info/ 27 | .installed.cfg 28 | *.egg 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *,cover 49 | .hypothesis/ 50 | 51 | # Translations 52 | *.mo 53 | *.pot 54 | 55 | # Django stuff: 56 | *.log 57 | local_settings.py 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # IPython Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # dotenv 82 | .env 83 | 84 | # virtualenv 85 | venv/ 86 | ENV/ 87 | 88 | # Spyder project settings 89 | .spyderproject 90 | 91 | # Rope project settings 92 | .ropeproject 93 | 94 | 95 | ### PyCharm ### 96 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 97 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 98 | 99 | # User-specific stuff: 100 | .idea/workspace.xml 101 | .idea/tasks.xml 102 | .idea/dictionaries 103 | .idea/vcs.xml 104 | .idea/jsLibraryMappings.xml 105 | 106 | # Sensitive or high-churn files: 107 | .idea/dataSources.ids 108 | .idea/dataSources.xml 109 | .idea/dataSources.local.xml 110 | .idea/sqlDataSources.xml 111 | .idea/dynamic.xml 112 | .idea/uiDesigner.xml 113 | 114 | # Gradle: 115 | .idea/gradle.xml 116 | .idea/libraries 117 | 118 | # Mongo Explorer plugin: 119 | .idea/mongoSettings.xml 120 | 121 | ## File-based project format: 122 | *.iws 123 | 124 | ## Plugin-specific files: 125 | 126 | # IntelliJ 127 | /out/ 128 | 129 | # mpeltonen/sbt-idea plugin 130 | .idea_modules/ 131 | 132 | # JIRA plugin 133 | atlassian-ide-plugin.xml 134 | 135 | # Crashlytics plugin (for Android Studio and IntelliJ) 136 | com_crashlytics_export_strings.xml 137 | crashlytics.properties 138 | crashlytics-build.properties 139 | fabric.properties 140 | 141 | ### PyCharm Patch ### 142 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 143 | 144 | # *.iml 145 | # modules.xml 146 | # .idea/misc.xml 147 | # *.ipr 148 | 149 | ### Own ignored files ### 150 | .idea/deployment.xml 151 | .idea/misc.xml 152 | .idea/remote-mappings.xml 153 | .idea/webServers.xml -------------------------------------------------------------------------------- /src/led_matrix.py: -------------------------------------------------------------------------------- 1 | import dbus 2 | import dbus.mainloop.glib 3 | 4 | try: 5 | from gi.repository import GObject 6 | except ImportError: 7 | import gobject as GObject 8 | 9 | from Adafruit_LED_Backpack import BicolorMatrix8x8 10 | 11 | from bluez_components import * 12 | 13 | mainloop = None 14 | 15 | COLOR_TABLE = { 16 | 0: BicolorMatrix8x8.OFF, 17 | 1: BicolorMatrix8x8.RED, 18 | 2: BicolorMatrix8x8.GREEN, 19 | 3: BicolorMatrix8x8.YELLOW 20 | } 21 | 22 | 23 | def int_to_hex(int_value): 24 | return { 25 | 0: '0', 26 | 1: '1', 27 | 2: '2', 28 | 3: '3', 29 | 4: '4', 30 | 5: '5', 31 | 6: '6', 32 | 7: '7', 33 | 8: '8', 34 | 9: '9', 35 | 10: 'a', 36 | 11: 'b', 37 | 12: 'c', 38 | 13: 'd', 39 | 14: 'e', 40 | 15: 'f' 41 | }.get(int_value, '0') 42 | 43 | 44 | def set_display_pixel(display, row, column, value): 45 | display.set_pixel(column, row, COLOR_TABLE[value]) 46 | 47 | 48 | def set_display_row(display, row, byte_values): 49 | """ 50 | The color of each pixel is encoded in two bits. 51 | 00: off 52 | 01: red 53 | 10: green 54 | 11: yellow 55 | Each row consists of 8 pixel and therefore of 2 bytes. 56 | Pixel | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 57 | -------+-----+-----+-----+-----+-----+-----+-----+---- 58 | Bit | 7 6 | 5 4 | 3 2 | 1 0 | 7 6 | 5 4 | 3 2 | 1 0 59 | -------+-----+-----+-----+-----+-----+-----+-----+----- 60 | Byte | 0 | 1 61 | """ 62 | pixel_0 = byte_values[0] >> 6 63 | pixel_1 = (byte_values[0] & 0x30) >> 4 64 | pixel_2 = (byte_values[0] & 0x0c) >> 2 65 | pixel_3 = (byte_values[0] & 0x03) 66 | 67 | pixel_4 = byte_values[1] >> 6 68 | pixel_5 = (byte_values[1] & 0x30) >> 4 69 | pixel_6 = (byte_values[1] & 0x0c) >> 2 70 | pixel_7 = (byte_values[1] & 0x03) 71 | 72 | set_display_pixel(display, row, 0, pixel_0) 73 | set_display_pixel(display, row, 1, pixel_1) 74 | set_display_pixel(display, row, 2, pixel_2) 75 | set_display_pixel(display, row, 3, pixel_3) 76 | set_display_pixel(display, row, 4, pixel_4) 77 | set_display_pixel(display, row, 5, pixel_5) 78 | set_display_pixel(display, row, 6, pixel_6) 79 | set_display_pixel(display, row, 7, pixel_7) 80 | display.write_display() 81 | 82 | 83 | class RowChrc(Characteristic): 84 | ROW_UUID = '12345678-1234-5678-1234-56789abc000' 85 | 86 | def __init__(self, bus, index, service, row, display): 87 | Characteristic.__init__( 88 | self, bus, index, 89 | self.ROW_UUID + int_to_hex(row), # use the row number to build the UUID 90 | ['read', 'write'], 91 | service) 92 | self.value = [0x00, 0x00] 93 | self.row = row 94 | self.display = display 95 | 96 | def ReadValue(self, options): 97 | print('RowCharacteristic Read: Row: ' + str(self.row) + ' ' + repr(self.value)) 98 | return self.value 99 | 100 | def WriteValue(self, value, options): 101 | print('RowCharacteristic Write: Row: ' + str(self.row) + ' ' + repr(value)) 102 | set_display_row(self.display, self.row, value[:2]) 103 | self.value = value[:2] 104 | 105 | 106 | class LedService(Service): 107 | LED_SVC_UUID = '12345678-1234-5678-1234-56789abc0010' 108 | 109 | def __init__(self, bus, index, display): 110 | Service.__init__(self, bus, index, self.LED_SVC_UUID, True) 111 | self.add_characteristic(RowChrc(bus, 0, self, 0, display)) 112 | self.add_characteristic(RowChrc(bus, 1, self, 1, display)) 113 | self.add_characteristic(RowChrc(bus, 2, self, 2, display)) 114 | self.add_characteristic(RowChrc(bus, 3, self, 3, display)) 115 | self.add_characteristic(RowChrc(bus, 4, self, 4, display)) 116 | self.add_characteristic(RowChrc(bus, 5, self, 5, display)) 117 | self.add_characteristic(RowChrc(bus, 6, self, 6, display)) 118 | self.add_characteristic(RowChrc(bus, 7, self, 7, display)) 119 | 120 | 121 | class LedApplication(Application): 122 | def __init__(self, bus, display): 123 | Application.__init__(self, bus) 124 | self.add_service(LedService(bus, 0, display)) 125 | 126 | 127 | class LedAdvertisement(Advertisement): 128 | def __init__(self, bus, index): 129 | Advertisement.__init__(self, bus, index, 'peripheral') 130 | self.add_service_uuid(LedService.LED_SVC_UUID) 131 | self.include_tx_power = True 132 | 133 | 134 | def setup_display(): 135 | display = BicolorMatrix8x8.BicolorMatrix8x8() 136 | display.begin() 137 | display.clear() 138 | display.write_display() 139 | return display 140 | 141 | 142 | def register_ad_cb(): 143 | """ 144 | Callback if registering advertisement was successful 145 | """ 146 | print('Advertisement registered') 147 | 148 | 149 | def register_ad_error_cb(error): 150 | """ 151 | Callback if registering advertisement failed 152 | """ 153 | print('Failed to register advertisement: ' + str(error)) 154 | mainloop.quit() 155 | 156 | 157 | def register_app_cb(): 158 | """ 159 | Callback if registering GATT application was successful 160 | """ 161 | print('GATT application registered') 162 | 163 | 164 | def register_app_error_cb(error): 165 | """ 166 | Callback if registering GATT application failed. 167 | """ 168 | print('Failed to register application: ' + str(error)) 169 | mainloop.quit() 170 | 171 | 172 | def main(): 173 | global mainloop 174 | global display 175 | 176 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 177 | 178 | bus = dbus.SystemBus() 179 | 180 | # Get ServiceManager and AdvertisingManager 181 | service_manager = get_service_manager(bus) 182 | ad_manager = get_ad_manager(bus) 183 | 184 | # Create gatt services 185 | display = setup_display() 186 | app = LedApplication(bus, display) 187 | 188 | # Create advertisement 189 | test_advertisement = LedAdvertisement(bus, 0) 190 | 191 | mainloop = GObject.MainLoop() 192 | 193 | # Register gatt services 194 | service_manager.RegisterApplication(app.get_path(), {}, 195 | reply_handler=register_app_cb, 196 | error_handler=register_app_error_cb) 197 | 198 | # Register advertisement 199 | ad_manager.RegisterAdvertisement(test_advertisement.get_path(), {}, 200 | reply_handler=register_ad_cb, 201 | error_handler=register_ad_error_cb) 202 | 203 | try: 204 | mainloop.run() 205 | except KeyboardInterrupt: 206 | display.clear() 207 | display.write_display() 208 | 209 | 210 | if __name__ == '__main__': 211 | main() 212 | -------------------------------------------------------------------------------- /src/bluez_components.py: -------------------------------------------------------------------------------- 1 | ''' 2 | These classes and functions were taken from the example-advertisement.py and example-gatt-server.py scripts 3 | of the BlueZ project. They were grouped together in this file to simplify the usage of BlueZ's D-Bus API. 4 | It allows us to register our own GATT services and to advertise them. 5 | For more information about BlueZ visit http://www.bluez.org/ 6 | ''' 7 | 8 | import dbus.exceptions 9 | import dbus.service 10 | 11 | BLUEZ_SERVICE_NAME = 'org.bluez' 12 | GATT_MANAGER_IFACE = 'org.bluez.GattManager1' 13 | LE_ADVERTISING_MANAGER_IFACE = 'org.bluez.LEAdvertisingManager1' 14 | 15 | DBUS_OM_IFACE = 'org.freedesktop.DBus.ObjectManager' 16 | DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties' 17 | 18 | GATT_SERVICE_IFACE = 'org.bluez.GattService1' 19 | GATT_CHRC_IFACE = 'org.bluez.GattCharacteristic1' 20 | GATT_DESC_IFACE = 'org.bluez.GattDescriptor1' 21 | 22 | LE_ADVERTISEMENT_IFACE = 'org.bluez.LEAdvertisement1' 23 | 24 | 25 | class InvalidArgsException(dbus.exceptions.DBusException): 26 | _dbus_error_name = 'org.freedesktop.DBus.Error.InvalidArgs' 27 | 28 | 29 | class NotSupportedException(dbus.exceptions.DBusException): 30 | _dbus_error_name = 'org.bluez.Error.NotSupported' 31 | 32 | 33 | class NotPermittedException(dbus.exceptions.DBusException): 34 | _dbus_error_name = 'org.bluez.Error.NotPermitted' 35 | 36 | 37 | class InvalidValueLengthException(dbus.exceptions.DBusException): 38 | _dbus_error_name = 'org.bluez.Error.InvalidValueLength' 39 | 40 | 41 | class FailedException(dbus.exceptions.DBusException): 42 | _dbus_error_name = 'org.bluez.Error.Failed' 43 | 44 | 45 | class Application(dbus.service.Object): 46 | def __init__(self, bus): 47 | self.path = '/' 48 | self.services = [] 49 | dbus.service.Object.__init__(self, bus, self.path) 50 | 51 | def get_path(self): 52 | return dbus.ObjectPath(self.path) 53 | 54 | def add_service(self, service): 55 | self.services.append(service) 56 | 57 | @dbus.service.method(DBUS_OM_IFACE, out_signature='a{oa{sa{sv}}}') 58 | def GetManagedObjects(self): 59 | response = {} 60 | print('GetManagedObjects') 61 | 62 | for service in self.services: 63 | response[service.get_path()] = service.get_properties() 64 | chrcs = service.get_characteristics() 65 | for chrc in chrcs: 66 | response[chrc.get_path()] = chrc.get_properties() 67 | descs = chrc.get_descriptors() 68 | for desc in descs: 69 | response[desc.get_path()] = desc.get_properties() 70 | 71 | return response 72 | 73 | 74 | class Service(dbus.service.Object): 75 | PATH_BASE = '/org/bluez/example/service' 76 | 77 | def __init__(self, bus, index, uuid, primary): 78 | self.path = self.PATH_BASE + str(index) 79 | self.bus = bus 80 | self.uuid = uuid 81 | self.primary = primary 82 | self.characteristics = [] 83 | dbus.service.Object.__init__(self, bus, self.path) 84 | 85 | def get_properties(self): 86 | return { 87 | GATT_SERVICE_IFACE: { 88 | 'UUID': self.uuid, 89 | 'Primary': self.primary, 90 | 'Characteristics': dbus.Array( 91 | self.get_characteristic_paths(), 92 | signature='o') 93 | } 94 | } 95 | 96 | def get_path(self): 97 | return dbus.ObjectPath(self.path) 98 | 99 | def add_characteristic(self, characteristic): 100 | self.characteristics.append(characteristic) 101 | 102 | def get_characteristic_paths(self): 103 | result = [] 104 | for chrc in self.characteristics: 105 | result.append(chrc.get_path()) 106 | return result 107 | 108 | def get_characteristics(self): 109 | return self.characteristics 110 | 111 | @dbus.service.method(DBUS_PROP_IFACE, 112 | in_signature='s', 113 | out_signature='a{sv}') 114 | def GetAll(self, interface): 115 | if interface != GATT_SERVICE_IFACE: 116 | raise InvalidArgsException() 117 | 118 | return self.get_properties()[GATT_SERVICE_IFACE] 119 | 120 | 121 | class Characteristic(dbus.service.Object): 122 | def __init__(self, bus, index, uuid, flags, service): 123 | self.path = service.path + '/char' + str(index) 124 | self.bus = bus 125 | self.uuid = uuid 126 | self.service = service 127 | self.flags = flags 128 | self.descriptors = [] 129 | dbus.service.Object.__init__(self, bus, self.path) 130 | 131 | def get_properties(self): 132 | return { 133 | GATT_CHRC_IFACE: { 134 | 'Service': self.service.get_path(), 135 | 'UUID': self.uuid, 136 | 'Flags': self.flags, 137 | 'Descriptors': dbus.Array( 138 | self.get_descriptor_paths(), 139 | signature='o') 140 | } 141 | } 142 | 143 | def get_path(self): 144 | return dbus.ObjectPath(self.path) 145 | 146 | def add_descriptor(self, descriptor): 147 | self.descriptors.append(descriptor) 148 | 149 | def get_descriptor_paths(self): 150 | result = [] 151 | for desc in self.descriptors: 152 | result.append(desc.get_path()) 153 | return result 154 | 155 | def get_descriptors(self): 156 | return self.descriptors 157 | 158 | @dbus.service.method(DBUS_PROP_IFACE, 159 | in_signature='s', 160 | out_signature='a{sv}') 161 | def GetAll(self, interface): 162 | if interface != GATT_CHRC_IFACE: 163 | raise InvalidArgsException() 164 | 165 | return self.get_properties()[GATT_CHRC_IFACE] 166 | 167 | @dbus.service.method(GATT_CHRC_IFACE, 168 | in_signature='a{sv}', 169 | out_signature='ay') 170 | def ReadValue(self, options): 171 | print('Default ReadValue called, returning error') 172 | raise NotSupportedException() 173 | 174 | @dbus.service.method(GATT_CHRC_IFACE, in_signature='aya{sv}') 175 | def WriteValue(self, value, options): 176 | print('Default WriteValue called, returning error') 177 | raise NotSupportedException() 178 | 179 | @dbus.service.method(GATT_CHRC_IFACE) 180 | def StartNotify(self): 181 | print('Default StartNotify called, returning error') 182 | raise NotSupportedException() 183 | 184 | @dbus.service.method(GATT_CHRC_IFACE) 185 | def StopNotify(self): 186 | print('Default StopNotify called, returning error') 187 | raise NotSupportedException() 188 | 189 | @dbus.service.signal(DBUS_PROP_IFACE, 190 | signature='sa{sv}as') 191 | def PropertiesChanged(self, interface, changed, invalidated): 192 | pass 193 | 194 | 195 | class Descriptor(dbus.service.Object): 196 | def __init__(self, bus, index, uuid, flags, characteristic): 197 | self.path = characteristic.path + '/desc' + str(index) 198 | self.bus = bus 199 | self.uuid = uuid 200 | self.flags = flags 201 | self.chrc = characteristic 202 | dbus.service.Object.__init__(self, bus, self.path) 203 | 204 | def get_properties(self): 205 | return { 206 | GATT_DESC_IFACE: { 207 | 'Characteristic': self.chrc.get_path(), 208 | 'UUID': self.uuid, 209 | 'Flags': self.flags, 210 | } 211 | } 212 | 213 | def get_path(self): 214 | return dbus.ObjectPath(self.path) 215 | 216 | @dbus.service.method(DBUS_PROP_IFACE, 217 | in_signature='s', 218 | out_signature='a{sv}') 219 | def GetAll(self, interface): 220 | if interface != GATT_DESC_IFACE: 221 | raise InvalidArgsException() 222 | 223 | return self.get_properties()[GATT_CHRC_IFACE] 224 | 225 | @dbus.service.method(GATT_DESC_IFACE, 226 | in_signature='a{sv}', 227 | out_signature='ay') 228 | def ReadValue(self, options): 229 | print('Default ReadValue called, returning error') 230 | raise NotSupportedException() 231 | 232 | @dbus.service.method(GATT_DESC_IFACE, in_signature='aya{sv}') 233 | def WriteValue(self, value, options): 234 | print('Default WriteValue called, returning error') 235 | raise NotSupportedException() 236 | 237 | 238 | class Advertisement(dbus.service.Object): 239 | PATH_BASE = '/org/bluez/example/advertisement' 240 | 241 | def __init__(self, bus, index, advertising_type): 242 | self.path = self.PATH_BASE + str(index) 243 | self.bus = bus 244 | self.ad_type = advertising_type 245 | self.service_uuids = None 246 | self.manufacturer_data = None 247 | self.solicit_uuids = None 248 | self.service_data = None 249 | self.include_tx_power = None 250 | dbus.service.Object.__init__(self, bus, self.path) 251 | 252 | def get_properties(self): 253 | properties = dict() 254 | properties['Type'] = self.ad_type 255 | if self.service_uuids is not None: 256 | properties['ServiceUUIDs'] = dbus.Array(self.service_uuids, 257 | signature='s') 258 | if self.solicit_uuids is not None: 259 | properties['SolicitUUIDs'] = dbus.Array(self.solicit_uuids, 260 | signature='s') 261 | if self.manufacturer_data is not None: 262 | properties['ManufacturerData'] = dbus.Dictionary( 263 | self.manufacturer_data, signature='qay') 264 | if self.service_data is not None: 265 | properties['ServiceData'] = dbus.Dictionary(self.service_data, 266 | signature='say') 267 | if self.include_tx_power is not None: 268 | properties['IncludeTxPower'] = dbus.Boolean(self.include_tx_power) 269 | return {LE_ADVERTISEMENT_IFACE: properties} 270 | 271 | def get_path(self): 272 | return dbus.ObjectPath(self.path) 273 | 274 | def add_service_uuid(self, uuid): 275 | if not self.service_uuids: 276 | self.service_uuids = [] 277 | self.service_uuids.append(uuid) 278 | 279 | def add_solicit_uuid(self, uuid): 280 | if not self.solicit_uuids: 281 | self.solicit_uuids = [] 282 | self.solicit_uuids.append(uuid) 283 | 284 | def add_manufacturer_data(self, manuf_code, data): 285 | if not self.manufacturer_data: 286 | self.manufacturer_data = dict() 287 | self.manufacturer_data[manuf_code] = data 288 | 289 | def add_service_data(self, uuid, data): 290 | if not self.service_data: 291 | self.service_data = dict() 292 | self.service_data[uuid] = data 293 | 294 | @dbus.service.method(DBUS_PROP_IFACE, 295 | in_signature='s', 296 | out_signature='a{sv}') 297 | def GetAll(self, interface): 298 | print('GetAll') 299 | if interface != LE_ADVERTISEMENT_IFACE: 300 | raise InvalidArgsException() 301 | print('returning props') 302 | return self.get_properties()[LE_ADVERTISEMENT_IFACE] 303 | 304 | @dbus.service.method(LE_ADVERTISEMENT_IFACE, 305 | in_signature='', 306 | out_signature='') 307 | def Release(self): 308 | print('%s: Released!' % self.path) 309 | 310 | 311 | def find_adapter_gattmanager(bus): 312 | remote_om = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, '/'), 313 | DBUS_OM_IFACE) 314 | objects = remote_om.GetManagedObjects() 315 | 316 | for o, props in objects.items(): 317 | if GATT_MANAGER_IFACE in props.keys(): 318 | return o 319 | 320 | return None 321 | 322 | 323 | def find_adapter_advertisingmanager(bus): 324 | remote_om = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, '/'), 325 | DBUS_OM_IFACE) 326 | objects = remote_om.GetManagedObjects() 327 | 328 | for o, props in objects.items(): 329 | if LE_ADVERTISING_MANAGER_IFACE in props: 330 | return o 331 | 332 | return None 333 | 334 | 335 | def get_service_manager(bus): 336 | # Get the GattManager 337 | adapter_gattmanager = find_adapter_gattmanager(bus) 338 | if not adapter_gattmanager: 339 | print('GattManager1 interface not found') 340 | return 341 | 342 | service_manager = dbus.Interface( 343 | bus.get_object(BLUEZ_SERVICE_NAME, adapter_gattmanager), 344 | GATT_MANAGER_IFACE) 345 | 346 | return service_manager 347 | 348 | 349 | def get_ad_manager(bus): 350 | # Get the AdapterManager 351 | adapter_advertisingmanager = find_adapter_advertisingmanager(bus) 352 | if not adapter_advertisingmanager: 353 | print('LEAdvertisingManager1 interface not found') 354 | return 355 | 356 | adapter_props = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, adapter_advertisingmanager), 357 | "org.freedesktop.DBus.Properties") 358 | 359 | adapter_props.Set("org.bluez.Adapter1", "Powered", dbus.Boolean(1)) 360 | 361 | ad_manager = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, adapter_advertisingmanager), 362 | LE_ADVERTISING_MANAGER_IFACE) 363 | 364 | return ad_manager 365 | -------------------------------------------------------------------------------- /COPYING.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. --------------------------------------------------------------------------------