class Nuimo: def __init__(self, macAddress='FA:48:12:00:CA:AC'): self.macAddress = macAddress def connect(self): try: self.peripheral = Peripheral(self.macAddress, addrType='random') except BTLEException: return False try: self.enableNotifications() self.peripheral.setDelegate(NuimoDelegate()) except BTLEException: self.peripheral.disconnect() self.peripheral = None return False return True def enableNotifications(self): self.peripheral.writeCharacteristic(CLICK_NOTIFICATION_HANDLE, NOTIFICATION_ON) self.peripheral.writeCharacteristic(BATTERY_NOTIFICATION_HANDLE, NOTIFICATION_ON) self.peripheral.writeCharacteristic(FLY_NOTIFICATION_HANDLE, NOTIFICATION_ON) self.peripheral.writeCharacteristic(SWIPE_NOTIFICATION_HANDLE, NOTIFICATION_ON) self.peripheral.writeCharacteristic(ROTATION_NOTIFICATION_HANDLE, NOTIFICATION_ON) def waitForNotifications(self): try: self.peripheral.waitForNotifications(1.0) return True except BTLEException as e: return False def displayLedMatrix(self, matrix, brightness, timeout): self.peripheral.writeCharacteristic(LEDMATRIX_VALUE_HANDLE, struct.pack("BBBBBBBBBBBBB", 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff))
r_ch = p.getCharacteristics(uuid=read_uuid)[0] # If we can, continuously send lock and unlock requests if (r_ch.supportsRead()): while 1: # Tell the Lockitron to lock msg = struct.pack('i', MSG_LOCK) print "Writing: " + str(msg) w_ch.write(msg) time.sleep(3) # Tell the Lockitron to unlock msg = struct.pack('i', MSG_UNLOCK) print "Writing: " + str(msg) w_ch.write(msg) time.sleep(3) # Get lock state msg = struct.pack('i', MSG_STATE_REQ) print "Writing: " + str(msg) w_ch.write(msg) time.sleep(0.5) if (r_ch.supportsRead()): val = binascii.b2a_hex(r_ch.read()) print "Lock state: " + str(val) time.sleep(3) finally: p.disconnect()
import binascii import struct import time from bluepy.bluepy.btle import UUID, Peripheral temp_uuid = UUID(0x2A6E) p = Peripheral("98:4F:EE:0D:05:6A", "public") try: ch = p.getCharacteristics(uuid=temp_uuid)[0] if (ch.supportsRead()): while 1: print ch.read() val = binascii.b2a_hex(ch.read()) print val val = binascii.unhexlify(val) print val #val = struct.unpack('f', val)[0] #print str(val) + " deg C" time.sleep(1) finally: p.disconnect()