interface BluetoothRemoteGATTServer {
    readonly connected: boolean;
    connect(): Promise<BluetoothRemoteGATTServer>;
    disconnect(): void;
    getPrimaryService(service: string): Promise<BluetoothRemoteGATTService>;
    getPrimaryServices(): Promise<BluetoothRemoteGATTService[]>;
}

interface BluetoothRemoteGATTService {
    readonly uuid: string;
    getCharacteristic(characteristic: string): Promise<BluetoothRemoteGATTCharacteristic>;
}

interface BluetoothRemoteGATTCharacteristic {
    writeValueWithResponse(data: BufferSource): Promise<void>;
    writeValueWithoutResponse(data: BufferSource): Promise<void>;
}

interface BluetoothDevice extends EventTarget {
    readonly id: string;
    readonly name?: string;
    readonly gatt?: BluetoothRemoteGATTServer;
}

interface Bluetooth extends EventTarget {
    requestDevice(options: {
        acceptAllDevices?: boolean;
        filters?: Array<Record<string, unknown>>;
        optionalServices?: string[];
    }): Promise<BluetoothDevice>;
    getDevices(): Promise<BluetoothDevice[]>;
}

interface SerialPort extends EventTarget {
    readable: ReadableStream<Uint8Array> | null;
    writable: WritableStream<Uint8Array> | null;
    open(options: {
        baudRate: number;
        dataBits?: number;
        stopBits?: number;
        parity?: string;
        flowControl?: string;
        bufferSize?: number;
    }): Promise<void>;
    close(): Promise<void>;
    getInfo(): { usbVendorId?: number; usbProductId?: number };
}

interface Serial extends EventTarget {
    requestPort(): Promise<SerialPort>;
    getPorts(): Promise<SerialPort[]>;
}

interface Navigator {
    serial?: Serial;
    bluetooth?: Bluetooth;
}
