Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# coding: utf-8
# for iOS + Pythonista
import cb
import time
class MyCentralManagerDelegate (object):
def __init__(self):
self.peripheral = None
self.toggle = False
def did_discover_peripheral(self, p):
print '+++ Discovered peripheral: %s (%s)' % (, p.uuid)
if and 'GENUINO 101-0000' in and not self.peripheral:
# Keep a reference to the peripheral, so it doesn't get garbage-collected:
self.peripheral = p
def did_connect_peripheral(self, p):
print '*** Connected: %s' %
print 'Discovering services...'
def did_fail_to_connect_peripheral(self, p, error):
print 'Failed to connect'
def did_disconnect_peripheral(self, p, error):
print 'Disconnected, error: %s' % (error,)
self.peripheral = None
def did_discover_services(self, p, error):
for s in
if '19B10000-E8F2-537E-4F6C-D104768A1214' in s.uuid:
print '+++ LED Service'
def did_discover_characteristics(self, s, error):
if '19B10000-E8F2-537E-4F6C-D104768A1214' in s.uuid:
for c in s.characteristics:
if '19B10001-E8F2-537E-4F6C-D104768A1214' in c.uuid:
print 'LED On...'
self.peripheral.write_characteristic_value(c, chr(0x01), True)
def did_write_value(self, c, error):
if self.toggle :
self.peripheral.write_characteristic_value(c, chr(0x01), True)
print 'LED On...'
self.peripheral.write_characteristic_value(c, chr(0x00), True)
print 'LED Off...'
self.toggle = not self.toggle
delegate = MyCentralManagerDelegate()
print 'Scanning for peripherals...'
# Keep the connection alive until the 'Stop' button is pressed:
while True: pass
except KeyboardInterrupt:
# Disconnect everything:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment