Esempio n. 1
0
    def did_update_value_for_characteristic(
        self,
        peripheral: CBPeripheral,
        characteristic: CBCharacteristic,
        value: bytes,
        error: NSError,
    ):
        cUUID = characteristic.UUID().UUIDString()
        c_handle = characteristic.handle()
        if error is not None:
            raise BleakError(
                "Failed to read characteristic {}: {}".format(cUUID, error)
            )

        notify_callback = self._characteristic_notify_callbacks.get(c_handle)
        if notify_callback:
            notify_callback(c_handle, value)

        logger.debug("Read characteristic value")
        event = self._characteristic_read_events.get(cUUID)
        if event:
            event.set()
        else:
            # only expected on read
            pass
Esempio n. 2
0
    async def stopNotify_(self, characteristic: CBCharacteristic) -> bool:
        c_handle = characteristic.handle()
        if c_handle not in self._characteristic_notify_callbacks:
            raise ValueError("Characteristic notification never started")

        event = self._characteristic_notify_change_events.get_cleared(c_handle)
        self.peripheral.setNotifyValue_forCharacteristic_(False, characteristic)
        # wait for peripheral_didUpdateNotificationStateForCharacteristic_error_ to set event
        # await event.wait()

        self._characteristic_notify_callbacks.pop(c_handle)

        return True
Esempio n. 3
0
    async def startNotify_cb_(self, characteristic: CBCharacteristic,
                              callback: Callable[[str, Any], Any]) -> bool:
        c_handle = characteristic.handle()
        if c_handle in self._characteristic_notify_callbacks:
            raise ValueError("Characteristic notifications already started")

        self._characteristic_notify_callbacks[c_handle] = callback

        event = self._characteristic_notify_change_events.get_cleared(c_handle)
        self.peripheral.setNotifyValue_forCharacteristic_(True, characteristic)
        # wait for peripheral_didUpdateNotificationStateForCharacteristic_error_ to set event
        # await event.wait()

        return True
Esempio n. 4
0
    def did_update_notification_for_characteristic(
            self, peripheral: CBPeripheral, characteristic: CBCharacteristic,
            error: NSError):
        cUUID = characteristic.UUID().UUIDString()
        c_handle = characteristic.handle()
        if error is not None:
            raise BleakError(
                "Failed to update the notification status for characteristic {}: {}"
                .format(cUUID, error))
        logger.debug("Character Notify Update")

        event = self._characteristic_notify_change_events.get(c_handle)
        if event:
            event.set()
        else:
            logger.warning(
                "Unexpected event didUpdateNotificationStateForCharacteristic")