ibeacon - No monitoring and no change of scan period in a service using altbeacon android library -
i'm android-beginner , testing out estimote-beacons on android. project worked estimote library want try more open altbeacon-library, have several problems.
the function of app should this: when app started, beaconapplication should start beaconsmonitoringservice. 2 regions should monitored. if region entered, service should sent intent using broadcast manager. information of intent checkbox should checked/unchecked in main-activity. service started when app killed or bluetooth state has changed.
the problems are:
- no region detected
- the scan periods not set
all debug-logs shown, except "entered" , "left" onbeaconserviceconnect. appreciated. it's dumb/newbie error , works generally. :)
here ist code of beaconapplication:
package de.mcd.presencealtbeacon; import android.app.application; import android.content.intent; import org.altbeacon.beacon.beaconmanager; public class beaconapplication extends application { private beaconmanager beaconmanager = null; @override public void oncreate() { super.oncreate(); beaconmanager = beaconmanager.getinstanceforapplication(this); startservice(new intent(getapplicationcontext(), beaconsmonitoringservice.class)); } public beaconmanager getbeaconmanager() { if (beaconmanager == null) { beaconmanager = beaconmanager.getinstanceforapplication(this); } return beaconmanager; } }
here code of beaconsmonitoringservice:
package de.mcd.presencealtbeacon; import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.app.service; import android.content.context; import android.content.intent; import android.os.ibinder; import android.os.remoteexception; import android.support.v4.content.localbroadcastmanager; import android.util.log; import org.altbeacon.beacon.beaconconsumer; import org.altbeacon.beacon.beaconmanager; import org.altbeacon.beacon.identifier; import org.altbeacon.beacon.monitornotifier; import org.altbeacon.beacon.region; import org.altbeacon.beacon.beaconparser; public class beaconsmonitoringservice extends service implements beaconconsumer { private static final string uuid = "1234567-1234-1234-1234-123456789012"; private static final region room = new region("mcd", identifier.parse(uuid), identifier.fromint(1), null); private static final region kitchen = new region("mcd", identifier.parse(uuid), identifier.fromint(2), null); private static final string tag = "beacon"; private beaconmanager beaconmanager; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { log.d(tag, "beacons monitoring service started"); } @override public void ondestroy() { log.d(tag, "beacons monitoring service destroyed"); } public void onbeaconserviceconnect(){ beaconmanager.setmonitornotifier(new monitornotifier() { @override public void didenterregion(region region) { log.d(tag, "entered"); if (region.getid2() == identifier.fromint(1)) { postnotification("room", "entered"); intent("1-1"); } else { postnotification("kitchen", "entered"); intent("2-1"); } } @override public void didexitregion(region region) { log.d(tag, "left"); if (region.getid2() == identifier.fromint(1)) { postnotification("room", "left"); intent("1-2"); } else { postnotification("kitchen", "left"); intent("2-2"); } } @override public void diddeterminestateforregion(int state, region region) { log.d(tag, "don't know it's useful for" + state); } }); try { log.d(tag, "service ready"); beaconmanager.startmonitoringbeaconsinregion(room); beaconmanager.startmonitoringbeaconsinregion(kitchen); } catch (remoteexception e) { log.e(tag, "cannot start ranging", e); } } @override public int onstartcommand(intent intent, int flags, int startid) { log.d(tag, "onstart start"); beaconapplication app = (beaconapplication)getapplication(); beaconmanager = app.getbeaconmanager(); beaconmanager.setbackgroundscanperiod(1100l); beaconmanager.setbackgroundbetweenscanperiod(10000l); beaconmanager.getbeaconparsers ().add ( new beaconparser ().setbeaconlayout ( "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24" ) ); beaconmanager.bind(this); log.d(tag, "onstart end"); notification noti = new notification.builder(this) .setcontenttitle("started") .setcontenttext("here go") .setsmallicon(r.mipmap.ic_launcher) .build(); notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service); mnotificationmanager.cancel(2); mnotificationmanager.notify(1, noti); return start_sticky; } private void postnotification(string room, string action) { intent notificationintent = new intent(beaconsmonitoringservice.this, myactivity.class); notificationintent.setflags(intent.flag_activity_clear_top | intent.flag_activity_single_top); pendingintent intent = pendingintent.getactivity(beaconsmonitoringservice.this, 0, notificationintent, 0); notification noti = new notification.builder(beaconsmonitoringservice.this) .setcontenttitle(room) .setcontenttext(action) .setsmallicon(r.mipmap.ic_launcher) .setcontentintent(intent) .build(); notificationmanager mnotificationmanager = (notificationmanager) getsystemservice(context.notification_service); mnotificationmanager.cancel(2); mnotificationmanager.notify(1, noti); } private void intent (string code){ intent intent = new intent("statechanged"); intent.putextra("info", code); localbroadcastmanager.getinstance(beaconsmonitoringservice.this).sendbroadcast(intent); } }
one issue first parameter of region
constructor unique string identify region library. each region must have unique string, otherwise replace region same identifier when start ranging or monitoring. code below uses same identifier 2 regions:
private static final region room = new region("mcd", identifier.parse(uuid), identifier.fromint(1), null); private static final region kitchen = new region("mcd", identifier.parse(uuid), identifier.fromint(2), null);
this causes second monitored region replace first 1 in code below:
beaconmanager.startmonitoringbeaconsinregion(room); beaconmanager.startmonitoringbeaconsinregion(kitchen);
to fix this, change region setup to:
private static final region room = new region("mcd1", identifier.parse(uuid), identifier.fromint(1), null); private static final region kitchen = new region("mcd2", identifier.parse(uuid), identifier.fromint(2), null);
on more fundamental level, building background service starts automatically in background pretty advanced task tackle self-described beginner. there many other issues aren't obvious looking @ code. android beacon library designed start automatically in background , scan beacons scan intervals change appropriately app changes foreground background.
you can see simple example of in application class in reference app here.
i recommend instead of using beaconmoinitoringservice
copy code reference app's application class beaconapplication
class. can move code didenterregion
, didexitregion
methods of beaconapplication
class beaconmonitoringservice
class.
if you'd prefer approach custom service, i'm sure possible, bit of uphill climb.
Comments
Post a Comment