Android 将环境光照传感器添加到cordova-2.5.0.js-phonegap中
我必须使用phongap开发一个Android应用程序,从设备中检索传感器数据 我必须听的传感器之一是环境光传感器。这个传感器在phoneGap中没有实现,所以我必须将它作为插件添加到phoneGap中 我知道如何添加插件,我知道如何从Java访问ALS数据——但为了确保我很好地实现了它,我想将其实现为PhoneGap实现其他传感器,如加速计。因此,我用java编写了一个Android 将环境光照传感器添加到cordova-2.5.0.js-phonegap中,android,cordova,phonegap-plugins,Android,Cordova,Phonegap Plugins,我必须使用phongap开发一个Android应用程序,从设备中检索传感器数据 我必须听的传感器之一是环境光传感器。这个传感器在phoneGap中没有实现,所以我必须将它作为插件添加到phoneGap中 我知道如何添加插件,我知道如何从Java访问ALS数据——但为了确保我很好地实现了它,我想将其实现为PhoneGap实现其他传感器,如加速计。因此,我用java编写了一个ALSManager类,我在这里实现了加速度计: 并添加了lightSensor和lightValues模块,如accele
ALSManager
类,我在这里实现了加速度计:
并添加了lightSensor和lightValues模块,如acceleromter和acceleration模块
但当我运行此应用程序时,收到以下错误消息:
TypeError:对象#没有方法“getCurrentLight”
(在lightSensor模块中,我有getCurrentLight方法)
有人能告诉我我缺少什么吗?或者我该怎么办
提前感谢,
我在cordova-2.5.0.js中添加的代码。如果还不够,请告诉我:
// file: lib/common/plugin/LightValues.js
define("cordova/plugin/LightValues", function(require, exports, module) {
var Acceleration = function(lux, timestamp) {
this.lux = lux;
this.timestamp = timestamp || (new Date()).getTime();
};
module.exports = LightValues;
});
// file: lib/common/plugin/lightSensor.js
define("cordova/plugin/lightSensor", function(require, exports, module) {
/**
* This class provides access to device accelerometer data.
* @constructor
*/
var argscheck = require('cordova/argscheck'),
utils = require("cordova/utils"),
exec = require("cordova/exec"),
LightValues = require('cordova/plugin/LightValues');
// Is the accel sensor running?
var running = false;
// Keeps reference to watchAcceleration calls.
var timers = {};
// Array of listeners; used to keep track of when we should call start and stop.
var listeners = [];
// Last returned acceleration object from native
var light = null;
// Tells native to start.
function start() {
exec(function(a) {
var tempListeners = listeners.slice(0);
light = new LightValues(a.lux, a.timestamp);
for (var i = 0, l = tempListeners.length; i < l; i++) {
tempListeners[i].win(light);
}
}, function(e) {
var tempListeners = listeners.slice(0);
for (var i = 0, l = tempListeners.length; i < l; i++) {
tempListeners[i].fail(e);
}
}, "Light", "start", []);
running = true;
}
// Tells native to stop.
function stop() {
exec(null, null, "Light", "stop", []);
running = false;
}
// Adds a callback pair to the listeners array
function createCallbackPair(win, fail) {
return {win:win, fail:fail};
}
// Removes a win/fail listener pair from the listeners array
function removeListeners(l) {
var idx = listeners.indexOf(l);
if (idx > -1) {
listeners.splice(idx, 1);
if (listeners.length === 0) {
stop();
}
}
}
var lightSensor = {
/**
* Asynchronously acquires the current acceleration.
*
* @param {Function} successCallback The function to call when the acceleration data is available
* @param {Function} errorCallback The function to call when there is an error getting the acceleration data. (OPTIONAL)
* @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL)
*/
getCurrentLight: function(successCallback, errorCallback, options) {
//argscheck.checkArgs('fFO', 'lightSensor.getCurrentLight', arguments);
var p;
var win = function(a) {
removeListeners(p);
successCallback(a);
};
var fail = function(e) {
removeListeners(p);
errorCallback && errorCallback(e);
};
p = createCallbackPair(win, fail);
listeners.push(p);
if (!running) {
start();
}
},
/**
* Asynchronously acquires the acceleration repeatedly at a given interval.
*
* @param {Function} successCallback The function to call each time the acceleration data is available
* @param {Function} errorCallback The function to call when there is an error getting the acceleration data. (OPTIONAL)
* @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL)
* @return String The watch id that must be passed to #clearWatch to stop watching.
*/
watchLight: function(successCallback, errorCallback, options) {
//argscheck.checkArgs('fFO', 'lightSensor.watchLight', arguments);
// Default interval (10 sec)
var frequency = (options && options.frequency && typeof options.frequency == 'number') ? options.frequency : 10000;
// Keep reference to watch id, and report accel readings as often as defined in frequency
var id = utils.createUUID();
var p = createCallbackPair(function(){}, function(e) {
removeListeners(p);
errorCallback && errorCallback(e);
});
listeners.push(p);
timers[id] = {
timer:window.setInterval(function() {
if (light) {
successCallback(light);
}
}, frequency),
listeners:p
};
if (running) {
// If we're already running then immediately invoke the success callback
// but only if we have retrieved a value, sample code does not check for null ...
if (light) {
successCallback(light);
}
} else {
start();
}
return id;
},
/**
* Clears the specified accelerometer watch.
*
* @param {String} id The id of the watch returned from #watchAcceleration.
*/
clearWatch: function(id) {
// Stop javascript timer & remove from timer list
if (id && timers[id]) {
window.clearInterval(timers[id].timer);
removeListeners(timers[id].listeners);
delete timers[id];
}
}
};
module.exports = lightSensor;
});
//文件:lib/common/plugin/LightValues.js
定义(“cordova/plugin/LightValues”、函数(要求、导出、模块){
var加速度=功能(勒克斯,时间戳){
这个。勒克斯=勒克斯;
this.timestamp=timestamp | |(new Date()).getTime();
};
module.exports=LightValue;
});
//文件:lib/common/plugin/lightSensor.js
定义(“cordova/plugin/lightSensor”,功能(要求、导出、模块){
/**
*此类提供对设备加速计数据的访问。
*@constructor
*/
var argscheck=require('cordova/argscheck'),
utils=要求(“科尔多瓦/utils”),
exec=要求(“cordova/exec”),
LightValues=require('cordova/plugin/LightValues');
//加速度传感器是否正在运行?
var running=false;
//保持对watchAcceleration调用的引用。
变量计时器={};
//侦听器数组;用于跟踪何时调用start和stop。
var侦听器=[];
//上次从本机返回的加速对象
var-light=null;
//告诉本机启动。
函数start(){
执行主任(职能(a){
var templateners=listeners.slice(0);
灯光=新灯光值(a.lux,a.timestamp);
for(var i=0,l=templalisteners.length;i-1){
剪接(idx,1);
if(listeners.length==0){
停止();
}
}
}
var光传感器={
/**
*异步获取当前加速度。
*
*@param{Function}成功回调加速度数据可用时要调用的函数
*@param{Function}errorCallback获取加速度数据时出错时要调用的函数。(可选)
*@param{AccelerationOptions}选项获取加速计数据的选项,如超时。(可选)
*/
getCurrentLight:函数(successCallback、errorCallback、options){
//argscheck.checkArgs('fFO','lightSensor.getCurrentLight',参数);
var-p;
var win=函数(a){
重组监听器(p);
成功(a);
};
var失效=功能(e){
重组监听器(p);
errorCallback&&errorCallback(e);
};
p=createCallbackPair(赢,失败);
听众。推(p);
如果(!正在运行){
start();
}
},
/**
*以给定的间隔重复异步获取加速度。
*
*@param{Function}成功回调每次加速度数据可用时要调用的函数
*@param{Function}errorCallback获取加速度数据时出错时要调用的函数。(可选)
*@param{AccelerationOptions}选项获取加速计数据的选项,如超时。(可选)
*@return String必须传递给#clearWatch才能停止观看的手表id。
*/
watchLight:函数(successCallback、errorCallback、options){
//argscheck.checkArgs('fFO','lightSensor.watchLight',参数);
//默认间隔(10秒)
变量频率=(options&&options.frequency&&typeof options.frequency==“number”)?options.frequency:10000;
//保持参考手表id,并按照频率中的定义报告加速度读数
var id=utils.createUUID();
var p=createCallbackPair(函数(){},函数(e){
重组监听器(p);
errorCallback&&errorCallback(e);
});
听众。推(p);
计时器[id]={
计时器:window.setInterval(函数(){
如果(灯){
成功回调(轻);
}
},频率),
听众:p
};
如果(正在运行){
//如果我们已经在运行,那么立即调用成功回调
//但只有当我们检索到一个值时,示例代码才不会检查null。。。
如果(灯){
成功回调(轻);
}
}否则{
start();
}
返回id;
},
/**
*清除指定的加速计手表。
*
*@param{String}id Th