[Python] Is it possible to have more than one client connecting to python BLE server at once?

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I've been testing the bless-library for BLE. This is the server code I've copied:

    import sys
    import logging
    import asyncio
    import threading

    from typing import Any, Union

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

    logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger(name=__name__)

    # 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:
    logger.debug(f"Reading {characteristic.value}")
    return characteristic.value


    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":
    logger.debug("NICE")
    trigger.set()


    async def run(loop):
    trigger.clear()
    # Instantiate the server
    my_service_name = "Test Service"
    server = BlessServer(name=my_service_name, loop=loop)
    server.read_request_func = read_request
    server.write_request_func = write_request

    # Add Service
    my_service_uuid = "A07498CA-AD5B-474E-940D-16F1FBE7E8CD"
    await server.add_new_service(my_service_uuid)

    # 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
    await server.add_new_characteristic(
    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}")
    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")
    await asyncio.sleep(5)
    await server.stop()


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


    It's avaivable here.

    While multiple clients can connect and disconnect to it, they don't seem able do it at the same time. Other clients will have to disconnect first. Would it be possible do do so in some way with this library or, some other one? Is it possible with BLE at all?

    I want to connect to a python BLE server with multiple clients.

    Continue reading...

Compartilhe esta Página