Skip to content

Instantly share code, notes, and snippets.

@GlennPegden2
Created November 14, 2020 17:25
Show Gist options
  • Save GlennPegden2/e0ff6d8e1ca05fe8f74d68d734e3cb6b to your computer and use it in GitHub Desktop.
Save GlennPegden2/e0ff6d8e1ca05fe8f74d68d734e3cb6b to your computer and use it in GitHub Desktop.
Cheap BLE Lights demo (magic string and UUID based on sniff via Bluefruit using Wireshark
import asyncio
import time
import copy
import random
from bleak import discover, BleakClient
async def _discoverDevices():
devices = await discover()
return devices
def _findLights(name):
print('Looking for BLE device')
loop = asyncio.get_event_loop()
devices = loop.run_until_complete(_discoverDevices())
for d in devices:
if d.name == name:
print(f'Found {d.address}')
return d.address
return None
def _getMagicString(r,g,b):
hexstr = '7eff0503' + '{:02x}'.format(r) +'{:02x}'.format(g) +'{:02x}'.format(b) +'ffef'
return hexstr
async def _doPattern(address,n):
try:
async with BleakClient(address) as client:
if n ==1:
while True:
command = _getMagicString(random.randint(0,255),random.randint(0,255),random.randint(0,255))
await client.write_gatt_char("0000ffe1-0000-1000-8000-00805f9b34fb", bytes.fromhex(command))
time.sleep(0.5)
else:
await client.write_gatt_char("0000ffe1-0000-1000-8000-00805f9b34fb", bytes.fromhex(_getMagicString(0,0,0)))
time.sleep(1.5)
await client.write_gatt_char("0000ffe1-0000-1000-8000-00805f9b34fb", bytes.fromhex(_getMagicString(255,0,0)))
time.sleep(1.5)
await client.write_gatt_char("0000ffe1-0000-1000-8000-00805f9b34fb", bytes.fromhex(_getMagicString(0,255,0)))
time.sleep(1.5)
await client.write_gatt_char("0000ffe1-0000-1000-8000-00805f9b34fb", bytes.fromhex(_getMagicString(0,0,255)))
except:
print("Send failed")
mac = _findLights("LEDBLE-0D5356")
if mac:
loop = asyncio.get_event_loop()
print("Starting test pattern")
loop.run_until_complete(_doPattern(mac,2))
print("Starting random pattern")
loop.run_until_complete(_doPattern(mac,1))
else:
print('Unable to find device')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment