我正在尝试编写一个与 Android 的 Radius Networks iBeacon 库交互的 Cordova 插件。现在,我知道该库是为与 Activity/Service 一起使用而设计的,但这在我的情况下不起作用,因此我正在尝试使用文档尽可能地对其进行调整。目前,我只是想让它转储它可以看到的 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﹕此消费者未绑定。绑定:com.thenathanjones.cordova.ibeacon.IBeaconPlugin@52962c9c
不幸的是,这就是我得到的所有信息。
有没有人对 API 有任何经验来帮助我确定原因可能是什么?
干杯,内森