i hope can guide me issue. i'm attempting use altbeaconorg, open-source android ibeacon wrapper created xamarin.
the issue i'm facing when listener implemented in service follows, works fine.
[service] [intentfilter(new [] {"com.testapp.sensorservice"})] public class droidsensorservice : service, irangenotifier, ibeaconconsumer { static readonly string tag = typeof(droidsensorservice).name; private ibinder binder; private region _region; private beaconmanager _beaconmanager; public droidsensorservice() { } public override void oncreate() { base.oncreate(); log.debug(tag, "oncreate called in service"); } public override startcommandresult onstartcommand(intent intent, startcommandflags flags, int startid) { log.debug(tag, "service started"); _beaconmanager = beaconmanager.getinstanceforapplication(applicationcontext); _beaconmanager.bind(this); return startcommandresult.sticky; } public override ibinder onbind(intent intent) { binder = new droidsensorservicebinder(this); return binder; } public override void ondestroy() { base.ondestroy(); log.debug(tag, "service has been terminated"); } public void onbeaconserviceconnect() { _beaconmanager.setforegroundbetweenscanperiod(5000); _beaconmanager.setrangenotifier(this); _region = new altbeaconorg.boundbeacon.region("region", null, null, null); _beaconmanager.startrangingbeaconsinregion(_region); } public void didrangebeaconsinregion(icollection<beacon> beacons, region region) { } }
the didrangebeacons event raised when new beacons found.
if implement solution using class wrap beacon code such as:
[service] [intentfilter(new [] {"com.testapp.sensorservice"})] public class droidsensorservice : service, isensorservice { static readonly string tag = typeof(droidsensorservice).name; private ibinder binder; private mybeaconclass _mybeaconclass; public droidsensorservice() { _mybeaconclass = new mybeaconclass(this); } public override void oncreate() { base.oncreate(); log.debug(tag, "oncreate called in service"); } public override startcommandresult onstartcommand(intent intent, startcommandflags flags, int startid) { log.debug(tag, "service started"); _mybeaconclass.start(); return startcommandresult.sticky; } public override ibinder onbind(intent intent) { binder = new droidsensorservicebinder(this); return binder; } public override void ondestroy() { base.ondestroy(); log.debug(tag, "sensor service has been terminated"); } } public class mybeaconclass : irangenotifier, ibeaconconsumer { private context _context; private region _region; private beaconmanager _beaconmanager; public mybeaconclass(context context) { _context = context; } public context applicationcontext { { return _context.applicationcontext; } } public intptr handle { { return _context.handle; } } public void start() { _beaconmanager = beaconmanager.getinstanceforapplication(_context.applicationcontext); _beaconmanager.bind(this); } public void onbeaconserviceconnect() { _beaconmanager.setforegroundbetweenscanperiod(5000); _beaconmanager.setrangenotifier(this); _region = new altbeaconorg.boundbeacon.region("region", null, null, null); _beaconmanager.startrangingbeaconsinregion(_region); } public void didrangebeaconsinregion(icollection<beacon> beacons, region region) { } public bool bindservice(intent intent, iserviceconnection serviceconnection, [generatedenum] bind flags) { return _context.bindservice(intent, serviceconnection, flags); } public void unbindservice(iserviceconnection serviceconnection) { _context.unbindservice(serviceconnection); } public void dispose() { } }
i following error produced:
could not activate jni handle 0x7fefabe700 (key_handle 0x11c1ff5) of java type 'md53c8887b34fc5ad8a1fb3519f652d3fea/droidsensorservice' managed type 'sensors.droid.classes.droidsensorservice'.
at java.interop.typemanager.n_activate (intptr jnienv, intptr jclass, intptr typename_ptr, intptr signature_ptr, intptr jobject, intptr parameters_ptr) [0x00180] in /users/builder/data/lanes/3540/1cf254db/source/monodroid/src/mono.android/src/java.interop/typemanager.cs:176 @ (wrapper dynamic-method) system.object:ac4a9174-05aa-4926-9a80-8a8455916649 (intptr,intptr,intptr,intptr,intptr,intptr)
the inner exception along lines of "attempt invoke virtual method 'android.content.context..." (xamarin studio cuts off).
if move line instantiating mybeaconclass above start() call, application on phone crashes no exception raised in xamarin studio.
i appreciate code intended work in activity or service class directly, neater if able achieve this, share service other functionality.
of note: i've tried using context injected directly , android.app.application.context directly too.
thanks reading, richard
your mybeaconclass
has inherit java.lang.object
in order jni wrapper created xamarin.android
java calls marshaled c#. remember, dealing 2 vms have bridged:
public class mybeaconclass : java.lang.object, irangenotifier, ibeaconconsumer { ~~~ }
Comments
Post a Comment