mjohan / cordova-plugin-empatica-device

Cordova plugin for Empatica device (android and iOS)
MIT License
4 stars 0 forks source link
android cordova-plugin empatica-device ios

cordova-plugin-empatica-device

This plugin helps cordova/ionic/phonegap projects to communicate with Empatica device (especially E4).

This plugin implements Empatica mobile SDK and needs SDK library (empalink) and API-KEY provided by Empatica. These should be available for you if you have empatica devices and an Empatica Connect account. Both of them are available from Empatica developer page.

Supported function list:

The details about these functions will be explained in Methods section. All these functions will be available after deviceready event.

// ionic
$ionicPlatform.ready(function() {
  // Empatica-plugin calls
});

// cordova
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
  // Empatica-plugin calls
});

Supported Platform

Note You should also change the minSdkVersion value on your project to 19 or greater. Please check whether your android platform has already supported this SDK version. Furthermore, please check also your build-tools version on your Android SDK if you have problems using this plugin.

Installation

git clone -v https://github.com/mjohan/cordova-plugin-empatica-device.git

Methods and Constants

initialize

Initializes the plugin with provided API-Key to bind the device. This method must be called before other method calls.

Empatica.initialize('<API_KEY>', success, failure);

Parameters

connect

Connects to the Empatica device.

Empatica.connect(success, failure);

Parameters

disconnect

Disconnects to the Empatica device.

Empatica.disconnect(success, failure);

Parameters

subscribe

Subscribes to any sensor's update value. This method needs the third param which indicates the sensor type. This param should be assigned with one of Empatica.SENSORS constants.

Empatica.subscribe(success, failure, sensor);

Warning The device is in streaming mode when the connection has been made. This subscribe method is only an interface to get the streamed values. In fact, all sensors values are synched by the device to your app.

Parameters

unsubscribe

Stops subscribing to a certain sensor type.

Empatica.unsubscribe(success, failure, sensor);

Parameters

Constants

These are the supported sensors by Empatica E4 device:

Sample

This sample shows how we could subscribe to BVP sensor. After initialization and connect process are succeeded, the progam tries to listen for any BVP update value. The program then stop listening to it after 5 seconds and disconnects the device after 15 seconds.

if (typeof Empatica !== 'undefined') {
  Empatica.initialize('<API_KEY>', function(initializeResult) {
    console.log(JSON.stringify(initializeResult));

    Empatica.connect(function(connectResult) {
      console.log(JSON.stringify(connectResult));

      // subscribes to BVP sensor
      Empatica.subscribe(function(data) {
        console.log(JSON.stringify(data)); // {"bvp": double_value, "timestamp": double_value}
      }, function (error) {
        console.log(JSON.stringify(error));
      }, Empatica.SENSORS.BVP_SENSOR);

      // unsubscribes after 5000 ms
      $interval(function () {
        Empatica.unsubscribe(function(data) {
          console.log(JSON.stringify(data));
        }, function (error) {
          console.log(JSON.stringify(error));
        }, Empatica.SENSORS.BVP_SENSOR);
      }, 5000, 1);

      // disconnects after 15000 ms
      $interval(function () {
        Empatica.disconnect(function() {
          console.log("Success disconnect");
        }, function(error) {
          console.log(JSON.stringify(error));
        });
      }, 15000, 1);
    }, function (error) {
      console.log(JSON.stringify(error));
    });
  }, function(error) {
    console.log(JSON.stringify(error));
  });
}

Note subscribe and unsubscribe methods do not need to be put inside connect success-callback.

License

MIT

Feedback

If you find any problems with this plugin, please create an issue or create a pull request.

Credits