Использование iBeacons в плагине Cordova, то есть вне Activity / Service
пытаюсь написать плагин Cordova, который взаимодействует с библиотекой Radius Networks iBeacon для Android. Сейчас я'Я знаю, что библиотека предназначена для использования с 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 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, чтобы помочь мне определить причину?
Ура, Натан