The EyeTribe Java SDK is a reference implementation of the EyeTribeAPI in Java. The SDK simplifies the communication with the EyeTribe Server and eases the process of interfacing with The Eye Tribe Tracker using Java.
To get set up, go to the EyeTribe Java SDK on GitHub. From here, you can either import the library using Gradle, clone the library project and reference it from your own or go to 'releases', download the latest JAR and add this to your project.
The EyeTribe Java SDK contains a calibration sample based on JavaFX. This sample is a perfect place to start if you wish to learn more about how to calibrate the EyeTribe Dev Kit and use gaze input in general.
The following explains how to use the SDK in a simple java program. To get started quickly you should add a reference to the main SDK entry point GazeManager in you main class.
import com.theeyetribe.client.GazeManager; public class TETSimple { public static void main(String[] args) { final GazeManager gm = GazeManager.getInstance(); } }
To establish a connection with the Eye Tribe Server we need to activate the GazeManager. In this example we’ll be coding against API version 1.0 and running in push mode. Make sure the Eye Tribe Server is running before making this call.
public static void main(String[] args) { final GazeManager gm = GazeManager.getInstance(); boolean success = gm.activate(ApiVersion.VERSION_1_0, ClientMode.PUSH); }
If successful, we are now ready to receive gaze data. In order to do so, we have to register an implementation of the interface IGazeListener
to the GazeManager
.
public static void main(String[] args) { // … final GazeListener gazeListener = new GazeListener(); gm.addGazeListener(gazeListener); } private static class GazeListener implements IGazeListener { @Override public void onGazeUpdate(GazeData gazeData) { System.out.println(gazeData.toString()); } }
Running your program at this point should output the received GazeData
to the output console. Note that rawCoordinates
and smoothedCoordinates
of the GazeData
object will only be filled out once the Eye Tribe Server has been calibrated, and will be empty otherwise. Before the application can receive data it must initialize the GazeManager
and register a listener for new data. It is good practice to register a shutdown hook to remove the listener and deinitialize the GazeManager
once the application is about to exit. Our complete program is then:
public class TETSimple { public static void main(String[] args) { final GazeManager gm = GazeManager.getInstance(); boolean success = gm.activate(ApiVersion.VERSION_1_0, ClientMode.PUSH); final GazeListener gazeListener = new GazeListener(); gm.addGazeListener(gazeListener); //TODO: Do awesome gaze control wizardry Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { gm.removeGazeListener(gazeListener); gm.deactivate(); } }); } private static class GazeListener implements IGazeListener { @Override public void onGazeUpdate(GazeData gazeData) { System.out.println(gazeData.toString()); } } }
In the SDK you will also find the ITrackerStateListener
interface. Once a class has implemented the interface and registered a listener with the GazeManager
, the method onTrackerStateChanged()
will be called when the device has been connected or disconnected. The TrackerState
enum can take one of the following values:
public enum TrackerState { TRACKER_CONNECTED(0), TRACKER_NOT_CONNECTED(1), TRACKER_CONNECTED_BADFW(2), TRACKER_CONNECTED_NOUSB3(3), TRACKER_CONNECTED_NOSTREAM(4); }
State needs to be TRACKER_CONNECTED(0)
for the server to work. The TRACKER_CONNECTED_NOUSB3(3)
means that the device has been connected to a USB2 host.
For a thorough explanation of the process of calibrating, please refer to the C# Calibration tutorial. The syntax here is close to identical to that of Java.
You can find more code samples, along with nice examples of how to implement a calibration procedure in C#, on EyeTribe's GitHub.