Skip to content

Instantly share code, notes, and snippets.

@zeroasterisk
Created October 21, 2013 12:51
Show Gist options
  • Save zeroasterisk/7083403 to your computer and use it in GitHub Desktop.
Save zeroasterisk/7083403 to your computer and use it in GitHub Desktop.
Hardware interface information/exchange for MeteorJS + Meteor-Cordova https://github.com/SpaceCapsule/Meteor-Cordova
/**
* Utility functions to help determine what kind of device you are on,
* and what it's capabilities are...
*
* This is very much a "custom" implementation to my needs, but may help as
* a starting point for others and perhaps for more generic Hardware
* information.
*
* Current version relies on Meteor-Cordova (the iframe appraoch)
* https://github.com/SpaceCapsule/Meteor-Cordova
*
* Easy to change the "guts" of these methods to handle other interface
* aproaches.
*
* TODO: abstract to support all of them
*
* NOTE: some overlap between this and client/lib/cordova.js
*/
// setup "cordova" re: https://github.com/SpaceCapsule/Meteor-Cordova
cordova = new Cordova({
plugins: {
// native plugin functionality
notification: true
}
});
// setup "Hardware" a class to abstract device handling (and support browsers)
Hardware = {
device: { name: 'default', platform: 'Browser' },
isPhoneGap: false,
/**
* Initialize hardware -- autorun on startup
* - ensures we are on PhoneGap or not
*
* @return void
*/
init: function() {
this.checkIsPhoneGap();
},
/**
* Query to see if we are on PhoneGap
* and setup basic "device" details if we are.
*
* This does an APP --> PhoneGap query/call
* and loads the values found into "self"
*
* return boolean or void
*/
checkIsPhoneGap: function() {
if (!_.isObject(cordova)) {
return false;
}
cordova.call('PG.device', [], function(value) {
Hardware.isPhoneGap = _.isObject(value);
Hardware.device = value;
});
},
/**
* Blink the Device Flashlight/Torch
* (setup via PhoneGap library: pg.js)
* (relies on a Torch plugin to PhoneGap which is a bit buggy)
*
* @param string style [fast] mid slow on off/release
* @return boolean
*/
blink: function(style) {
if (!this.isPhoneGap) {
return false;
}
if (_.isObject(style)) {
return cordova.call('PG.blink', [style]);
}
if (!_.isString(style)) {
style = 'fast'; // default
}
if (style == 'slow') {
return cordova.call('PG.blinkSlow', []);
}
if (style == 'mid') {
return cordova.call('PG.blinkMid', []);
}
if (style == 'on') {
return cordova.call('PG.blinkOn', []);
}
if (style == 'off' || style == 'release') {
return cordova.call('PG.blinkRelease', []);
}
return cordova.call('PG.blinkFast', []);
},
/**
* Can this device support QR code scanning
* (relies on a barcodescanner plugin to PhoneGap)
*
* @return boolean
*/
canScan: function() {
return (this.isPhoneGap);
}
};
// autorun Hardware init, all the time...
Hardware.init();
@raix
Copy link

raix commented Oct 21, 2013

Cool didnt know about the blink plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment