async def setup(): nonlocal cb_char await pmd.wait_for_powered_on(5) # Setup Service cbid: CBUUID = CBUUID.alloc().initWithString_(service_id) service: CBMutableService = ( CBMutableService.alloc().initWithType_primary_(cbid, True) ) # Add a subscribable Characteristic props: GattCharacteristicsFlags = ( GattCharacteristicsFlags.read | GattCharacteristicsFlags.write | GattCharacteristicsFlags.notify ) permissions: CBAttributePermissions = ( CBAttributePermissions.readable | CBAttributePermissions.writeable ) cb_char_id: CBUUID = CBUUID.alloc().initWithString_(char_id) cb_char = ( CBMutableCharacteristic.alloc() .initWithType_properties_value_permissions_( cb_char_id, props.value, None, permissions.value ) ) service.setCharacteristics_([cb_char]) await pmd.addService(service) assert pmd._services_added_events[cbid.UUIDString()].is_set() # Verify that we're not yet advertising assert pmd.is_advertising() is False # Start Advertising advertisement_data: Dict[str, Any] = { "kCBAdvDataServiceUUIDs": [cbid], "kCBAdvDataLocalName": "TestPeripheral" } try: await pmd.startAdvertising_(advertisement_data, timeout=5) except asyncio.exceptions.TimeoutError: await setup()
async def init(self): """ Initailize the CoreBluetooth Service object """ service_uuid: CBUUID = CBUUID.alloc().initWithString_(self._uuid) cb_service: CBMutableService = CBMutableService.alloc( ).initWithType_primary_(service_uuid, True) # Cannot call this because of handle issue # super(BlessGATTService, self).__init__(obj=cb_service) self.obj = cb_service
def cb_uuid_to_str(_uuid: CBUUID) -> str: """Converts a CoreBluetooth UUID to a Python string. If ``_uuid`` is a 16-bit UUID, it is assumed to be a Bluetooth GATT UUID (``0000xxxx-0000-1000-8000-00805f9b34fb``). Args _uuid: The UUID. Returns: The UUID as a lower case Python string (``xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx``) """ _uuid = _uuid.UUIDString() if len(_uuid) == 4: return "0000{0}-0000-1000-8000-00805f9b34fb".format(_uuid.lower()) # TODO: Evaluate if this is a necessary method... # elif _is_uuid_16bit_compatible(_uuid): # return _uuid[4:8].lower() else: return _uuid.lower()
async def init(self): """ Initializes the backend-specific characteristic object and stores it in self.obj """ properties_value: int = self._properties.value permissions_value: int = self._permissions.value cb_uuid: CBUUID = CBUUID.alloc().initWithString_(self._uuid) cb_characteristic: CBMutableCharacteristic = ( CBMutableCharacteristic.alloc(). initWithType_properties_value_permissions_(cb_uuid, properties_value, self._initial_value, permissions_value)) super(BlessGATTCharacteristic, self).__init__(obj=cb_characteristic)
def _convert_int_to_uuid(i: int) -> str: UUID_bytes = i.to_bytes(length=16, byteorder="big") UUID_data = NSData.alloc().initWithBytes_length_(UUID_bytes, len(UUID_bytes)) UUID_cb = CBUUID.alloc().initWithData_(UUID_data) return UUID_cb.UUIDString().lower()
def _convert_uuid_to_int(_uuid: str) -> int: UUID_cb = CBUUID.alloc().initWithString_(_uuid) UUID_data = UUID_cb.data() UUID_bytes = UUID_data.getBytes_length_(None, len(UUID_data)) UUID_int = int.from_bytes(UUID_bytes, byteorder="big") return UUID_int