Skip to content

Instantly share code, notes, and snippets.

@JetForMe
Created August 30, 2024 22:32
Show Gist options
  • Save JetForMe/6b7a646ba1ab987b23e615756ef4a23f to your computer and use it in GitHub Desktop.
Save JetForMe/6b7a646ba1ab987b23e615756ef4a23f to your computer and use it in GitHub Desktop.
CP BLE Central for Countdown Clock
import _bleio
import adafruit_ble
from adafruit_ble.advertising.standard import Advertisement
from adafruit_ble.services.standard.device_info import DeviceInfoService
from adafruit_ble import Service
from adafruit_ble.characteristics import Characteristic
from adafruit_ble.characteristics.int import Uint16Characteristic
from adafruit_ble.uuid import VendorUUID
class CountdownClockService(Service):
"""Countdown Clock Service"""
uuid = VendorUUID("F1B8ED4E-648B-4689-8A0C-4930BC3A2325")
countdownStart = Uint16Characteristic(max_value = 9 * 60 + 59,
uuid = VendorUUID("955CB941-F229-4AAB-81FB-44349AFB6CA7"),
properties = Characteristic.WRITE | Characteristic.READ)
def __init__(
self,
countdownStart = 10
) -> None:
super().__init__(
manufacturer = "Latency: Zero, LLC",
software_revision = "1.0",
model_number = "CC",
serial_number = "12345",
firmware_revision = "1.0",
hardware_revision = "1.0",
)
gBLE = adafruit_ble.BLERadio()
gConnection = None
while True:
print("Scanning...")
for adv in gBLE.start_scan(Advertisement, timeout=5):
name = adv.complete_name
if name == "Countdown Clock BLE":
print(f"Found device: { name }")
gConnection = gBLE.connect(adv)
break
gBLE.stop_scan()
try:
if gConnection and gConnection.connected:
print("Connected")
if DeviceInfoService in gConnection:
dis = gConnection[DeviceInfoService]
print(f"Man: { dis.manufacturer }")
else:
print("No device info")
service = gConnection[CountdownClockService]
service.countdownStart = 25
print("Wrote countdown start, exting")
break
except ConnectionError:
try:
gConnection.disconnect()
except ConnectionError:
pass
gConnection = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment