def connect(mac_addr, auth_key=None): global channel global connection MAC_ADDR = mac_addr AUTH_KEY = str(auth_key) # Validate MAC address if 1 < len(MAC_ADDR) != 17: print("Error:") print(" Your MAC length is not 17, please check the format") print(" Example of the MAC: a1:c2:3d:4e:f5:6a") return False # Validate Auth Key if auth_key != None: if 1 < len(AUTH_KEY) != 32: print("Error:") print(" Your AUTH KEY length is not 32, please check the format") return False # Convert Auth Key from hex to byte format if auth_key != None: AUTH_KEY = bytes.fromhex(AUTH_KEY) try: band = None if (AUTH_KEY): band = miband(MAC_ADDR, AUTH_KEY, debug=True) success = band.initialize() if success: try: logging.getLogger("pika").propagate = False connection = pika.BlockingConnection( pika.ConnectionParameters('localhost')) channel = connection.channel() except: pass get_activity_logs(band) else: band = miband(MAC_ADDR, debug=True) get_activity_logs(band) except Exception as ex: print("Error in device: %s", MAC_ADDR) print(ex) if type(band) is miband: band.disconnectDevice() return False if type(band) is miband: band.disconnectDevice() if connection != None: connection.close() return True
def connect(): global band success = False timeout = 3 msg = 'Connection to the band failed. Trying again in {} seconds' MAC_ADDR = get_mac_address(mac_filename) AUTH_KEY = get_auth_key(auth_key_filename) while not success: try: band = miband(MAC_ADDR, AUTH_KEY, debug=True) success = band.initialize() except BTLEDisconnectError: print(msg.format(timeout)) time.sleep(timeout) except KeyboardInterrupt: print("\nExit.") exit()
def get(self): success = False attempts = 0 while attempts < 3 and not success: try: if (AUTH_KEY): band = miband(MAC_ADDR, AUTH_KEY, debug=True) success = band.initialize() steps = band.get_steps() band.disconnect() return steps, 200 except BTLEDisconnectError: attempts += 1 if attempts == 3: return 'Connection to the MIBand failed', 500 print( 'Connection to the MIBand failed. Trying out again in 3 seconds' ) time.sleep(3) continue
def set_music(): band.setMusicCallback(_default_music_play,_default_music_pause,_default_music_forward,_default_music_back,_default_music_vup,_default_music_vdown,_default_music_focus_in,_default_music_focus_out) fi = input("Set music track to : ") band.setTrack(fi, MUSICSTATE.PLAYED) while True: if band.waitForNotifications(0.5): continue input("enter any key") if __name__ == "__main__": success = False while not success: try: if (AUTH_KEY): band = miband(MAC_ADDR, AUTH_KEY, debug=True) success = band.initialize() else: band = miband(MAC_ADDR, debug=True) success = True break except BTLEDisconnectError: print('Connection to the MIBand failed. Trying out again in 3 seconds') time.sleep(3) continue except KeyboardInterrupt: print("\nExit.") exit() menu = CursesMenu("MIBand4", "Features marked with @ require Auth Key") info_item = FunctionItem("Get general info of the device", general_info)
#! /usr/bin/python3 import sys from miband import miband from bluepy.btle import BTLEDisconnectError while True: try: band = miband(sys.argv[1], debug=True) band.send_custom_alert(3, sys.argv[2]) band.waitForNotifications(10) band.disconnect() break except BTLEDisconnectError: print('connection to the MIBand failed. Trying out again') continue