def __init__(self, service_id, characteristic_id, uuid, value, notifying, flags, read_callback=None, write_callback=None, notify_callback=None): self.read_callback = read_callback self.write_callback = write_callback self.notify_callback = notify_callback # Setup D-Bus object paths and register service service_path = (f'{constants.BLUEZERO_DBUS_OBJECT}/' f'service{service_id:04d}') self.path = f'{service_path}/char{characteristic_id:04d}' self.bus = dbus.SystemBus() dbus.service.Object.__init__(self, self.bus, self.path) self.props = { constants.GATT_CHRC_IFACE: { 'UUID': uuid, 'Service': dbus_tools.get_dbus_obj(service_path), 'Value': dbus.Array(value, signature='y'), 'Notifying': notifying, 'Flags': flags } } for prop in self.props[constants.GATT_CHRC_IFACE].keys(): self.Set(constants.GATT_CHRC_IFACE, prop, self.props[constants.GATT_CHRC_IFACE][prop])
def adapter(self): """The address of the adapter the device belongs to.""" adapter_obj = dbus_tools.get_dbus_obj(self._adapter) adapter_props = dbus_tools.get_dbus_iface(dbus.PROPERTIES_IFACE, adapter_obj) return dbus_tools.get(adapter_props, constants.ADAPTER_INTERFACE, 'Address')
def test_get_property_default(self): self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device') path_obj = dbus_tools.get_dbus_obj('/org/bluez/hci0') result = dbus_tools.get(path_obj, 'org.bluez.Adapter1', 'address', 'xx') self.assertEqual('xx', result)
def __init__(self, device_addr): """Default initialiser. Creates the interface to the remote Bluetooth device. :param device_addr: Address of Bluetooth device player to use. """ self.player_path = _find_player_path(device_addr) self.player_object = dbus_tools.get_dbus_obj(self.player_path) self.player_methods = dbus_tools.get_dbus_iface( constants.MEDIA_PLAYER_IFACE, self.player_object) self.player_props = dbus_tools.get_dbus_iface(dbus.PROPERTIES_IFACE, self.player_object)
def _subscribe(self, chrc, cb): chrc.resolve_gatt() uuid = chrc.chrc_uuid path = dbus_tools.get_dbus_path(characteristic=uuid, device=self.device_addr, adapter=self.adapter_addr) obj = dbus_tools.get_dbus_obj(path) iface = dbus_tools.get_dbus_iface(constants.DBUS_PROP_IFACE, obj) iface.connect_to_signal('PropertiesChanged', cb) obj.StartNotify(reply_handler=lambda: logging.debug('subscribed'), error_handler=dbus_tools.generic_error_cb, dbus_interface=constants.GATT_CHRC_IFACE)
def __init__(self, service_id, characteristic_id, descriptor_id, uuid, value, flags): # Setup D-Bus object paths and register service char_path = (f'{constants.BLUEZERO_DBUS_OBJECT}/' f'service{service_id:04d}/char{characteristic_id:04d}') self.path = f'{char_path}/desc{descriptor_id:04d}' self.bus = dbus.SystemBus() dbus.service.Object.__init__(self, self.bus, self.path) self.props = { constants.GATT_DESC_IFACE: { 'UUID': uuid, 'Characteristic': dbus_tools.get_dbus_obj(char_path), 'Value': value, 'Flags': flags } } for prop in self.props[constants.GATT_DESC_IFACE].keys(): self.Set(constants.GATT_DESC_IFACE, prop, self.props[constants.GATT_DESC_IFACE][prop])
def start(self): """Start the application. This function performs the following steps to start the BLE peripheral application: 1. Registers the Bluetooth adapter and turns it on. 2. Gets the Bluez D-Bus advertising manager interface. 3. Gets the Bluez D-Bus gatt manager interface. 4. Creates an advertisement with the primary application service. 5. Registers the advertisement with the Bluez advertising manager. 6. Registers the application with the Bluez gatt manager. 7. Runs the program loop The application must first have had a service added to it. :Example: >>> app = peripheral.Application() >>> app.add_service(your_service) >>> app.start() It is good practice to put the ``app.start()`` in a ``try-except-finally`` block to enable keyboard interrupts. """ # Register the Bluetooth adapter self.dongle.powered = True # Setup the advertising manager print('setup ad_manager') self.ad_manager = dbus_tools.get_dbus_iface( constants.LE_ADVERTISING_MANAGER_IFACE, dbus_tools.get_dbus_obj(self.dongle.path)) # Setup the service manager print('setup service_manager') self.service_manager = dbus_tools.get_dbus_iface( constants.GATT_MANAGER_IFACE, dbus_tools.get_dbus_obj(self.dongle.path)) # Setup the advertisement self.service_ad = Advertisement(self, 'peripheral') for service in self.services: if service.primary: print('Advertising service ', service.uuid) self.service_ad.add_service_uuid(service.uuid) self.service_ad.ad_type = service.type if service.service_data is not None: print('Adding service data: ', service.uuid, service.service_data) self.service_ad.add_service_data(service.uuid, service.service_data) # Register the advertisement print('Register Advert', self.service_ad.get_path()) self.ad_manager.RegisterAdvertisement( self.service_ad.get_path(), {}, reply_handler=register_ad_cb, error_handler=register_ad_error_cb) # Register the application print('Register Application ', self.get_path()) self.service_manager.RegisterApplication( self.get_path(), {}, reply_handler=register_service_cb, error_handler=register_service_error_cb) try: # Run the mainloop self.mainloop.run() except KeyboardInterrupt: print('Closing Mainloop') self.mainloop.quit()
def test_get_property_exception(self): self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device') path_obj = dbus_tools.get_dbus_obj('/org/bluez/hci0') with self.assertRaises(dbus.exceptions.DBusException): result = dbus_tools.get(path_obj, 'org.bluez.AdapterXX', 'Address')
def test_get_property(self): self.dbusmock_bluez.AddAdapter('hci0', 'My-Test-Device') path_obj = dbus_tools.get_dbus_obj('/org/bluez/hci0') result = dbus_tools.get(path_obj, 'org.bluez.Adapter1', 'Address') self.assertEqual('00:01:02:03:04:05', result)