from Bluetooth import Bluestus
import serial

MAC_ADRESS = "00:22:12:02:71:FA"
SERIAL_PORT = "COM22"
BAUD_RATE = 115200

bluethoot = Bluestus(MAC_ADRESS)

connected = False
count = 0

print("Trying to connect...")

while not connected:
    try:
        count += 1
        my_serial = serial.Serial(SERIAL_PORT, BAUD_RATE)
        connected = True

    except Exception:
        if count == 1000:
            print("Trying to connect...")
            count = 0

while True:
    if my_serial.is_open:
        print("\n")

        serial_line = my_serial.readline().decode('utf-8').strip()

        while serial_line != "|":
            print(serial_line)

            serial_line = my_serial.readline().decode('utf-8').strip()

        result = my_serial.readline().decode('utf-8').strip()
        
        print(f"Result: {result}")

        bluethoot.send(result)

        serial_line = my_serial.readline().decode('utf-8').strip()