Example #1
0
class Reader:
    def __init__(self, address):
        self.requester = GATTRequester(address, False)
        self.connect()
        self.request_name()

        characteristics = self.requester.discover_characteristics()
        for ch in characteristics:
            print(ch)

        data = self.requester.read_by_handle(0x000e)
        print(data)

    def connect(self):
        print("Connecting...", end=" ")
        sys.stdout.flush()

        self.requester.connect(True, channel_type="random")
        print("OK.")

    def request_name(self):
        data = self.requester.read_by_uuid(
            "00002a00-0000-1000-8000-00805f9b34fb")[0]
        try:
            print("Device name:", data.decode("utf-8"))
        except AttributeError:
            print("Device name:", data)

    def onNotification(self, args):
        pass
Example #2
0
class BleClient:
    def __init__(self, address):
        self.requester = GATTRequester(address, False)
        self.connect()
        self.request_data()

    def connect(self):
        print("~ BLE ~ Connecting...", end=' ')
        sys.stdout.flush()

        self.requester.connect(True)
        print("~ BLE ~ OK!")

        time.sleep(1)

    def disconnect(self):
        print("~ BLE ~ Disconnecting...", end=' ')
        sys.stdout.flush()

        self.requester.disconnect()
        print("~ BLE ~ OK!")

        time.sleep(1)

    def request_data(self, uuid="00002a00-0000-1000-8000-00805f9b34fb"):
        data = self.requester.read_by_uuid(uuid)[0]
        try:
            print("~ BLE ~ Device name:", data.decode("utf-8"))
        except AttributeError:
            print("~ BLE ~ Device name:", data)
        else:
            return data

    def wait_disconnection(self):
        status = "connected" if self.requester.is_connected(
        ) else "not connected"
        print("~ BLE ~ Checking current status: {}".format(status))
        print(
            "\n~ BLE ~Now, force a hardware disconnect. To do so, please switch off,\n"
            "reboot or move away your device. Don't worry, I'll wait...")

        while self.requester.is_connected():
            time.sleep(1)

        print("\n~ BLE ~OK. Current state is disconnected. Congratulations ;)")
    def OnBPButtonClick(self):
	
	   f = open("bp.csv", 'wt')
	   writer = csv.writer(f)
	   writer.writerow( ('sys', 'dia','pulse') )
	   req = GATTRequester("98:4F:EE:0F:59:D6")
	   req.write_by_handle(0x000e,str(bytearray([02])))
	   tt = req.read_by_handle(0x0010)[0]
	   pp = []
	   for c in tt:
		  pp.append(ord(c))
	   print pp
	   if(pp[1] == 2):
		
		while(1):
			try:
				tt = req.read_by_handle(0x0010)[0]
				pp = []
				for c in tt:
					pp.append(ord(c))
				if(pp[0] == 3):
					break
			except Exception,e:
				print e
		
		try:
			name = req.read_by_uuid("2A40")[0]
			#steps = (req.read_by_handle(0x0009)[0])
			print type(name)
		
			value = []
			for c in name:
				value.append((c))
			print value
			print "sys :"+value[1]+value[2]+value[3]+"\n"
			print "dia :"+value[6]+value[7]+value[8]+"\n"
			print "sys :"+value[11]+value[12]+value[13]+"\n"
			writer.writerow((value[1]+value[2]+value[3],value[6]+value[7]+value[8],value[11]+value[12]+value[13]))
		
				
		except Exception,e:
			#name = False
			print e
				if (dataPoints % dataPointsPerMovementIteration == 0 or dataPoints == 0) and classIndex > 3:
					if speech == True and countDown == True:

						playTextToSpeech(str("<speak>Get ready!</speak>"))
						time.sleep(afterSpeechDelay)
						playTextToSpeech(str("<speak>" + str((dataPoints / dataPointsPerMovementIteration) + 1) + "</speak>"))				
				data = [[] for i in range(20)] # Accel
				data2 = [[] for i in range(20)] # Gyro
				data3 = [[] for i in range(20)] # Steps, temp
				data4 = [[] for i in range(20)] # Accel2
				#data5 = [[] for i in range(20)] # Gyro2
				#data6 = [[] for i in range(20)] # Magnetometer

		        	for i in range(bufferSize): # Read IMU data # TODO in between delay
	        		        data[i] = req.read_by_uuid("3a19")[0]
					data2[i] = req.read_by_uuid("3a20")[0]
					#time.sleep(publishDelay)
					data3[i] = req.read_by_uuid("3a21")[0]
					data4[i] = req.read_by_uuid("3a22")[0]
					#data5[i] = req.read_by_uuid("3a23")[0]
					#data6[i] = req.read_by_uuid("3a24")[0]
					#time.sleep(publishDelay)
					#print("data4[i] length: ", len(data4[0]))
				
				
				if (dataPoints % dataPointsPerMovementIteration == 0 or dataPoints == 0) and classIndex > 3:
					if speech == True and countDown == True:
						playTextToSpeech(str("<speak>OK!</speak>"))

				imuPacketList = []
Example #5
0
bAddr = ""
for dev in devices:
    if "6c:72:20" in dev.addr and dev.getValueText(1) and dev.getValueText(
            7) and dev.getValueText(9):
        bAddr = dev.addr
        print "[+] Komfy switch found: %s (%s), RSSI=%d dB" % (
            dev.addr, dev.addrType, dev.rssi)
if not bAddr:
    print "No Komfy switches found"
    sys.exit(1)

req = GATTRequester(bAddr.encode('ascii', 'ignore'), False, 'hci0')
req.connect(True, 'public', 'none', 0, 78)

