Class CortexClient
- All Implemented Interfaces:
AutoCloseable
Connect with your registered application credentials, add a
CortexListener, and subscribe to a stream. This class manages
authorization, headset selection, and the underlying network connection.
The first available headset is selected automatically.
For a first experiment, run EMOTIV Launcher, connect a headset (or
Virtual Brainwear), and create a cortex4j.properties file on the
classpath containing cortex.client.id and
cortex.client.secret. Alternatively, set the
CORTEX_CLIENT_ID and CORTEX_CLIENT_SECRET environment variables.
try (CortexClient cortex = CortexClient.connect()) {
cortex.addListener("met", sample ->
System.out.println(sample.getValues()));
cortex.subscribe("met");
Thread.sleep(30_000);
}
Supported streams and their availability depend on the headset, Cortex permissions, and EMOTIV licensing. Use try-with-resources to close the active session when finished.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(String stream, CortexListener listener) Registers an observer for a stream such asdev,met,eeg,mot, orpow.voidclose()Closes the active Cortex session and releases its network connection.static CortexClientconnect()Connects to the local Cortex service and opens an authenticated headset session.static CortexClientconnect(CortexCredentials credentials) Connects using credentials for a registered Cortex application.static CompletableFuture<CortexClient> connectAsync(CortexCredentials credentials) Opens an authenticated session without blocking the caller.static CompletableFuture<CortexClient> connectAsync(URI endpoint, CortexCredentials credentials) Opens an authenticated session at a specified Cortex WebSocket endpoint.Returns the security mode of the established WebSocket connection.Returns the identifier of the headset selected during connection.Returns sensor labels reported for the selected headset.voidremoveListener(String stream, CortexListener listener) Unregisters a stream observer; this does not unsubscribe the stream.voidSubscribes to one or more Cortex data streams.voidunsubscribe(String... streams) Unsubscribes from one or more Cortex streams.
-
Method Details
-
connect
Connects to the local Cortex service and opens an authenticated headset session.- Returns:
- connected client; close it when finished
- Throws:
IllegalStateException- if credentials, headset, authorization, or connection fail
-
connect
Connects using credentials for a registered Cortex application.- Parameters:
credentials- application client ID and secret- Returns:
- connected client
- Throws:
NullPointerException- if credentials is nullIllegalStateException- if authorization or connection fails
-
connectAsync
Opens an authenticated session without blocking the caller.- Parameters:
credentials- registered application credentials- Returns:
- future completing with a connected client, or exceptionally on failure
-
connectAsync
public static CompletableFuture<CortexClient> connectAsync(URI endpoint, CortexCredentials credentials) Opens an authenticated session at a specified Cortex WebSocket endpoint.- Parameters:
endpoint- Cortex WebSocket URIcredentials- registered application credentials- Returns:
- future completing with a connected client, or exceptionally on failure
-
addListener
Registers an observer for a stream such asdev,met,eeg,mot, orpow. Register before subscribing.- Parameters:
stream- Cortex stream namelistener- callback invoked for each matching sample- Throws:
IllegalArgumentException- if stream is blankNullPointerException- if listener is null
-
removeListener
Unregisters a stream observer; this does not unsubscribe the stream.- Parameters:
stream- Cortex stream namelistener- previously registered callback
-
subscribe
Subscribes to one or more Cortex data streams.Available streams depend on headset and account entitlements.
- Parameters:
streams- one or more stream names, such as"dev"or"met"- Throws:
IllegalArgumentException- if no streams are providedIllegalStateException- if the subscription is rejected or the client is closed
-
unsubscribe
Unsubscribes from one or more Cortex streams.- Parameters:
streams- one or more stream names to stop- Throws:
IllegalArgumentException- if no streams are providedIllegalStateException- if the client is closed or the request fails
-
getHeadsetId
Returns the identifier of the headset selected during connection.- Returns:
- selected headset ID
-
getSensorLabels
Returns sensor labels reported for the selected headset.Labels may be empty; do not assume a fixed channel layout.
- Returns:
- immutable list of sensor labels
-
getConnectionSecurity
Returns the security mode of the established WebSocket connection.- Returns:
- connection security mode
-
close
public void close()Closes the active Cortex session and releases its network connection.Safe to call more than once.
- Specified by:
closein interfaceAutoCloseable
-