WiPy, send and receive bytes from service characteristic



  • Hello all,

    I have two WiPy 3.0 units and I have been tasked with sending data between them.

    For starters, I chose one to be the client, the other to be the server, and I wish to read a byte from the server's characteristic and send it to the client. For the server, I am using the example code in the API example.

    For the client:

    if BluetoothConnection.isconnected():   #check if we are already connected (from previous run)
        services = BluetoothConnection.services()
        print("Available services are:")
        for service in services:
            print(service.uuid())
        print("-----------------------------")
    
        if type(service.uuid()) == bytes:
            print('Reading chars from service = {}'.format(service.uuid()))
        else:
            print('Reading chars from service = %x' % service.uuid())
        chars = service.characteristics()
        for char in chars:
            if (char.properties() & Bluetooth.PROP_READ):
                print('char {} value = {}'.format(char.uuid(), char.read()))
    

    It works, I get 0xF1, 0xF2, 0xF3 ect. for each additional time I run the code on the client. But, if I have several services on the server, how do I specify which service I want to read chars from? I think the answer is in this line:

    chars = service.characteristics()
    

    Is there a way to pass the UUID of the service to the function characteristics()?

    FYI, I know little of Python and nothing of bluetooth BLE except for what I have been reading in the API.

    Thanks,
    Nick



  • Thanks! That answers my question!



  • @nick9995
    It's not currently possible to request a specific characteristic UUID, you need to iterate through the list of characteristics and look for characteristic.uuid() matches the one you're interested in. (also remember .uuid() returns the bytes representing the UUID, not an UUID string)


Log in to reply
 

Pycom on Twitter