簡體   English   中英

Cordova 3.x的插件開發

[英]Plugin development for Cordova 3.x

我正在嘗試為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;

您可以檢查這些課程是否可用嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM