Skip to content

Commit

Permalink
Add differences in synchronization primitives for different OS
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincar committed Jun 26, 2023
1 parent 623a56d commit fd29197
Showing 1 changed file with 33 additions and 42 deletions.
75 changes: 33 additions & 42 deletions examples/server.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@

"""
Example for a BLE 4.0 Server
"""

import sys
import logging
import asyncio
import threading

from typing import Any
from typing import Any, Union

from bless import ( # type: ignore
BlessServer,
BlessGATTCharacteristic,
GATTCharacteristicProperties,
GATTAttributePermissions
)
BlessServer,
BlessGATTCharacteristic,
GATTCharacteristicProperties,
GATTAttributePermissions,
)

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(name=__name__)
trigger: threading.Event = threading.Event()

# NOTE: Some systems require different synchronization methods.
trigger: Union[asyncio.Event, threading.Event]
if sys.platform in ["darwin", "win32"]:
trigger = threading.Event()
else:
trigger = asyncio.Event()


def read_request(
characteristic: BlessGATTCharacteristic,
**kwargs
) -> bytearray:
def read_request(characteristic: BlessGATTCharacteristic, **kwargs) -> bytearray:
logger.debug(f"Reading {characteristic.value}")
return characteristic.value


def write_request(
characteristic: BlessGATTCharacteristic,
value: Any,
**kwargs
):
def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs):
characteristic.value = value
logger.debug(f"Char value set to {characteristic.value}")
if characteristic.value == b'\x0f':
if characteristic.value == b"\x0f":
logger.debug("NICE")
trigger.set()

Expand All @@ -56,38 +54,31 @@ async def run(loop):
# Add a Characteristic to the service
my_char_uuid = "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B"
char_flags = (
GATTCharacteristicProperties.read |
GATTCharacteristicProperties.write |
GATTCharacteristicProperties.indicate
)
permissions = (
GATTAttributePermissions.readable |
GATTAttributePermissions.writeable
)
GATTCharacteristicProperties.read
| GATTCharacteristicProperties.write
| GATTCharacteristicProperties.indicate
)
permissions = GATTAttributePermissions.readable | GATTAttributePermissions.writeable
await server.add_new_characteristic(
my_service_uuid,
my_char_uuid,
char_flags,
None,
permissions)

logger.debug(
server.get_characteristic(
my_char_uuid
)
)
my_service_uuid, my_char_uuid, char_flags, None, permissions
)

logger.debug(server.get_characteristic(my_char_uuid))
await server.start()
logger.debug("Advertising")
logger.info(f"Write '0xF' to the advertised characteristic: {my_char_uuid}")
trigger.wait()
if trigger.__module__ == "threading":
trigger.wait()
else:
await trigger.wait()

await asyncio.sleep(2)
logger.debug("Updating")
server.get_characteristic(my_char_uuid)
server.update_value(
my_service_uuid, "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B"
)
server.update_value(my_service_uuid, "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B")
await asyncio.sleep(5)
await server.stop()


loop = asyncio.get_event_loop()
loop.run_until_complete(run(loop))

0 comments on commit fd29197

Please sign in to comment.