async def instance0_task(): service = aioble.Service(SERVICE_UUID) characteristic = aioble.Characteristic(service, CHAR_UUID, read=True, write=True, notify=True, indicate=True) aioble.register_services(service) multitest.globals(BDADDR=aioble.config("mac")) multitest.next() # Write initial characteristic value. characteristic.write("periph0") # Wait for central to connect to us. print("advertise") connection = await aioble.advertise(20_000, adv_data=b"\x02\x01\x06\x04\xffMPY", timeout_ms=TIMEOUT_MS) print("connected") # A # Wait for a write to the characteristic from the central, # then reply with a notification. await characteristic.written(timeout_ms=TIMEOUT_MS) print("written", characteristic.read()) print("write") characteristic.write("periph1") print("notify") characteristic.notify(connection) # B # Wait for a write to the characteristic from the central, # then reply with value-included notification. await characteristic.written(timeout_ms=TIMEOUT_MS) print("written", characteristic.read()) print("notify") characteristic.notify(connection, "periph2") # C # Wait for a write to the characteristic from the central, # then reply with an indication. await characteristic.written(timeout_ms=TIMEOUT_MS) print("written", characteristic.read()) print("write") characteristic.write("periph3") print("indicate") await characteristic.indicate(connection, timeout_ms=TIMEOUT_MS) # Wait for the central to disconnect. await connection.disconnected(timeout_ms=TIMEOUT_MS) print("disconnected")
def register_server(): server_service = aioble.Service(SERVICE_UUID) server_characteristic = aioble.Characteristic(server_service, CHAR_UUID, read=True, write=True, notify=True, indicate=True) aioble.register_services(server_service) return server_characteristic
async def instance0_task(): service = aioble.Service(SERVICE_UUID) characteristic = aioble.Characteristic(service, CHAR_UUID, read=True, notify=True) aioble.register_services(service) multitest.globals(BDADDR=aioble.config("mac")) multitest.next() # Wait for central to connect to us. print("advertise") connection = await aioble.advertise(20_000, adv_data=b"\x02\x01\x06\x04\xffMPY", timeout_ms=TIMEOUT_MS) print("connected") # Send a subscribed-write (but client isn't subscribed, won't send anything). multitest.wait("discovery") await asyncio.sleep_ms(100) characteristic.write("before-subscribe", send_update=True) # Send a subscribed-write (now client is subscribed, client should get notified). multitest.wait("subscribed") await asyncio.sleep_ms(100) characteristic.write("after-subscribe", send_update=True) # Send a subscribed-write (now client is unsubscribed, won't send anything). multitest.wait("unsubscribed") await asyncio.sleep_ms(100) characteristic.write("after-unsubscribe", send_update=True) # Send 5 direct notifications. multitest.wait("start-direct") for i in range(5): # Send 1 notification each time, except for 3 quick notifications the third time. # The client should only see the last one. for j in range(3 if i == 2 else 1): if j > 0: await asyncio.sleep_ms(100) msg = "direct-{}-{}".format(i, j) print("notify", msg) characteristic.notify(connection, msg) # Tell client to wait for notification. multitest.broadcast("notified") # Wait until client is ready for next notification. multitest.wait("next") # Wait for the central to disconnect. await connection.disconnected(timeout_ms=TIMEOUT_MS) print("disconnected")
async def instance0_task(): multitest.globals(BDADDR=aioble.config("mac")) multitest.next() for i in range(3): service = aioble.Service(SERVICE_UUID) characteristic = aioble.Characteristic(service, CHAR_UUID, read=True) aioble.register_services(service) # Write initial characteristic value. characteristic.write("periph{}".format(i)) multitest.broadcast("connect-{}".format(i)) # Wait for central to connect to us. print("advertise") connection = await aioble.advertise( 20_000, adv_data=b"\x02\x01\x06\x04\xffMPY", timeout_ms=TIMEOUT_MS ) print("connected") multitest.broadcast("connected-{}".format(i)) for j in range(3): channel = await connection.l2cap_accept(_L2CAP_PSN, _L2CAP_MTU) print("channel accepted") buf = bytearray(10) n = await channel.recvinto(buf) print("recv", n, buf[:n]) multitest.broadcast("recv-{}-{}".format(i, j)) await channel.disconnected(5000) print("channel disconnected") # Wait for the central to disconnect. await connection.disconnected(timeout_ms=TIMEOUT_MS) print("disconnected") # Shutdown aioble + modbluetooth. print("shutdown") aioble.stop()
async def instance0_task(): service = aioble.Service(SERVICE_UUID) characteristic = aioble.Characteristic( service, CHAR_UUID, write=True, ) # Second characteristic enabled write capture. characteristic_capture = aioble.Characteristic( service, CHAR_CAPTURE_UUID, write=True, capture=True, ) aioble.register_services(service) multitest.globals(BDADDR=aioble.config("mac")) multitest.next() # Wait for central to connect to us. print("advertise") async with await aioble.advertise(20_000, adv_data=b"\x02\x01\x06\x04\xffMPY", timeout_ms=TIMEOUT_MS) as connection: print("connected") # We should miss writes while we're sleeping. for i in range(2): await characteristic.written(timeout_ms=TIMEOUT_MS) print("written", characteristic.read()) await asyncio.sleep_ms(500) # Shouldn't miss any writes as they will be captured and queued. for i in range(5): write_connection, value = await characteristic_capture.written( timeout_ms=TIMEOUT_MS) print("written", value, write_connection == connection) await asyncio.sleep_ms(500)
_COMMAND_DONE = const(4) _STATUS_OK = const(0) _STATUS_NOT_IMPLEMENTED = const(1) _STATUS_NOT_FOUND = const(2) _L2CAP_PSN = const(22) _L2CAP_MTU = const(128) # Register GATT server. file_service = aioble.Service(_FILE_SERVICE_UUID) control_characteristic = aioble.Characteristic( file_service, _CONTROL_CHARACTERISTIC_UUID, write=True, notify=True ) aioble.register_services(file_service) send_file = None recv_file = None list_path = None op_seq = None l2cap_event = asyncio.Event() def send_done_notification(connection, status=_STATUS_OK): global op_seq control_characteristic.notify(connection, struct.pack("<BBB", _COMMAND_DONE, op_seq, status)) op_seq = None
_ENV_SENSE_UUID = bluetooth.UUID(0x181A) # org.bluetooth.characteristic.temperature _ENV_SENSE_TEMP_UUID = bluetooth.UUID(0x2A6E) # org.bluetooth.characteristic.gap.appearance.xml _ADV_APPEARANCE_GENERIC_THERMOMETER = const(768) # How frequently to send advertising beacons. _ADV_INTERVAL_MS = 250_000 # Register GATT server. temp_service = aioble.Service(_ENV_SENSE_UUID) temp_characteristic = aioble.Characteristic(temp_service, _ENV_SENSE_TEMP_UUID, read=True, notify=True) aioble.register_services(temp_service) # Helper to encode the temperature characteristic encoding (sint16, hundredths of a degree). def _encode_temperature(temp_deg_c): return struct.pack("<h", int(temp_deg_c * 100)) # This would be periodically polling a hardware sensor. async def sensor_task(): t = 24.5 while True: temp_characteristic.write(_encode_temperature(t)) t += random.uniform(-0.5, 0.5) await asyncio.sleep_ms(1000)