def _from_bleak( cls, connection: _bleio.connection.Connection, bleak_gatt_service: BleakGATTService, ) -> Service: service = cls(UUID(bleak_gatt_service.uuid), remote=True) service._connection = connection service._characteristics = tuple( Characteristic._from_bleak(service, bleak_characteristic) for bleak_characteristic in bleak_gatt_service.characteristics) service._bleak_gatt_service = bleak_gatt_service return service
def _from_bleak(cls, characteristic: Characteristic, bleak_descriptor: BleakGATTDescriptor): desc = Descriptor.add_to_characteristic( characteristic=characteristic, uuid=UUID(bleak_descriptor.uuid), read_perm=Attribute.OPEN, write_perm=Attribute.OPEN, ) # pylint: disable=protected-access desc._bleak_gatt_descriptor = bleak_descriptor # pylint: enable=protected-access return desc
def _from_bleak(cls, service: "Service", _bleak_characteristic: BleakGATTCharacteristic): properties = 0 for prop in _bleak_characteristic.properties: properties |= GattCharacteristicsFlags[prop.replace("-", "_")].value charac = Characteristic.add_to_service( service=service, uuid=UUID(_bleak_characteristic.uuid), properties=properties, read_perm=Attribute.OPEN, write_perm=Attribute.OPEN, ) # pylint: disable=protected-access charac._bleak_gatt_characteristic = _bleak_characteristic # pylint: enable=protected-access return charac
async def _discover_remote_services_async( self, service_uuids_whitelist: Optional[Iterable] = None ) -> Tuple[Service]: """Do BLE discovery for all services or for the given service UUIDS, to find their handles and characteristics, and return the discovered services. `Connection.connected` must be True. :param iterable service_uuids_whitelist: an iterable of :py:class:~`UUID` objects for the services provided by the peripheral that you want to use. The peripheral may provide more services, but services not listed are ignored and will not be returned. If service_uuids_whitelist is None, then all services will undergo discovery, which can be slow. If the service UUID is 128-bit, or its characteristic UUID's are 128-bit, you you must have already created a :py:class:~`UUID` object for that UUID in order for the service or characteristic to be discovered. Creating the UUID causes the UUID to be registered for use. (This restriction may be lifted in the future.) :return: A tuple of `Service` objects provided by the remote peripheral. """ # Fetch the services. bleak_services = await self.__bleak_client.get_services() # pylint: disable=protected-access if service_uuids_whitelist: filtered_bleak_services = tuple( s for s in bleak_services if UUID(s.uuid) in service_uuids_whitelist) else: filtered_bleak_services = bleak_services return tuple( Service._from_bleak(self, bleak_service) for bleak_service in filtered_bleak_services)