Skip to content

Instantly share code, notes, and snippets.

@JetForMe
Last active August 30, 2024 22:09
Show Gist options
  • Save JetForMe/a83d59ac4d8baa49bbe11bb5c93cfe56 to your computer and use it in GitHub Desktop.
Save JetForMe/a83d59ac4d8baa49bbe11bb5c93cfe56 to your computer and use it in GitHub Desktop.
Custom CircuitPython BLE Service
import board
import os
import time
from adafruit_ble import Service
from adafruit_ble import BLERadio
from adafruit_ble.advertising import Advertisement
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
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 = BLERadio()
gBLE.name = "Countdown Clock BLE"
gService = CountdownClockService()
gAdv = ProvideServicesAdvertisement(gService)
#gAdv = Advertisement()
gAdv.short_name = "ClockSvc"
def BLETask():
gBLE.stop_advertising()
while True:
gBLE.start_advertising(gAdv)
print("Advertising")
while not gBLE.connected:
time.sleep(0.100)
pass
print("Stopping Advertising")
gBLE.stop_advertising()
while gBLE.connected:
print("BLE connected")
cs = gService.countdownStart
if cs is not None:
print(f"countdown start is { cs }")
time.sleep(0.500)
while True:
BLETask()
print("main exited")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment