Example #1
0
 def peripheralManager_didReceiveReadRequest_(  # noqa: N802
         self, peripheral_manager: CBPeripheralManager,
         request: CBATTRequest):
     # This should probably be a callback to be handled by the user, to be
     # implemented or given to the BleakServer
     logger.debug(
         "Received read request from {} for characteristic {}".format(
             request.central().identifier().UUIDString(),
             request.characteristic().UUID().UUIDString()))
     request.setValue_(
         self.read_request_func(
             request.characteristic().UUID().UUIDString()))
     peripheral_manager.respondToRequest_withResult_(
         request, CBATTError.Success.value)
    def init(self):
        """macOS init function for NSObjects"""
        self = objc.super(PeripheralManagerDelegate, self).init()

        self.event_loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()

        self.peripheral_manager: CBPeripheralManager = (
            CBPeripheralManager.alloc().initWithDelegate_queue_(
                self,
                dispatch_queue_create(b"bleak.corebluetooth",
                                      DISPATCH_QUEUE_SERIAL),
            ))

        self._callbacks: Dict[str, Callable] = {}

        # Events
        self._powered_on_event: threading.Event = threading.Event()
        self._advertisement_started_event: asyncio.Event = asyncio.Event()
        self._services_added_events: Dict[str, asyncio.Event] = {}

        # Documentation requires that no calls be made until we can validate
        # that the bluetooth module is powered on
        self._powered_on_event.wait()

        self._central_subscriptions = {}

        if not self.compliant():
            logger.warning("PeripheralManagerDelegate is not compliant")

        return self
    def peripheralManager_didReceiveWriteRequests_(  # noqa: N802
            self, peripheral_manager: CBPeripheralManager,
            requests: List[CBATTRequest]):
        # Again, this should likely be moved to a callback
        logger.debug("Receving write requests...")
        for request in requests:
            central: CBCentral = request.central()
            char: CBCharacteristic = request.characteristic()
            value: bytearray = request.value()
            logger.debug("Write request from {} to {} with value {}".format(
                central.identifier().UUIDString(),
                char.UUID().UUIDString(), value))
            self.write_request_func(char.UUID().UUIDString(), value)

        peripheral_manager.respondToRequest_withResult_(
            requests[0], CBATTError.Success.value)
Example #4
0
    def did_update_state(self, peripheral_manager: CBPeripheralManager):
        if peripheral_manager.state() == CBManagerStateUnknown:
            logger.debug("Cannot detect bluetooth device")
        elif peripheral_manager.state() == CBManagerStateResetting:
            logger.debug("Bluetooth is resetting")
        elif peripheral_manager.state() == CBManagerStateUnsupported:
            logger.debug("Bluetooth is unsupported")
        elif peripheral_manager.state() == CBManagerStateUnauthorized:
            logger.debug("Bluetooth is unauthorized")
        elif peripheral_manager.state() == CBManagerStatePoweredOff:
            logger.debug("Bluetooth powered off")
        elif peripheral_manager.state() == CBManagerStatePoweredOn:
            logger.debug("Bluetooth powered on")

        if peripheral_manager.state() == CBManagerStatePoweredOn:
            self._powered_on_event.set()
        else:
            self._powered_on_event.clear()
            self._advertisement_started_event.clear()