Пример #1
0
    while not connected_to_central:
        # Get information from the dongle
        # to see if it's connected to the central
        status = my_dongle.at_gapstatus()

        # Checks for information about connection
        if '\\nConnected\\r' in str(status):
            print('\nConnected to central')
            while True:
                # Listen for a received message
                response = str(my_dongle.rx_buffer)
                if 'Received' in response:
                    print(response)
                    # Clean up the response
                    response = response.replace('b\'\\r\\n', '')
                    response = response.replace('b\'\'', '')
                    response = response.replace('\\r\\n\'', '')
                    response = response.replace('\\n', '')
                    response = response.replace('\\r\\r', '\n')

                    print(response)
        else:
            print('Trying to connect to central')
            sleep(2)

except KeyboardInterrupt:
    # Disconnects and stops the dongle
    my_dongle.at_gapdisconnect()
    my_dongle.stop_daemon()
    exit()
            connected = True
            break
        if "\r\nNot Connected" in connected_status[0]:
            sender_dongle.at_gapconnect(mac_addr_to_repeater)
            time.sleep(5)
        print("Trying to connect...")
        time.sleep(2)

    print("Connected.")
    print("Getting services...")
    get_services = sender_dongle.at_get_services()
    sender_dongle.rx_state = "rx_waiting"
    time.sleep(2)
    ready = input("Press enter to start sending data to the repeater dongle.")

    while 1:
        data = scan_and_get_results()
        time.sleep(1)
        if not data == "":
            sent = sender_dongle.at_spssend(data)
            time.sleep(0.1)
            if len(sent) == 1:
                if "[Sent]" in sent[0]:
                    print("Data = (" + data + ") sent.")
                    time.sleep(1)
            data = ""
        time.sleep(1)
        sender_dongle.rx_state = "rx_waiting"
except KeyboardInterrupt:
    sender_dongle.at_gapdisconnect()
    print("Shutting down script.")
    ready = input(
        "Press enter to connect to the repeater dongle. (This should be connected first)."
    )
    print("Connecting...")
    reciever_dongle.at_gapconnect(mac_addr_to_repeater)
    time.sleep(5)
    while not connected:
        connected_status = reciever_dongle.ati()
        if "\r\nConnected" in connected_status[0]:
            connected = True
            break
        if "\r\nNot Connected" in connected_status[0]:
            reciever_dongle.at_gapconnect(mac_addr_to_repeater)
            time.sleep(5)
        print("Trying to connect...")
        time.sleep(2)

    print("Connected.")
    print("Waiting to recieve...")

    while 1:
        buffer = reciever_dongle.rx_buffer.decode("utf-8", "ignore")
        if "\r\nhandle_evt_gattc_notification:" in buffer:
            save_msg(buffer)
        time.sleep(0.5)
except KeyboardInterrupt:
    reciever_dongle.at_cancel_connect()
    reciever_dongle.at_gapdisconnect()
    reciever_dongle.at_advstop()
    print("Shutting down script.")
        print("Forwarding data to reciever:")
        print(msg_to_send)
        if not msg_to_send == "":
            repeter_dongle.at_spssend(msg_to_send)
            repeter_dongle.rx_state = "rx_waiting"
    except:
        print(" ")


try:
    while 1:
        buffer = repeter_dongle.rx_buffer.decode("utf-8", "ignore")
        if "\r\nCONNECTED." in buffer:
            num_of_connected_devices = num_of_connected_devices + 1
            print("A Dongle has connected!")
            repeter_dongle.at_advstart()
            repeter_dongle.rx_state = "rx_waiting"
        if "DISCONNECTED." in buffer:
            num_of_connected_devices = num_of_connected_devices - 1
            connection_list = []
            print("No Dongles connected.")
        if num_of_connected_devices > 1:
            if "\r\n[Received]:" in buffer:
                # print(buffer)
                send_msg(buffer)
        buffer = ""
        time.sleep(0.1)
except KeyboardInterrupt:
    repeter_dongle.at_advstop()
    repeter_dongle.at_gapdisconnect()
    print("Shutting down script.")