我正在尝试编写一个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的经验来帮助我确定原因可能是什么?
干杯, 弥敦道
为了获得更详细的调试信息,请尝试设置IBeaconManager.LOG_DEBUG = true;然后再次运行。如果您收到更多日志消息,请详细编辑您的问题。 – davidgyoung