Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 54 lines (39 sloc) 1.45 KB
from bluepy import btle
import struct
import pexpect
import datetime
# String to send data to openhab
send2OH = 'curl --header "Content-Type: text/plain" --request PUT --data "%s"'
# Timeout in minutes
timeout = 1
def listAll(p):
for svc in p.getServices():
for ch in svc.getCharacteristics():
print(" " + str(ch.valHandle) + ": " + ch.uuid.getCommonName())
class MyDelegate(btle.DefaultDelegate):
def __init__(self):
def handleNotification(self, cHandle, data):
#print("A notification was received: " + data + "<-- data")
# Get battery level
svc = p.getServiceByUUID( "180F" )
chBat = svc.getCharacteristics()[0]
batVal = struct.unpack('b',[0] %(str(batVal), "Battery_Level"))
p = btle.Peripheral("f2:5c:dc:a2:94:ea", btle.ADDR_TYPE_RANDOM)
p.setDelegate( MyDelegate() )
# Setup to turn notifications on, e.g.
svc = p.getServiceByUUID( "e198000158b346e7b9a6bedfad46833c" )
ch = svc.getCharacteristics()[0]
p.writeCharacteristic(ch.valHandle+1, "\x02\x00")
while True:
if p.waitForNotifications(int(timeout*60)):
# handleNotification() was called
print(str('.')[0] + ": Waited for %i min without notification..." %(timeout))
# Perhaps do something else here
You can’t perform that action at this time.