Skip to content

Instantly share code, notes, and snippets.

@nikhilm
Created October 24, 2021 22:13
Show Gist options
  • Save nikhilm/a47f2ba944e6b2c80b6bd5ad7d706581 to your computer and use it in GitHub Desktop.
Save nikhilm/a47f2ba944e6b2c80b6bd5ad7d706581 to your computer and use it in GitHub Desktop.
Adafruit Bluefruit BLE bluetooth proximity bike light
import time
import adafruit_ble
from adafruit_ble import BLERadio
from adafruit_ble.advertising import Advertisement
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.standard.device_info import DeviceInfoService
# from adafruit_ble.services.standard.hid import HIDService
from adafruit_circuitplayground import cp
from blink import blink
ble = BLERadio()
# hid = HIDService()
device_info = DeviceInfoService(software_revision=adafruit_ble.__version__,
manufacturer="Adafruit Industries")
advertisement = ProvideServicesAdvertisement(device_info)
advertisement.appearance = 0x7c2
scan_response = Advertisement()
scan_response.complete_name = "Tail light"
ble = adafruit_ble.BLERadio()
def try_to_connect():
if not ble.connected:
print("advertising")
ble.start_advertising(advertisement, scan_response)
while not ble.connected:
pass
else:
print("already connected")
print(ble.connections)
cp.pixels.brightness = 1
while True:
while not cp.switch:
time.sleep(5)
while not ble.connected:
try_to_connect()
print("Connected")
cp.red_led = 1
while cp.switch and ble.connected:
blink()
cp.red_led = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment