Пример #1
0
class Reader(object):
    def __init__(self, address):
        self.requester = GATTRequester(address, False)
        self.connect()
        self.request_data()

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

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

    def request_data(self):
        data = self.requester.read_by_uuid("00002a00-0000-1000-8000-00805f9b34fb")[0]
        print("Device name: " + data.decode("utf-8"))
Пример #2
0
 def __init__(self, address):
     self.requester = GATTRequester(address, False)
     self.connect()
     self.request_data()
Пример #3
0
    def __init__(self, address):
        self.requester = GATTRequester(address, False)
        self.connect()
	t = threading.Thread(name=address,target=self.periodical_request)
        t.daemon = True
        t.start()
Пример #4
0
class Reader(object):
    def __init__(self, address):
        self.requester = GATTRequester(address, False)
        self.connect()
	t = threading.Thread(name=address,target=self.periodical_request)
        t.daemon = True
        t.start()

    def connect(self):
        self.requester.connect(True,"random","medium",0,32)
        print ("Connected")

    def request_uuid(self):
        self.uuid = self.requester.read_by_uuid("D6F8BDCC-3885-11E6-AC61-9E71128CAE77")[0]
        print("uuid read:", self.uuid)
        if len(self.uuid) < 37:
            self.uuid = self.uuid + self.requester.read_by_uuid("D6F8BDCC-3885-11E6-AC61-9E71128CAE77")[0]
        print("complete uuid:", self.uuid[0:35])   

    def periodical_request(self):
        tracked_devices.append(threading.current_thread().getName())
        try:
            self.request_uuid()
        except RuntimeError:
            tracked_devices.remove(threading.current_thread().getName())
            return
        count = 0
        while True:
            self.request_data()
            count = count + 1
            print("Count:",count)
            time.sleep(1)
    


    def request_data(self):
        if not self.requester.is_connected():
            print("Reconnecting")
            self.requester.disconnect()
            try:
                self.requester.connect(True,"random","medium",0,32)
            except RuntimeError:
                # End the thread bc cannot reconnect
                if threading.current_thread().getName() in tracked_devices:
                    # should be in the tracked devices list
                    # remove when thread is going to end
                    tracked_devices.remove(threading.current_thread().getName())
                else :
                    print("Error: thread should be tracked")
                print("Encountered error when reconnecting. Now exit thread.")
                sys.exit()
            print("Reconnected")
        try:
            start_time = current_milli_time()
            data = self.requester.read_by_uuid(
                "16864516-21E0-11E6-B67B-9E71128CAE77")[0]
        except RuntimeError as e:
            print("RuntimeError", e)
            self.requester.disconnect()
            return
        try:
            d = decode_string_to_double(data)
            print ("Device name:",self.uuid,"Device motion:", d) 
            if socket_open:
                socket_connect.sendMotionAndUUID(d,self.uuid)
            motion_manager.process_data(self.uuid[0:35], self.uuid[36],d)
        except AttributeError:
            print ("Device name: " + data)