Eeg Algorithm SDK For Android Development Guide
Eeg Algorithm SDK For Android Development Guide
Development Guide
February 2, 2016
e NeuroSky® product families consist of hardware and
software components for simple integration of this biosensor
technology into consumer and industrial end-applications.
All products are designed and manufactured to meet consumer
thresholds for quality, pricing, and feature sets. NeuroSky
sets itself apart by providing building block component
solutions that offer friendly synergies with related and complemen-
tary technological solutions.
API Documentation 6
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
SDK Listener Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
setOnStateChangeListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
setOnAttAlgoIndexListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
setOnMedAlgoIndexListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
setOnBPAlgoIndexListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
setOnEyeBlinkDetectionListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
NskAlgoSignalQualityListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
SDK Utility Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
NskAlgoInit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
NskAlgoUninit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
NskAlgoAlgoVersion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
NskAlgoSdkVersion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
NskAlgoDataStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
NskAlgoStart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
NskAlgoPause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
NskAlgoStop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Applications 16
Application of Attention Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Application of Meditation Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Application of EEG Bandpower Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Application of Eye Blink Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
SDK Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Pause and Resume . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Stop and Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Customize Algorithm Output Interval . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3
February 2, 2016 | © 2015 NeuroSky, Inc. All Rights Reserved.
Chapter 1
is document will guide you through the process of generating algorithm outputs from different
NeuroSky Proprietary Mind Algorithms using NeuroSky EEG Algorithm SDK for Android with
EEG data collected by NeuroSky Biosensor System (e.g. TGAM module or MindWave Mobile Head-
set).
is development guide is intended for Android application developers who are already familiar with
standard Android development using Android Studio/Eclipse. If you are not already familiar with
developing for Android, please rst visit Android's developer web site for instruction and tools to
develop Android apps.
Important: .
• Requires minimum Android API version 16 or later
Note: .
• e sample project was created with Android Studio. However, developers can still use any
familiar IDE to start his own Android project
• e minimum requirements for the sample project is API 19
Application development
Introduction
We recommend developers to use our COMM SDK for Android in their application. Comm SDK
reduces the complexity of managing EEG Algorithm SDK connections and handles data stream pars-
4
February 2, 2016 | © 2015 NeuroSky, Inc. All Rights Reserved.
Chapter 1 – About the Android SDK
ing. With the help of our SDKs, the application could be as simple as passing the data received and
parsed by the Comm SDK to speci c function call(s) at Algo SDK. Speci c EEG algorithm index
would then be returned accordingly.
Important: .
• NeuroSky Comm SDK can only communicate with one paired device at a time.
Important: .
• e sample project compiles with Android Studio. However, the sample code inside still com-
piles with any other Android application development IDEs (e.g. Eclipse)
• e sample project only demonstrates how to iterate with the EEG Algo SDK.
• Comm SDK enclosed in the sample project is version 1.0.4. Please make sure you are using
the latest stable Comm SDK version and make proper changes on sample project if needed.
Application development 5
February 2, 2016 | © 2015 NeuroSky, Inc. All Rights Reserved.
Chapter 2
API Documentation
e EEG Algorithm SDK API Reference in this section contains descriptions of the classes and
protocols available in the EEG Algorithm Android API.
Data Types
See the NskAlgoSdk.jar in the SDK package
6
February 2, 2016 | © 2015 NeuroSky, Inc. All Rights Reserved.
Chapter 2 – API Documentation
/*
Algo SDK is analysing provided bulk data (i.e. NSK_ALGO_DataStream()
is invoked with NSK_ALGO_DATA_TYPE_BULK_EEG.
NSK_ALGO_STATE_MASK (0xFF00),
/* Incoming EEG data type definitions (data from COMM SDK or recorded EEG data) */
public enum NskAlgoDataType {
NSK_ALGO_DATA_TYPE_EEG (0x01), /* Raw EEG data */
NSK_ALGO_DATA_TYPE_ATT (0x02), /* Attention data */
NSK_ALGO_DATA_TYPE_MED (0x03), /* Meditation data */
NSK_ALGO_DATA_TYPE_PQ (0x04), /* Poor signal quality data */
NSK_ALGO_DATA_TYPE_BULK_EEG (0x05), /* Bulk of EEG data */
NSK_ALGO_DATA_TYPE_MAX (0x06);
}
7
February 2, 2016 | © 2015 NeuroSky, Inc. All Rights Reserved.
Chapter 2 – API Documentation
setOnStateChangeListener
EEG Algo SDK state change noti cation listener method
// Required
public void setOnStateChangeListener(NskAlgoSdk.OnStateChangeListener listener);
Note: .
• Developer will always need to check with the SDK state and perform proper GUI handling
Example
nskAlgoSdk.setOnStateChangeListener(new NskAlgoSdk.OnStateChangeListener() {
@Override
public void onStateChange(int state, int reason) {
Log.i(TAG, "On State Change: " + state + " [reason:" + reason + "]");
runOnUiThread(new Runnable() {
@Override
public void run() {
// change UI elements here
}
});
}
});
setOnAttAlgoIndexListener
Attention Algorithm index noti cation listener method
// Optional
public void setOnAttAlgoIndexListener(OnAttAlgoIndexListener listener);
Note: .
• Attention algorithm has a xed output interval of 1 second
• Attention algorithm index ranges from 0 to 100 where higher the attention index, higher the
attention level
Example
nskAlgoSdk.setOnAttAlgoIndexListener(new NskAlgoSdk.OnAttAlgoIndexListener() {
@Override
public void onAttAlgoIndex(int value) {
setOnMedAlgoIndexListener
Meditation Algorithm index noti cation listener method
// Optional
public void setOnMedAlgoIndexListener(OnMedAlgoIndexListener listener);
Note: .
• Meditation algorithm has a xed output interval of 1 second
• Meditation algorithm index ranges from 0 to 100 where higher the meditation index, higher the
meditation level
Example
nskAlgoSdk.setOnMedAlgoIndexListener(new NskAlgoSdk.OnMedAlgoIndexListener() {
@Override
public void onMedAlgoIndex(int value) {
Log.i(TAG, "NskAlgoMedAlgoIndexListener: Meditation:" + value);
runOnUiThread(new Runnable() {
@Override
public void run() {
// change UI elements here
}
});
}
});
setOnBPAlgoIndexListener
EEG Bandpower Algorithm index noti cation listener method
// Optional
public void setOnBPAlgoIndexListener(OnBPAlgoIndexListener listener);
Note: .
• EEG bandpower (in dB) algorithm has a xed output interval of 1 second
Example
nskAlgoSdk.setOnBPAlgoIndexListener(new NskAlgoSdk.OnBPAlgoIndexListener() {
@Override
public void onBPAlgoIndex(float delta, float theta, float alpha, float beta, float gamma) {
Log.i(TAG, "NskAlgoBPAlgoIndexListener: BP: D[" + delta + " dB] T[" + theta + " dB] A[" +
alpha + " dB] B[" + beta + " dB] G[" + gamma + "]");
runOnUiThread(new Runnable() {
@Override
public void run() {
// change UI elements here
}
});
}
});
setOnEyeBlinkDetectionListener
Eye blink detection noti cation listener method
// Optional
public void setOnEyeBlinkDetectionListener(OnEyeBlinkDetectionListener listener);
Note: .
• No baseline data collection will be needed
• Eye blink strength will be returned once eye blink is detected when Algo SDK state is RUN-
NING
Example
nskAlgoSdk.setOnEyeBlinkDetectionListener(new NskAlgoSdk.OnEyeBlinkDetectionListener() {
@Override
public void onEyeBlinkDetection(int value) {
Log.i(TAG, "NskAlgoEyeBlinkDetectionListener: Eye blink detected");
runOnUiThread(new Runnable() {
@Override
public void run() {
// change UI elements here
}
});
}
});
NskAlgoSignalQualityListener
EEG data signal quality noti cation listener method
// Optional
public void setOnSignalQualityListener(OnSignalQualityListener listener);
Note: .
• Signal Quality was measured and reported at a xed output interval of 1 second
• SDK state will be changed from RUNNING to PAUSE when signal quality is poor or sensor
off-head is detected. It would return to its previous state (e.g. RUNNING) when the signal
quality returns to normal
Example
nskAlgoSdk.setOnSignalQualityListener(new NskAlgoSdk.OnSignalQualityListener() {
@Override
public void onSignalQuality(int level) {
Log.i(TAG, "On Signal Quality: " + level);
runOnUiThread(new Runnable() {
@Override
public void run() {
// change UI elements here
}
});
}
});
NskAlgoInit
Initialize the Algo SDK with supported algorithm type(s).
/**
* @brief Required: Initialize the Algo SDK with supported algorithm types.
*
* @param type : Algorithm type(s) (see NskAlgoType)
* @param dataPath : An user data path to store user data
* @retval Return 0 on operation success or else fail
*/
int NskAlgoInit(int algoTypes, String dataPath);
NskAlgoUninit
Uninitialize the Algo SDK
/**
* @brief Required: Uninitialize the Algo SDK
* Note: if SDK state is NSK_ALGO_STATE_RUNNING, then SDK state will change to
NSK_ALGO_STATE_STOP with reason NSK_ALGO_REASON_BY_USER before SDK is uninitialized
* @retval Return 0 on operation success or else fail
*/
int NskAlgoUninit ();
NskAlgoAlgoVersion
Get the Algo SDK version.
/**
* @brief Optional: Get the Algo SDK version
* Format: M.m.p, where M is major version, m is minor version and p is patch
version
*
* @param type : Specify the supported Algo type version to be queried
* @retval Null terminated string
*/
String NskAlgoAlgoVersion (int type);
Example
apVersionStr = NskAlgoAlgoVersion(NSK_ALGO_TYPE_AP);
if (apVersionStr != NS_NULL) {
Log.d(TAG, "Appreciation Algo ver.: %s\n", apVersionStr);
}
NskAlgoSdkVersion
Get the Algo SDK version.
/**
* @brief Optional: Get the Algo SDK version
* Format: M.m.p, where M is major version, m is minor version and p is patch
version
*
* @retval Null terminated string
*/
String NskAlgoSdkVersion ();
Example
sdkVersionStr = NskAlgoSdkVersion();
if (sdkVersionStr != NS_NULL) {
Log.d(TAG, "EEG Algo ver.: %s\n", sdkVersionStr);
}
NskAlgoDataStream
EEG data stream input from NeuroSky Biosensor System (e.g. TGAM or MindWave Mobile headset).
/**
* @brief Required: EEG data stream input from NeuroSky Biosensor System (e.g. TGAM or MindWave
Mobile headset)
* When type = NSK_ALGO_DATA_TYPE_PQ, dataLength = 1
* When type = NSK_ALGO_DATA_TYPE_EEG, dataLength = 512 (i.e. 1 second EEG raw data)
* When type = NSK_ALGO_DATA_TYPE_ATT, dataLength = 1
* When type = NSK_ALGO_DATA_TYPE_MED, dataLength = 1
* When type = NSK_ALGO_DATA_TYPE_BULK_EEG, dataLength = N*512 (i.e. N continous
seconds of EEG raw data)
* Note 1: In case of type = NSK_ALGO_DATA_TYPE_BULK_EEG, caller should NOT release
the data buffer until SDK state changes back to NSK_ALGO_STATE_STOP
* Note 2: In case of type = NSK_ALGO_DATA_TYPE_BULK_EEG, the first 5 seconds of data
will be used as baseline data
*
* @param type : Data type
* @param data : Data stream array
* @param dataLenght : Size of the data stream
* @retval Return 0 on operation success or else fail
*/
int NskAlgoDataStream (int type, short data[], int dataLenght);
Note: .
• For the data format from NeuroSky Biosensor System, please refer to TGAM Communication
Protocol
Important: .
• ere are different data output giving out from NeuroSky Biosensor System.
• EEG Algo SDK handles only the following 4 data output for now. ey are:
– Poor Signal Quality
– EEG Raw Data
– Attention
– Meditation
NskAlgoStart
Start processing data from NskAlgoDataStream() call.
/**
* @brief Required: Start processing data from NskAlgoDataStream() call
* Note: SDK state will changed to NSK_ALGO_STATE_RUNNING when previous state is
NSK_ALGO_STATE_STOP / NSK_ALGO_STATE_PAUSE / NSK_ALGO_STATE_INITED
*
* @param bBaseline: Always be false [RESERVED]
* @retval Return 0 on operation success or else fail
*/
int NskAlgoStart (boolean bBaseline);
Note: .
• SDK state will only change to RUNNING by invoking NSK_ALGO_Start()
NskAlgoPause
Pause processing/collecting data.
/**
* @brief Required: Pause processing/collecting data
* Note: SDK state will changed to NSK_ALGO_STATE_PAUSE with reason
NSK_ALGO_REASON_BY_USER
*
* @retval Return 0 on operation success or else fail
*/
int NskAlgoPause ();
Note: .
• SDK state will change to PAUSE
• No algorithm index callback will not be invoked unless NskAlgoStart() function is invoked
again
NskAlgoStop
Stop processing/collecting data.
/**
* @brief Required: Stop processing/collecting data.
* Note: SDK state will changed to NSK_ALGO_STATE_STOP with reason
NSK_ALGO_REASON_BY_USER
*
* @retval Return 0 on operation success or else fail
*/
int NskAlgoStop ();
Note: .
• SDK state will change to STOP
• No algorithm index callback will be invoked unless NskAlgoStart() method is invoked again
• NskAlgoStop() �requires recollection of baseline data once restart �(Exception for Attention
and Meditation) �while NskAlgoPause() doesn'�t.
Applications
Note: .
• Attention has a xed output interval of 1 second, i.e. one new Attention index every second
Note: .
• Meditation has a xed output interval of 1 second, i.e. one new Meditation index every second
Note: .
• EEG Bandpower algorithm has a xed output interval of 1 second
16
February 2, 2016 | © 2015 NeuroSky, Inc. All Rights Reserved.
Chapter 3 – Applications
Note: .
• No baseline data collection will be needed
SDK Operations
Note: .
• ere will be no effect on NskAlgoPause() when previous SDK state is not RUNNING
• When SDK state is ANALYSING BULK DATA, then NskAlgoPause() will always return non-
zero (i.e. no effect)
Note: .
• ere will be no effect on NskAlgoStop() when previous SDK state is not RUNNING
Note: .
• Different algorithm may have different minimum/default output interval
20
February 2, 2016 | © 2015 NeuroSky, Inc. All Rights Reserved.