#request SSID
wifiSsid = req.read_by_uuid("0xb006")[0]
reg = re.search(r"(:\s\"(.*)\")", wifiSsid)
wifiSsid = reg.groups()[1].replace("\\", "")

#request komfy encoded wifi password
wifiPassKomfy64 = req.read_by_uuid("0xb007")[0]
reg = re.search(r"(:\s\"(.*)\")", wifiPassKomfy64)
wifiPassKomfy64 = reg.groups()[1].replace("\\", "")

#convert password to real base64
wifiPassBase64 = ""
for char in wifiPassKomfy64:
    i = komfy64Alphabet.index(char)
    wifiPassBase64 += base64Alphabet[i]

wifiPass = base64.b64decode(wifiPassBase64)
Example #6
0
from gattlib import GATTRequester, DiscoveryService
service = DiscoveryService()
devices = service.discover(2)
address = None
for addr, name in devices.items():
    if "CA" in addr:
        address = addr

print(address)

req = GATTRequester(address)
try:
    name = req.read_by_uuid('8888')[0]
    print("uuid name ", name)
except:
    print("not uuid")

try:
    name = req.read_by_handle('8888')[0]
    print("handle name ", name)
except:
    print("not handle")
print(req)

print(name)
Example #7
0
from gattlib import GATTRequester
import paho.mqtt.client as mqtt
from time import sleep

try:
    req = GATTRequester("3C:71:BF:FD:08:96")

    client = mqtt.Client()
    client.connect("mqtt.thingspeak.com", 1883, 60)

    channelId = "1046955"
    apiKey = "R2GFC1Q7A5Y999I0"  #Write
    #apiKey = "3CIKRBES3MDG9W7J" #Read

    while True:
        data = req.read_by_uuid("00002A37-0000-1000-8000-00805F9B34FB")[0]
        #data = req.read_by_handle(0x180F)[0]

        value_byte = int(data[1])

        print("bytes received:", end=' ')
        print(value_byte, end=' ')
        if (value_byte < 100):
            send_to_thingspeak = 0
            print("0. Darah rendah")
        elif (value_byte < 120):
            send_to_thingspeak = 1
            print("1. Normal")
        else:
            send_to_thingspeak = 2
            print("2. Darah tinggi")
Example #8
0
    return r.text

service = DiscoveryService()
devices = service.discover(2)

for addr, name in devices.items():
	print("%s (%s)" % (name,addr))
	print(name)
	if(name=="RapidPrototype"):
		req=GATTRequester(addr,False)
		req.connect(True)
		status="connected" if req.is_connected() else "not connectd"
		print(status)
		prevData="00"
		while(True):
			data=req.read_by_uuid("A495FF21-C5B1-4B44-B512-1370F02D74DE")
			data=str(data)
			data=data[4:6]
			if(data=="00" and prevData=="01"):
				print("Box is open")
				val = {
                                        "sensor_id": "pillbox",
                                        "value": 0
                                }
				post(host, val)
			if(data=="01" and prevData=="00"):
				print("Box is closed")
				val = {
                                        "sensor_id": "pillbox",
                                        "value": 1
                                }
from gattlib import GATTRequester
import time

from gattlib import DiscoveryService

service = DiscoveryService("hci0")
devices = service.discover(2)

print "Nearby devices :",devices.items()

req = GATTRequester("Bluetooth MAC Adress of Peripheral")
while 1:
	name = req.read_by_uuid("2a37")[0]
	value = []
	for c in name:
		value.append(ord(c))
	if(name):
		print "Sensor value : ",value[1]

	else:
		print "*************************************"
		#time.sleep(1)
Example #10
0
#!bin/python

from gattlib import GATTRequester

req = GATTRequester("E0:14:88:54:3D:51")
name = req.read_by_uuid("00002a01-0000-1000-8000-00805f9b34fb")[0]
steps = req.read_by_handle(0x15)[0]
from gattlib import GATTRequester
import time

from gattlib import DiscoveryService

service = DiscoveryService("hci0")
devices = service.discover(2)

print "Nearby devices :", devices.items()

req = GATTRequester("Bluetooth MAC Adress of Peripheral")
while 1:
    name = req.read_by_uuid("2a37")[0]
    value = []
    for c in name:
        value.append(ord(c))
    if (name):
        print "Sensor value : ", value[1]

    else:
        print "*************************************"
        #time.sleep(1)
Example #12
0
devices = scanner.scan(5.0)
bAddr = ""
for dev in devices:
    if "6c:72:20" in dev.addr and dev.getValueText(1) and dev.getValueText(7) and dev.getValueText(9):
        bAddr = dev.addr
        print "[+] Komfy switch found: %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)
if not bAddr:
    print "No Komfy switches found"
    sys.exit(1)

req = GATTRequester(bAddr.encode("ascii", "ignore"), False, "hci0")
req.connect(True, "public", "none", 0, 78)

# request SSID
wifiSsid = req.read_by_uuid("0xb006")[0]
reg = re.search(r"(:\s\"(.*)\")", wifiSsid)
wifiSsid = reg.groups()[1].replace("\\", "")

# request komfy encoded wifi password
wifiPassKomfy64 = req.read_by_uuid("0xb007")[0]
reg = re.search(r"(:\s\"(.*)\")", wifiPassKomfy64)
wifiPassKomfy64 = reg.groups()[1].replace("\\", "")

# convert password to real base64
wifiPassBase64 = ""
for char in wifiPassKomfy64:
    i = komfy64Alphabet.index(char)
    wifiPassBase64 += base64Alphabet[i]

wifiPass = base64.b64decode(wifiPassBase64)