Beautiful Blobfish
Beautiful Blobfish

Reputation: 23

Python Bleak Notifications With a BLE Rubik's Cube

I am using python 3.12 with bleak to try and communicate with a BLE rubik's cube. Currently all I can do is pull the model id with this code:

import asyncio
from bleak import BleakClient

address = "E0:F4:65:59:81:EF"
MODEL_NBR_UUID = "00002a00-0000-1000-8000-00805f9b34fb"

async def main(address):
    client = BleakClient(address)
    try:
        await client.connect()
        model_number = await client.read_gatt_char(MODEL_NBR_UUID)
        print("Model Number: {0}".format("".join(map(chr, model_number))))

    except Exception as e:
        print(e)
    finally:
        await client.disconnect()

asyncio.run(main(address))

This Works as Expected and prints the following out Model Number: GoCube_EF8159_1 but when I try to read or start notifications on other characteristics nothing seems to happen. I added notification code like this:

import asyncio
from bleak import BleakClient

address = "E0:F4:65:59:81:EF"
MODEL_NBR_UUID = "00002a00-0000-1000-8000-00805f9b34fb"


async def cube_update(sender, data):
    print(f"{sender}: {data}")
async def main(address):
    client = BleakClient(address)
    try:
        await client.connect()
        model_number = await client.read_gatt_char(MODEL_NBR_UUID)
        print("Model Number: {0}".format("".join(map(chr, model_number))))

        await client.start_notify("6e400003-b5a3-f393-e0a9-e50e24dcca9e", cube_update)

    except Exception as e:
        print(e)
    finally:
        await client.disconnect()

asyncio.run(main(address))

Even when I added this code, the output is the same and it finishes executing almost immediately after printing out the name. I do not believe I am using the wrong characteristic and more information about the cube's specific protocol can be found on this gituhb page. Additionally, I tested the notifications using a debugging app on my phone it notified my phone just fine. My main goal is to get the move notation every time the cube spins which I will eventually use to come up with a solution to the cube.

Upvotes: 0

Views: 104

Answers (1)

user28951219
user28951219

Reputation: 1

A couple points that could be helpful for you:

  1. In order to have the program run continuously, you'll need some kind of loop in the async def main body. This will allow the code to keep running and recieve notifications from the cube. I'd suggest adding an else: block with a while loop in your try-except-finally structure. Sample structure would read as:

try: 
  # Cases that could generate an exception
except: 
  # Handle exceptions
else: 
  while(condition that remains true as long as you want to read notifications): 
    # Code to do stuff
finally: 
  # Disconnect

  1. I had the same problem in a similar application where it didn't seem like the start_notify method was doing anything. After digging through the backend of bleak I found that it takes 1-2 seconds for the start_notify function to configure properly. If you call the start_notify in your try block the program should automatically wait until it's configured before proceeding. Keep that delay in mind before trying to send any info from the cube to your script

Hope this helps. I'm pretty new to wireless protocols myself, so still learning in bits and pieces.

Upvotes: 0

Related Questions