2014-01-23 28 views
2

我正在尝试编写一个Cordova插件,它与Android的Radius Networks iBeacon库进行交互。现在,我知道这个库是为使用Activity/Service而设计的,但这不适用于我的情况,所以我正在尽可能地使用它来适应文档。目前,我只是试图将它看到的iBeacons数量转储出来。在Cordova插件中使用iBeacons,即活动/服务之外

这里是我的插件代码,我省略了包/进口简洁:

public class IBeaconPlugin extends CordovaPlugin implements IBeaconConsumer { 

    public static final String TAG = IBeaconPlugin.class.getName(); 

    public static final String ACTION_FIND = "findBeacons"; 

    private int count = -1; 

    public Context context; 

    private IBeaconManager iBeaconManager; 

    /** 
    * Constructor. 
    */ 
    public IBeaconPlugin() { 
    } 

    /** 
    * Sets the context of the Command. This can then be used to do things like 
    * get file paths associated with the Activity. 
    * 
    * @param cordova The context of the main Activity. 
    * @param webView The CordovaWebView Cordova is running in. 
    */ 
    public void initialize(CordovaInterface cordova, CordovaWebView webView) { 
     super.initialize(cordova, webView); 

     context = cordova.getActivity().getApplicationContext(); 

     iBeaconManager = IBeaconManager.getInstanceForApplication(context); 
     Log.d(TAG, iBeaconManager.toString()); 

     iBeaconManager.bind(this); 
    } 

    @Override 
    public Context getApplicationContext() { 
     return context; 
    } 

    @Override 
    public boolean bindService(Intent arg0, ServiceConnection arg1, int arg2) { 
     // TODO Auto-generated method stub 
     return true; 
    } 

    @Override 
    public void unbindService(ServiceConnection arg0) { 
     // TODO Auto-generated method stub 

    } 

    /** 
    * Executes the request and returns PluginResult. 
    * 
    * @param action   The action to execute. 
    * @param args    JSONArry of arguments for the plugin. 
    * @param callbackContext The callback id used when calling back into JavaScript. 
    * @return     True if the action was valid, false if not. 
    */ 
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
     if (action.equals(ACTION_FIND)) { 
      JSONObject r = new JSONObject(); 
      r.put("thebeacons", this.count); 
      callbackContext.success(r); 
     } 
     else { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public void onIBeaconServiceConnect() { 
     Log.d(TAG, "onIBeaconServiceConnect called"); 

     iBeaconManager.setRangeNotifier(new RangeNotifier() { 
     @Override 
     public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) { 
      IBeaconPlugin.this.count = iBeacons.size(); 
     } 
     }); 

     try { 
      iBeaconManager.startRangingBeaconsInRegion(new Region("allTheBeacons", null, null, null)); 
     } catch (RemoteException e) { } 
    } 
} 

正如你所看到的,我试着输入一些记录,以便我有流动的想法。由此我可以确定“onIBeaconServiceConnect”永远不会被调用。看在日志中,我有以下线,我认为是罪魁祸首:

D/IBeaconManager:此消费者没有绑定。绑定:[email protected]

不幸的是,这就是我得到的所有信息。

有没有人有API的经验来帮助我确定原因可能是什么?

干杯, 弥敦道

+0

为了获得更详细的调试信息,请尝试设置IBeaconManager.LOG_DEBUG = true;然后再次运行。如果您收到更多日志消息,请详细编辑您的问题。 – davidgyoung

回答

7

你空bindServiceunbindService方法导致您的问题。如果你的课程是一个活动,服务或应用程序,这些都将为你实施。如果你想创建一个自定义类,你必须提供一个实现。

如果没有执行bindService方法,iBeaconManager.bind(this);行最终不会执行任何操作,因为它反过来必须调用其中一种方法。 (将日志行添加到这些空方法中,您将看到。)结果,IBeaconService永远不会启动,并且onIBeaconServiceConnect永远不会被调用。

足够无聊的解释 - 现在是解决问题的时候了!最简单的做法是链接这些方法。尝试类似于:

@Override 
public boolean bindService(Intent intent, ServiceConnection conn, int mode) { 
    return context.bindService(intent, conn, mode); 
} 

@Override 
public void unbindService(ServiceConnection conn) { 
    context.unbindService(conn); 
} 
+0

感谢那@davidgyoung! “确实是这个问题。一旦我这样做了,并在清单中添加了缺失的服务定义,我就能够愉快地发现坐在我旁边的3个信标。感谢那。 – thenathanjones

+1

只需要在一个页面上获得整个解决方案:这些是清单文件中缺少的服务定义,那么thajjones正在讨论:' android:label =“iBeacon”'。只要把它放在'应用程序'里面。 –

相关问题