【问题标题】:Add ambient light sensor into cordova-2.5.0.js - phonegap将环境光传感器添加到 cordova-2.5.0.js - phonegap
【发布时间】:2016-01-17 00:45:25
【问题描述】:

我必须使用 phongap 开发一个从设备检索传感器数据的 Android 应用程序。

我必须听的其中一个传感器是环境光传感器。这个传感器在 phoneGap 中没有实现,所以我必须将它作为插件添加到 PhoneGap。

我知道如何添加插件,也知道如何从 Java 访问 ALS 数据 - 但为了确保我很好地实现它,我想实现它,因为 PhoneGap 实现了其他传感器,如 Accelerometer。因此,我在 java 中编写了一个 ALSManager 类,因为我发现 Accelerometer 是在这里实现的:

https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/AccelListener.java

并添加了 lightSensor 和 lightValues 模块,如加速器和加速模块。

但是当我运行这个应用程序时,我收到了以下错误消息:

TypeError: Object # has no method '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;

});

【问题讨论】:

  • 您能发布您的 config.xml 和 javascript 文件吗?听起来好像从 javascript 到 java 的映射是错误的。
  • 您能告诉我您的电子邮件地址吗?我会把文件发给你吗?它们太长了……
  • 好吧,我希望我们继续在这里讨论,以便任何偶然发现这个问题的人都能得到答案。您可以使用 pastebucket.com 之类的服务来共享代码,或者您可以在 github 上创建一个帐户并 fork cordova-android 并在那里共享代码。您可能也不需要发布整个文件,只需将插件的 javascript 端添加到页面的部分以及列出插件的 config.xml 即可。
  • @MBillau:请参阅上面的编辑

标签: android cordova phonegap-plugins


【解决方案1】:

我认为问题可能在于您将插件代码添加到cordova-2.5.0.js 文件中。相反,您应该为每个 JavaScript 文件创建一个独立的 JS 文件,然后在您想要使用该功能的 HTML 页面中 cordova.require() 这些文件。

因此,在您的 www 文件夹中的某处创建 LightValues.jsLightSensor.js 作为单独的文件。然后在您的 HTML 文件中,确保包含 JS 文件:&lt;script type="text/javascript" src="path-to-lightSensor.JS-file"&gt;(您只需要包含这个文件,因为它需要()第二个文件。)

接下来,在deviceReady() 函数中,您可以使用var lightSensor = cordova.require("cordova/plugin/lightSensor") 调用光传感器。请注意,cordova/plugin/lightSensor 不是 JS 文件的路径,而是您在编写插件时在 define() 部分中声明的模块的名称。

在此之后,您应该能够调用 lightSensor.getCurrentLight()。如果您console.log(lightSensor),您会希望看到您编写的所有可用方法。

请注意,我不肯定 cordova.requirecordova.define 在 cordova-2.5 中工作。我希望他们这样做,但this page sort of suggests it may not be supported until 2.6。如果您在拆分文件后遇到问题,可能是因为这个。

【讨论】:

    【解决方案2】:

    我刚刚开发了一个光传感器插件,幸运的是成功了。我刚刚阅读了上面的代码,发现了一些关于标识符的小错误,例如“var Acceleration = function(lux, timestamp)”变量应该是 LightValues 而不是 Acceleration。所以请先检查代码以避免一些基本错误。然后,我首先使用“lux”作为变量的名称,但是在调试程序时我得到了一个未定义的 light 值。所以我将“lux”改为“x”,它确实有效!

    有6个文件需要注意:“LightListener.java”、“LightValues.js”、“LightSensor.js”、“cordova_plugin.js”、“index.html”和“config.xml”。如果你配置所有 6 个文件,程序应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      相关资源
      最近更新 更多