def __init__(self, *, service=None, secondary=False, **initial_values): if service is None: # pylint: disable=no-member self.bleio_service = _bleio.Service(self.uuid.bleio_uuid, secondary=secondary) elif not service.remote: raise ValueError( "Can only create services with a remote service or None") else: self.bleio_service = service # This internal dictionary is manipulated by the Characteristic descriptors to store their # per-Service state. It is NOT managed by the Service itself. It is an attribute of the # Service so that the lifetime of the objects is the same as the Service. self.bleio_characteristics = {} # Set the field name on all of the characteristic objects so they can replace themselves if # they choose. # TODO: Replace this with __set_name__ support. for class_attr in dir(self.__class__): if class_attr.startswith("__"): continue value = getattr(self.__class__, class_attr) if not isinstance(value, Characteristic) and not isinstance( value, ComplexCharacteristic): continue value.field_name = class_attr # Get or set every attribute to ensure that they are all bound up front. We could lazily # init them but the Nordic Soft Device requires characteristics be added immediately # after the Service. In other words, only characteristics for the most recently added # service can be added. if not self.remote: if class_attr in initial_values: setattr(self, class_attr, initial_values[class_attr]) else: getattr(self, class_attr)
adapter = _bleio.adapter if adapter_name is not None: adapter.name = adapter_name else: adapter.name = "" # Convert the adapter name into a scan response, so it shows up as something # other than "N/A". scan_response = None if adapter_name is not None: adapter_name_bytes= adapter_name.encode("utf-8") scan_response = bytearray([len(adapter_name_bytes) + 1, 8]) + adapter_name_bytes # Create the BLE service that other devices will connect to in order to discover # our contact tracing token. bt_service = _bleio.Service(bluetrace_uuid, secondary=False) print("Advertising data: ", hexlify(advertising_data)) def bind_to_service(service, uuid, token): properties = _bleio.Characteristic.READ | _bleio.Characteristic.WRITE read_perm = _bleio.Attribute.OPEN write_perm = _bleio.Attribute.OPEN return _bleio.Characteristic.add_to_service( service, uuid, initial_value = token, max_length = len(token), fixed_length = True, properties = properties, read_perm = read_perm, write_perm = write_perm )