# Methods

## Start

Initialize connection to the Emotiv launcher, enabling use of the API. It is best to delay other functionality until the connection has been [authorized](https://bonspiel-games.gitbook.io/cortex-unity-plugin/events#authorized), or until [the connection state](https://bonspiel-games.gitbook.io/cortex-unity-plugin/events#connection-state-changed) has been updated to authorized.

```csharp
void Start(string clientId, string clientSecret, string appURL = "wss://localhost:6868", string emotivAppsPath = "C:\\Program Files\\EmotivApps", bool logs = false, bool streamPrint = false, string license = "");
```

| Parameter      | Description                                                                                 |
| -------------- | ------------------------------------------------------------------------------------------- |
| clientId       | client id of your cortex application                                                        |
| clientSecret   | client secret of your cortex application                                                    |
| appURL         | url through which to connect to the emotiv launcher                                         |
| emotivAppsPath | filepath where the emotiv launcher is installed, used to check if the launcher is installed |
| logs           | print logs to the debug console                                                             |
| streamPrint    | print all incoming stream data to the debug console                                         |
| license        | cortex api license, required for EEG access, which the plugin does not currently support    |

**Example:**

```csharp
using UnityEngine;
using CortexPlugin;

public class ConnectToCortex : MonoBehaviour
{
    [Header("credentials")]
    public string clientId;
    public string clientSecret;

    [Header("settings")]
    public bool printLogs;
    public bool printDataStreams;

    void Awake()
    {
        Cortex.Start(clientId, clientSecret, logs: printLogs, streamPrint: printDataStreams);
    }

    private void OnApplicationQuit()
    {
        Cortex.Stop();
    }
}
```

## Query Headsets

Queries a list of available headsets, triggering [Headset Query Result](https://bonspiel-games.gitbook.io/cortex-unity-plugin/events#headset-query-result).

-> [Headset Query Result](https://bonspiel-games.gitbook.io/cortex-unity-plugin/events#headset-query-result)

```csharp
void QueryHeadsets();
```

## Start Session

Opens a session and starts data streams with the headset, provided it has been connected. If successful, will trigger [Data Stream Started](https://bonspiel-games.gitbook.io/cortex-unity-plugin/events#data-stream-started).

->-> [Data Stream Started](https://bonspiel-games.gitbook.io/cortex-unity-plugin/events#data-stream-started)

```csharp
void StartSession(string headsetId);
```

**Example:**

```csharp
public void PairWithHeadset(Headset target)
{
    if(target.status == "connected")
    {
        Cortex.StartSession(target.headsetId);
    }
}
```

## Connect Device

Pairs a device with the Emotiv launcher, needed to start a session. Devices may already be connected, which will result in an error event.

-> [Device Connected](https://bonspiel-games.gitbook.io/cortex-unity-plugin/events#device-connected)

```csharp
void ConnectDevice(string headsetId);
```

**Example:**

```csharp
string pairingHeadset;

public void PairWithHeadset(Headset target)
{
    if(target.status == "connected")
    {
        Cortex.StartSession(target.headsetId);
    }
    else
    {
        pairingHeadset = target.headsetId;
        Cortex.ConnectDevice(target.headsetId);
        Cortex.HeadsetConnected += PairWithNewlyConnectedHeadset;
    }
}

void PairWithNewlyConnectedHeadset(HeadsetConnectEventArgs args)
{
    if (args.HeadsetId != pairingHeadset)
        return;

    Cortex.StartSession(args.HeadsetId);
    pairingHeadset = "";
    Cortex.HeadsetConnected -= PairWithNewlyConnectedHeadset;
}
```

## End Session

Ends a headset session.

-> [Data Stream Ended](https://bonspiel-games.gitbook.io/cortex-unity-plugin/events#data-stream-ended)

```csharp
void EndSession(string sessionId);
```

## End Session By Headset

End a headset session with a headset id rather than a session id.

-> [Data Stream Ended](https://bonspiel-games.gitbook.io/cortex-unity-plugin/events#data-stream-ended)

```csharp
void EndSessionByHeadset(string headsetId);
```

## Get Session By Headset

Gets the session id associated with the given headset if it exists.

```csharp
string GetSessionByHeadset(string headsetId);
```

## End Most Recent Session

Ends the session that was started most recently, regardless of activity.

-> [Data Stream Ended](https://bonspiel-games.gitbook.io/cortex-unity-plugin/events#data-stream-ended)

```csharp
void EndMostRecentSession();
```

## Stop

Closes the Cortex connection.

```csharp
void Stop();
```
