2013-12-09 12 views
0

我正在嘗試爲Android開發一個iBeacon Cordova 3.x插件,但它不起作用。我不斷收到以下錯誤: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); 
    } 
}; 

然後我得到錯誤如上所述。我用google搜索了一下github中的其他代碼,但是我找不到這個錯誤。

謝謝。

回答

0

我認爲,以下兩個進口導致此問題:

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

,您可檢查這些課程有哪些?

+0

是的,它們可用。我通過以下方式添加這些類: 在Eclipse中,我轉到Project> Properties> Android> Library> Add並選擇包含這些類的庫。我已經在一個原生的android應用程序中遵循這個過程,它的效果很好。我需要以不同的方式爲科爾多瓦插件做到這一點嗎? – hugohabel