def handleNotification(self, hnd, data): if hnd == self.device._char_auth.getHandle(): if data[:3] == b'\x10\x01\x01': self.device._req_rdn() elif data[:3] == b'\x10\x01\x04': self.device.state = AUTH_STATES.KEY_SENDING_FAILED elif data[:3] == b'\x10\x02\x01': # 16 bytes random_nr = data[3:] self.device._send_enc_rdn(random_nr) elif data[:3] == b'\x10\x02\x04': self.device.state = AUTH_STATES.REQUEST_RN_ERROR elif data[:3] == b'\x10\x03\x01': self.device.state = AUTH_STATES.AUTH_OK elif data[:3] == b'\x10\x03\x04': self.device.status = AUTH_STATES.ENCRIPTION_KEY_FAILED self.device._send_key() else: self.device.state = AUTH_STATES.AUTH_FAILED elif hnd == self.device._char_heart_measure.getHandle(): self.device.queue.put((QUEUE_TYPES.HEART, data)) elif hnd == 0x38: # Not sure about this, need test if len(data) == 20 and struct.unpack('b', data[0])[0] == 1: self.device.queue.put((QUEUE_TYPES.RAW_ACCEL, data)) elif len(data) == 16: self.device.queue.put((QUEUE_TYPES.RAW_HEART, data)) #0x0041 elif hnd == 0x41: hexPayloadStr = str(data.encode("hex")) # print "01>" + hexPayloadStr + " len:" + str(len(data)) # self.device._log.info('data length:'+ str(len(data))) self.device.datapool["payload"].append(hexPayloadStr) self.idx = self.idx + 1 #Value elif hnd == 0x3e: #self.disconnect() hexStr = str(data.encode("hex")) # self.device._log.info('Response Status:'+ hexStr ) if hexStr == "100201": self.device._log.info('Response Status:' + hexStr) self.device.datapool["status"] = hexStr elif hexStr == "100204": self.device._log.info('Response Status:' + hexStr) self.device.datapool["status"] = hexStr elif hexStr.startswith("100101"): self.device._log.info('Response Status:' + hexStr) # add cisco. may be top of data time startDttm = utils.hexbin2dttm(data, 14) self.device.datapool["StartDttm"] = startDttm else: self.device.datapool["status"] = hexStr #self.device._log.error("Unhandled Response " + hex(hnd) + ": " + # str(data.encode("hex")) + " len:" + str(len(data))) else: self.device._log.error("06>Unhandled Response " + hex(hnd) + ": " + str(data.encode("hex")) + " len:" + str(len(data)))
def startGetData(MAC_ADDR, datapool, getStartBin): #band = MiBand3(MAC_ADDR, debug=True, datapool=datapool) #band.setSecurityLevel(level = "high") #if band.initialize(): # print("Initialized...") # band.disconnect() band = MiBand3(MAC_ADDR, debug=True, datapool=datapool) band.setSecurityLevel(level="high") # Authenticate the MiBand band.authenticate() # get Mi Band3 time mibandtime = band.readCharacteristic(0x002f) midttm = utils.hexbin2dttm(mibandtime, 0) band.writeCharacteristic(0x0051, "\x01\x00", False) # insert sleep when low down 100204 time.sleep(0.24) band.writeCharacteristic(0x005b, "\x01\x00", False) band.writeCharacteristic(0x005a, "\x01\x00", False) band.writeCharacteristic(0x005b, "\x00\x00", False) band.writeCharacteristic(0x0039, "\x01\x00", False) band.writeCharacteristic(0x0038, "\x0c", False) band.writeCharacteristic(0x0039, "\x00\x00", False) band.writeCharacteristic(0x0039, "\x01\x00", False) band.writeCharacteristic(0x0038, "\x06\x17\x00\x65\x6e\x5f\x55\x53", False) band.writeCharacteristic(0x0039, "\x00\x00", False) band.writeCharacteristic(0x0039, "\x01\x00", False) band.writeCharacteristic(0x0038, "\x11", False) band.writeCharacteristic(0x0039, "\x00\x00", False) band.writeCharacteristic(0x0045, "\x01\x00", True) band.writeCharacteristic(0x004e, "\x01\x00", True) band.writeCharacteristic(0x0042, "\x01\x00", True) band.writeCharacteristic(0x0048, "\x01\x00", True) band.writeCharacteristic(0x003f, "\x01\x00", False) band.writeCharacteristic(0x0039, "\x01\x00", False) band.writeCharacteristic(0x0038, "\x06\x19\x00\x00", False) band.writeCharacteristic(0x0039, "\x00\x00", False) # # getStr = "\x01\x01\xe3\x07\x07\x1e\x12\x00\x00\x24" # getStartBin = b'\x01\x01\xe3\x07\x07\x1e\x12\x00\x00\x24' band.writeCharacteristic(0x003e, getStartBin, False) band.writeCharacteristic(0x003e, "\x02", False) try: while True: if band.waitForNotifications(0.5) == False: break except KeyboardInterrupt: print "Catch Interrupt: disconnect" finally: # utils.dumpDataPool( band.datapool ) band.disconnect()
def startGetData3( MAC_ADDR, key, datapool, getStartBin): #band = MiBand3(MAC_ADDR, debug=True, datapool=datapool) #band.setSecurityLevel(level = "high") #if band.initialize(): # print("Initialized...") # band.disconnect() band = None band = MiBand3(MAC_ADDR, key, debug=True, datapool=datapool) band.setSecurityLevel(level = "high") # Authenticate the MiBand band.authenticate() # get Mi Band3 time mibandtime = band.readCharacteristic(0x002f) midttm = utils.hexbin2dttm( mibandtime, 0) band.writeCharacteristic(0x0051, "\x01\x00", False) # insert sleep when low down 100204 time.sleep(0.24) band.writeCharacteristic(0x005b, "\x01\x00", False) band.writeCharacteristic(0x005a, "\x01\x00", False) band.writeCharacteristic(0x005b, "\x00\x00", False) band.writeCharacteristic(0x0039, "\x01\x00", False) band.writeCharacteristic(0x0038, "\x0c", False) band.writeCharacteristic(0x0039, "\x00\x00", False) band.writeCharacteristic(0x0039, "\x01\x00", False) band.writeCharacteristic(0x0038, "\x06\x17\x00\x65\x6e\x5f\x55\x53", False) band.writeCharacteristic(0x0039, "\x00\x00", False) band.writeCharacteristic(0x0039, "\x01\x00", False) band.writeCharacteristic(0x0038, "\x11", False) band.writeCharacteristic(0x0039, "\x00\x00", False) band.writeCharacteristic(0x0045, "\x01\x00", True) band.writeCharacteristic(0x004e, "\x01\x00", True) band.writeCharacteristic(0x0042, "\x01\x00", True) band.writeCharacteristic(0x0048, "\x01\x00", True) band.writeCharacteristic(0x003f, "\x01\x00", False) band.writeCharacteristic(0x0039, "\x01\x00", False) band.writeCharacteristic(0x0038, "\x06\x19\x00\x00", False) band.writeCharacteristic(0x0039, "\x00\x00", False) # # getStr = "\x01\x01\xe3\x07\x07\x1e\x12\x00\x00\x24" # getStartBin = b'\x01\x01\xe3\x07\x07\x1e\x12\x00\x00\x24' band.writeCharacteristic(0x003e, getStartBin, False) band.writeCharacteristic(0x003e, "\x02", False) while True: if band.waitForNotifications(0.5) == False: break band.disconnect() log.info('connection disconnected(nomal process)')