Cordova 3.x插件开发

Plugin development for Cordova 3.x

本文关键字:开发 插件 Cordova      更新时间:2024-05-30

我正在尝试为Android开发一个iBeacon Cordova 3.x插件,但它不起作用。我一直得到以下错误:

 Error adding plugin me.habel.plugins.IBeacon.IBeacon
 exec() call to unknown plugin: IBeacon
 Error: Class not found

我的目录结构如下:

MyiBeaconsPlugin
 + src
  + android
   - IBeacon.java
 + www
  - ibeacon.js
 - plugin.xml

IBeacon.java文件的源代码是:

package me.habel.plugins.IBeacon;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import com.radiusnetworks.ibeacon.IBeaconConsumer;
import com.radiusnetworks.ibeacon.IBeaconManager;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
public class IBeacon extends CordovaPlugin implements IBeaconConsumer {
    private static String DEBUG_TAG = "iBeacon :: DEBUG => ";
    public static final String ACTION_VERIFY_BT = "verifyBluetooth";
    private IBeaconManager iBeaconManager = IBeaconManager
            .getInstanceForApplication(this.cordova.getActivity().getApplicationContext());
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
            throws JSONException {
        try {
            if (action.equalsIgnoreCase(ACTION_VERIFY_BT)) {
                verifyBluetooth();
                callbackContext.success();
                return true;
            }
        } catch (Exception e) {
            System.out.println(DEBUG_TAG + e.getMessage());
            callbackContext.error(e.getMessage());
            return false;
        }
        return false;
    }
    private void verifyBluetooth() {
    }
    @Override
    public boolean bindService(Intent arg0, ServiceConnection arg1, int arg2) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public Context getApplicationContext() {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onIBeaconServiceConnect() {
        // TODO Auto-generated method stub
    }
    @Override
    public void unbindService(ServiceConnection arg0) {
        // TODO Auto-generated method stub
    }
}

ibeacon.js文件的源代码是:

var ibeacon = {
    verifyBluetooth: function (successCallback, errorCallback) {
        cordova.exec(
            successCallback,
            errorCallback,
            'IBeacon',
            'verifyBluetooth',
            [{
            }]
        );
    }
}
module.exports = ibeacon;

plugin.xml文件的源代码是:

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
    xmlns:android="http://schemas.android.com/apk/res/android"
    id="me.habel.cordova.plugins.ibeacon"
    version="1.0.1">
    <name>IBeacon</name>
    <description>iBeacon</description>
    <license>MIT</license>
    <engines>
        <engine name="cordova" version=">=3.0.0" />
    </engines>
    <js-module src="www/ibeacon.js" name="ibeacon">
        <clobbers target="window.ibeacon" />
    </js-module>
    <!-- android -->
    <platform name="android">
        <source-file src="src/android/com/IBeacon.java" target-dir="src/me/habel/plugins/IBeacon" />
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="IBeacon">
                <param name="android-package" value="me.habel.plugins.IBeacon.IBeacon" />
            </feature>
        </config-file>
    </platform>
</plugin>

然后在我的index.js文件上,我试着这样使用它:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        app.verifyBluetooth();
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');
        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');
        console.log('Received Event: ' + id);
    },
    verifyBluetooth: function () {
        var success = function () {
            console.log("Success");
        };
        var error = function (msg) {
            console.log("Error: " + msg);
        };
        ibeacon.verifyBluetooth(success, error);
    }
};

然后我得到上面提到的错误。我已经在谷歌上搜索并查看了github中的其他代码,但我找不到错误。

谢谢。

我认为,以下两个导入导致了问题:

import com.radiusnetworks.ibeacon.IBeaconConsumer;
import com.radiusnetworks.ibeacon.IBeaconManager;

你能检查一下这些课程是否可用吗?