> For the complete documentation index, see [llms.txt](https://emotiv.gitbook.io/cortex-library-for-mobile/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emotiv.gitbook.io/cortex-library-for-mobile/java-api/cortexclient-class.md).

# CortexClient class

```java
import com.emotiv.CortexClient;
```

Use CortexClient to send and receive JSON messages to the Cortex library. You must [initialize](/cortex-library-for-mobile/java-api/cortexlib-class.md) the Cortex library before creating an instance of CortexClient.

## registerResponseHandler

```java
public void registerResponseHandler(ResponseHandler responseHandler)
```

Set a handler to receive the JSON messages from the Cortex library.&#x20;

The handler is defined like this:

```java
public interface ResponseHandler {
    public void processResponse(String responseMessage);
}
```

The argument **responseMessage** of the method **processResponse** is a JSON message sent by the Cortex library.

## authenticate

```java
public void authenticate(@NonNull Activity activity, 
                         String clientId, 
                         int requestCode)
```

This function is to help you log in into Cortex, using [OAuth 2](https://oauth.net/2/) for authentication.

When you call this function, your application displays a web form from the EMOTIV web site. This form requires the user to enter their EmotivID and password.

The first argument is the activity that will display the web form. It must be an object of the type [android.app.Activity](https://developer.android.com/reference/android/app/Activity). The second argument is the client id of your Cortex application. You can find it on the [EMOTIV web site](https://www.emotiv.com/my-account/cortex-apps/). The argument **requestCode** can take any value.

Your application must get the result from the activity and then call the function **getAuthenticationCode**. This function returns an authentication code that you can then use to call the JSON API method [loginWithAuthenticationCode](/cortex-library-for-mobile/jsonapi/loginwithauthenticationcode.md).

One way to get the result of the activity is to override its function **onActivityResult** like this:

```java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    String code = cortexClient.getAuthenticationCode(requestCode, intent);
    if (!code.isEmpty()) {
        cortexClient.loginWithAuthenticationCode(code);
    }
}
```

## getAuthenticationCode

```java
public String getAuthenticationCode(int requestCode, @NonNull Intent intent)
```

This function returns an authentication code that you can use to call the JSON API method [loginWithAuthenticationCode](/cortex-library-for-mobile/jsonapi/loginwithauthenticationcode.md). You must call this function after you called the function **authenticate** and get the result of the activity.

## sendRequest

```java
public void sendRequest(String requestMessage)
```

Call this method to send a JSON message to the Cortex library. Cortex will respond asynchronously, using the handler you provided in the method **registerResponseHandler.**

## close

```java
public void close()
```

This method closes the connection to the Cortex library and frees all the resources allocated for this object. Calling this method is optional. After you called it, you must not call any other method on this object.
