#Metawear plugin for Apache Cordova
Use this plugin to connect your hybrid mobile application to a metawear device.
##Dependencies
http://plugins.cordova.io/#/package/com.megster.cordova.ble This plugin relies on the Bluetooth Low Energy Central plugin for Apache Cordova.
#Supported Platforms
- iOS
- Android
This Cordova plugin relies on the Bluetooth Low Energy Central Plugin. That plugin currently only supports iOS and Android.
#Installing Install with Cordova CLI
$ cd /path/to/your/project $ cordova plugin add com.lisaseacat.metawear
When playing a color on the metawear there are three choices for colors, red, green, and blue. You will need to pass one of these color variables into the setLED method to indicate your color choice.
COLOR.RED - red
COLOR.GREEN - green
COLOR.BLUE - blue
- metawear.init
- metawear.listenForButton
- metawear.setLED
- metawear.play
- metawear.pause
- metawear.stop
- metawear.motor
- metawear.buzzer
- metawear.startAccelerometer
- metawear.stopAccelerometer
- metawear.disconnect
Intialize the metawear device.
metawear.init(success, failure);
Function 'init' initializes the metawear device. This method attempts to connect to a metawear device over bluetooth low energy. The success callback is called when a metawear device is found and connected to.
- success: Success callback function that is invoked when a metawear device is found and connected to.
- failure: Error callback function, invoked when error occurs. [optional]
Call if you'd like to listen for button press events on the metawear device.
metawear.listenForButton(failure, messageReceived, messageError);
Function 'listenForButton' adds an event listener to the metawear device button press. When a message is received the messageReceived handler is called.
- failure: Error callback function that is invoked when failing to register the button listener.
- messageReceived: Success callback function, invoked when the metawear button is pressed. [optional]
- messageError: Error callback function, invoked when the metawear button is pressed but there was an error retrieving the message. [optional]
Call to light up the LED on the metawear.
metawear.setLED(metawear.COLOR.BLUE);
Function 'setLED' allows you to tell the metawear led to light up with a specific color.
- color: The color to light the LED as. Supported colors for the metawear are COLOR.RED, COLOR.GREEN, and COLOR.BLUE. If no color is specified the color will be green [optional]
Plays the saved color channel on the metawear
metawear.play(autoplay);
Function 'play' tells the metawear to being playing the saved LED color pattern.
- autoplay: boolean value to indicate whether the color pattern should automatically play or not.
Pauses the LED on the metawear
metawear.pause();
Function 'pause' causes the metawear to stop playing the LED color pattern.
Stops the saved color channel on the metawear
metawear.stop(clearPattern);
Function 'stop' tells the metawear to stop playing the saved LED color pattern.
- clearPattern: boolean value to indicate whether the color pattern should be cleared out. If you were to call the play method after calling the stop method with the clearPattern flag set to true, nothing will play because the pattern has been removed. Instead you'll have to first add a color to the pattern with the 'setLED' function.
Tells the optional motor sensor to pulse
metawear.motor(pulseLength);
Function 'motor' tells the optional metawear motor to pulse.
- pulseLength: value indicates how long you'd like the motor to pulse.
Tells the optional buzzer sensor to pulse
metawear.buzzer(pulseLength);
Function 'buzzer' tells the optional metawear buzzer to pulse.
- pulseLength: value indicates how long you'd like the motor to pulse.
Tells the metawear to start sharing information about the accelerometer
metawear.startAccelerometer();
Function 'startAccelerometer' tells the metawear to start sharing information about the accelerometer. We can use the information returned to see how the values have changed.
onDataReceived : function(buffer) { // data received from MetaWear
console.log('data received plugin handler');
var data = new Uint8Array(buffer);
if (data[0] === 3 && data[1] === 4) { // module = 3, opscode = 4
//console.log('accelerometer data is: ' + JSON.stringify(data));
//FYI guessing as the xyz values
var d2 = data[2]; //x
var d3 = data[3];
var d4 = data[4]; //y
var d5 = data[5];
var d6 = data[6]; //z
var d7 = data[7];
message = "Got accelerometer information: [2]"
+ d2 + ",[3]" + d3
+ ",[4]" + d4
+ ",[5]" + d5
+ ",[6]" + d6
+ ",[7]" + d7;
//console.log("ACCELEROMETER MESSAGE: " + message);
//compare against old values
var xdiff = Math.abs(metawear.accelerometerVALS.x - d2);
if (xdiff > 30 && metawear.accelerometerVALS.x !== 22){
console.log("x value changes more than 30 degrees: " + xdiff);
console.log("ACCELEROMETER MESSAGE: " + message);
metawear.setLED(metawear.COLOR.RED);
}
var ydiff = Math.abs(metawear.accelerometerVALS.y - d4);
if (ydiff > 30 && metawear.accelerometerVALS.x !== 22){
console.log("y value changes more than 30 degrees: " + ydiff);
console.log("ACCELEROMETER MESSAGE: " + message);
metawear.setLED(metawear.COLOR.GREEN);
}
//reset accelerometer values
metawear.accelerometerVALS.x = d2;
metawear.accelerometerVALS.y = d4;
metawear.accelerometerVALS.z = d6;
//all the rest of the values are the same
}
Tells the metawear to stop sharing information about the accelerometer
metawear.stopAccelerometer();
Function 'startAccelerometer' tells the metawear to start sharing information about the accelerometer. We can use the information returned to see how the values have changed.
Disconnects the metawear device from bluetooth.
metawear.disconnect(success, failure);
Function 'disconnect' disconnects the metawear device. This method attempts to disconnect the attached metawear device over bluetooth low energy. The success callback is called when a metawear device is successfully disconnected.
- success: Success callback function that is invoked when a metawear device is disconnected.
- failure: Error callback function, invoked when error occurs. [optional]
To download a sample application to see these methods in action, visit: https://github.com/ldeluca/metawear
If you have any questions or find a problem with the code, please file an issue or create a pull request against the github repository.