Hey guys,
I'm hoping you can help. I'm pretty stuck. I have created a UI that has a button on it. When I press the button I want it to send a character to my arduino device. The example below works perfect when I run it. The problem is, I can't figure out how to put this code, self.peripheral.write_characteristic_value(c, 'H', False), into the def button function I have so that it send the character when the button is pressed, instead of when it connects. Any help is very much appreciated. I have been stuck on this for about 2 days now. Thanks!
import cb
class MyCentralManagerDelegate (object):
def __init__(self):
self.peripheral = None
def did_discover_peripheral(self, p):
if p.name == 'HM-10-BLE' and not self.peripheral:
print 'Discovered ' + p.name
self.peripheral = p
cb.connect_peripheral(self.peripheral)
def did_connect_peripheral(self, p):
print 'Connected Peripheral ' + p.name
print 'Looking for Service FFE0'
p.discover_services()
def did_discover_services(self, p, error):
for s in p.services:
if s.uuid == 'FFE0':
print 'Found Service ' + s.uuid
print 'Looking for Characteristic FFE1'
p.discover_characteristics(s)
def did_discover_characteristics(self, s, error):
for c in s.characteristics:
if c.uuid == 'FFE1':
print 'Found Characteristic ' + c.uuid
print 'Writing H'
self.peripheral.write_characteristic_value(c, 'H', False)
cb.set_central_delegate( MyCentralManagerDelegate() )
print 'Looking for HM-10-BLE module'
cb.scan_for_peripherals()
# Keep the connection alive until the 'Stop' button is pressed:
try:
while True: pass
except KeyboardInterrupt:
# Disconnect everything:
cb.reset()