import supervisor
import board
from digitalio import DigitalInOut, Direction
import time

# BLE
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
from adafruit_ble.services.standard.hid import HIDService

def start_central():

    #BLE Setup
    ble = BLERadio()
    ble.name = "lhf_central"
    connection = None


    while True:
        # If connection is 'null'
        if not connection:
            # Scan for advertisements.
            for ad in ble.start_scan(ProvideServicesAdvertisement):
                # If the other keyboard is detected and its running the UARTSerice.
                if HIDService in ad.services:
                    print(str(ad.complete_name) + ": " + str(ad.services))
                    # Stop scanning.
                    ble.stop_scan()
                    # Save the connection.
                    connection = ble.connect(ad)
                    break

        # If connection is populated.     
        if connection and connection.connected:
            #Set up UART
            uart = connection[UARTService]
            while connection.connected:
                # Send some test text.
                uart.write(b'From lhf_central.')
                pass