android - Using Beacon Library from background service -
i trying utilize android beacon library in app runs background service. bind beaconmanager on service starting. unbind ondestroy. want stop , start ranging based on user action (on_duty / off_duty)
i have tried several combinations on lastly week can't seem right ranging restart after have stopped it. in cases next beaconintentprocessor:
"but ranging notifier null, we're dropping it."
what right methods phone call both stop , start ranging? should not able bind , unbind service based on (on_duty / off_duty) (i have tried have not been able create work consistently)
my app personal in field using beacons periodic checkins @ different locations while on_duty , disable ble in app while off_duty conserve battery. while app in off_duty mode app still maintains background service other functionality.
below startup sequence. not sure if there problem - @ end keeps looping through same "restarting scan" sequence.
10-06 23:23:16.348: d/beaconservice(9203): no org.altbeacon.beacon.simulatedscandata class exists. 10-06 23:23:16.348: i/beaconservice(9203): binding 10-06 23:23:16.408: i/beaconservice(9203): start monitoring received 10-06 23:23:16.408: d/beaconservice(9203): startmonitoring called 10-06 23:23:16.408: d/beaconservice(9203): monitoring 1 regions. 10-06 23:23:16.408: d/bluetoothadapter(9203): startlescan(): null 10-06 23:23:16.468: d/bluetoothadapter(9203): onclientregistered() - status=0 clientif=5 10-06 23:23:16.468: d/beaconservice(9203): waiting stop scan 1100 milliseconds 10-06 23:23:16.468: d/beaconservice(9203): scan started 10-06 23:23:17.478: d/beaconservice(9203): waiting stop scan 99 milliseconds 10-06 23:23:17.568: d/beaconservice(9203): done scan cycle 10-06 23:23:17.598: d/bluetoothadapter(9203): stoplescan() 10-06 23:23:17.608: d/beaconservice(9203): restarting scan. unique beacons seen lastly cycle: 0 total beacon advertisement packets seen: 0 10-06 23:23:17.608: d/bluetoothadapter(9203): startlescan(): null 10-06 23:23:17.618: d/bluetoothadapter(9203): onclientregistered() - status=0 clientif=5 10-06 23:23:17.618: d/beaconservice(9203): waiting stop scan 5000 milliseconds 10-06 23:23:17.618: d/beaconservice(9203): scan started 10-06 23:23:18.618: d/beaconservice(9203): waiting stop scan 3999 milliseconds 10-06 23:23:18.978: d/bluetoothadapter(9203): onscanresult() - device=78:a5:04:5b:1a:f9 rssi=-48 10-06 23:23:18.978: d/beaconservice(9203): got record 10-06 23:23:19.618: d/beaconservice(9203): waiting stop scan 2998 milliseconds 10-06 23:23:19.878: d/bluetoothadapter(9203): onscanresult() - device=78:a5:04:5b:1a:f9 rssi=-48 10-06 23:23:19.878: d/beaconservice(9203): got record 10-06 23:23:20.618: d/beaconservice(9203): waiting stop scan 1998 milliseconds 10-06 23:23:20.788: d/bluetoothadapter(9203): onscanresult() - device=78:a5:04:5b:1a:f9 rssi=-48 10-06 23:23:20.788: d/beaconservice(9203): got record 10-06 23:23:21.618: d/beaconservice(9203): waiting stop scan 997 milliseconds 10-06 23:23:21.698: d/bluetoothadapter(9203): onscanresult() - device=78:a5:04:5b:1a:f9 rssi=-48 10-06 23:23:21.698: d/beaconservice(9203): got record 10-06 23:23:22.608: d/bluetoothadapter(9203): onscanresult() - device=78:a5:04:5b:1a:f9 rssi=-48 10-06 23:23:22.608: d/beaconservice(9203): got record 10-06 23:23:22.618: d/beaconservice(9203): done scan cycle 10-06 23:23:22.618: d/bluetoothadapter(9203): stoplescan() 10-06 23:23:22.628: d/beaconservice(9203): restarting scan. unique beacons seen lastly cycle: 0 total beacon advertisement packets seen: 0 10-06 23:23:22.638: d/bluetoothadapter(9203): startlescan(): null 10-06 23:23:22.638: d/bluetoothadapter(9203): onclientregistered() - status=0 clientif=5 10-06 23:23:22.648: d/beaconservice(9203): waiting stop scan 5000 milliseconds 10-06 23:23:22.648: d/beaconservice(9203): scan started 10-06 23:23:23.658: d/beaconservice(9203): waiting stop scan 3993 milliseconds 10-06 23:23:24.418: d/bluetoothadapter(9203): onscanresult() - device=78:a5:04:5b:1a:f9 rssi=-46 10-06 23:23:24.418: d/beaconservice(9203): got record 10-06 23:23:24.658: d/beaconservice(9203): waiting stop scan 2993 milliseconds 10-06 23:23:25.328: d/bluetoothadapter(9203): onscanresult() - device=78:a5:04:5b:1a:f9 rssi=-47 10-06 23:23:25.328: d/beaconservice(9203): got record 10-06 23:23:25.658: d/beaconservice(9203): waiting stop scan 1992 milliseconds 10-06 23:23:26.228: d/bluetoothadapter(9203): onscanresult() - device=78:a5:04:5b:1a:f9 rssi=-46 10-06 23:23:26.228: d/beaconservice(9203): got record 10-06 23:23:26.658: d/beaconservice(9203): waiting stop scan 991 milliseconds 10-06 23:23:27.648: d/beaconservice(9203): done scan cycle 10-06 23:23:27.648: d/bluetoothadapter(9203): stoplescan() 10-06 23:23:27.658: d/beaconservice(9203): restarting scan. unique beacons seen lastly cycle: 0 total beacon advertisement packets seen: 0 10-06 23:23:27.658: d/bluetoothadapter(9203): startlescan(): null 10-06 23:23:27.668: d/bluetoothadapter(9203): onclientregistered() - status=0 clientif=5 10-06 23:23:27.678: d/beaconservice(9203): waiting stop scan 5000 milliseconds 10-06 23:23:27.678: d/beaconservice(9203): scan started 10-06 23:23:28.678: d/beaconservice(9203): waiting stop scan 3999 milliseconds
------------- simpleservice code below -------------
public class simpleservice extends service implements beaconconsumer { protected static final string tag = "mybeaconservice"; private beaconmanager beaconmanager; rangingdata rangingdata = null; beacon beacon = null; int mapkey = 0; arraylist<string> sbeacons = new arraylist<string>(); hashtable<integer, string> source = new hashtable<integer,string>(); final hashmap<integer, string> map = new hashmap(source); final hashmap<integer, string> maptime = new hashmap(source); string[] parts = null; public class simpleservicebinder extends binder { public simpleservice getservice() { homecoming simpleservice.this; } } @override public ibinder onbind(intent arg0) { // todo auto-generated method stub homecoming null; } @override public void oncreate() { super.oncreate(); log.i(tag, "service created ..."); beaconmanager = beaconmanager.getinstanceforapplication(getbasecontext()); // beaconmanager = org.altbeacon.beacon.beaconmanager.getinstanceforapplication(this); // default androidbeaconlibrary find altbeacons. if wish create // find different type of beacon, must specify byte layout beacon's // advertisement line below. illustration shows how find beacon // same byte layout altbeacon beacontypecode of 0xaabb // // beaconmanager.getbeaconparsers().add(new beaconparser(). // setbeaconlayout("m:2-3=aabb,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25")); 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); beaconmanager.debug = true; beaconmanager.setforegroundbetweenscanperiod(1000); } @override public void onstart(intent intent, int startid) { log.i(tag,"onstart called"); beaconmanager.setbackgroundmode(false); } @override public int onstartcommand(intent intent, int flags, int startid) { log.i(tag,"onstartcommand called"); homecoming start_sticky; //start_redeliver_intent; } @override public void ondestroy() { super.ondestroy(); log.i(tag, "service destroyed ..."); //toast.maketext(this, "service destroyed ...", toast.length_long).show(); beaconmanager.unbind(this); } @override public void onbeaconserviceconnect() { log.i(tag, "<<< onbeaconserviceconnect >>>"); beaconmanager.setmonitornotifier(new monitornotifier() { @override public void didenterregion(region region) { log.i(tag, "onbeaconserviceconnect \ngetid1: "+region.getid1()+"\ngetid2: "+region.getid2()+"\ngetid3: "+region.getid3()); log.i(tag, "**************-------------****************"); logbeacondata(true); } @override public void didexitregion(region region) { //logtodisplay("exit part "+ region.getuniqueid(), true); log.i(tag, "********!!!!!!!!! didexitregion !!!!!!!!!!*******"); mapkey = 0; logbeacondata(false); seek { beaconmanager.stoprangingbeaconsinregion(new region("sbeacon", null, null, null)); } grab (remoteexception e) { e.printstacktrace();} iterator<integer> keysetiterator = map.keyset().iterator(); while(keysetiterator.hasnext()) { integer key = keysetiterator.next(); log.i(tag, "****delete key: " + mapkey + " value: " + map.get(key)); keysetiterator.remove(); maptime.remove(key); } printtoscreen(); } @override public void diddeterminestateforregion(int state, part region) { log.i(tag, "diddeterminestateforregion \ngetid1: "+region.getid1()+"\ngetid2: "+region.getid2()+"\ngetid3: "+region.getid3()); } }); seek { beaconmanager.startmonitoringbeaconsinregion(new region("sbeacon", null, null, null)); } grab (remoteexception e) { log.i(tag, "remoteexception: "+e); } } /** * * @param ibeacon */ private void logbeacondata(final boolean enter) { beaconmanager.setrangenotifier(new rangenotifier() { @override public void didrangebeaconsinregion(collection<beacon> beacons, part region) { if (beacons.size() > 0) { // sbeacons.clear(); beacon = beacons.iterator().next(); log.i(tag, " uuid: " + beacon.getid1()); log.i(tag, " major: " + beacon.getid2()); log.i(tag, " minor: " + beacon.getid3()); log.i(tag, " rssi: " + beacon.getrssi()); log.i(tag, " power: "+ beacon.gettxpower()); log.i(tag, " distance: "+ beacon.getdistance()); if (map.values().contains(beacon.getidentifiers().tostring())) { // log.i(tag, "<<< there >>> "+beacon.getidentifiers().tostring()); } else { mapkey = mapkey + 1; map.put(mapkey, beacon.getidentifiers().tostring()); maptime.put(mapkey, ""+system.currenttimemillis()); } iterator<integer> keysetiterator = map.keyset().iterator(); final int seconds = 10; while(keysetiterator.hasnext()) { integer key = keysetiterator.next(); if (map.get(key).contains(beacon.getidentifiers().tostring())) { maptime.put(key, ""+system.currenttimemillis()); } long diff = system.currenttimemillis() - (long.parselong(maptime.get(key))); int sec = (int) ((diff / 1000) % 60); log.i(tag, "key: " + key + " value: " + map.get(key)+" diff: "+second); if(second >= seconds ) { log.i(tag, "****delete key: " + mapkey + " value: " + map.get(key)+" diff: "+second); keysetiterator.remove(); maptime.remove(key); //logtodisplay("\n** beacon not longer reporting **"+ map.get(key), true); } printtoscreen(); } } } }); seek { beaconmanager.startrangingbeaconsinregion(new region("sbeacon", null, null, null)); log.i(tag, "*** startrangingbeaconsinregion ***"); } grab (remoteexception e) { log.i(tag, "remoteexception: "+e); } } public void printtoscreen() { iterator<integer> keysetiterator = map.keyset().iterator(); // logtodisplay("", false); while(keysetiterator.hasnext()) { integer key = keysetiterator.next(); log.i(tag, "---------------------------------------------------"); if (map.get(key) != null) { parts = map.get(key).tostring().substring(1, map.get(key).tostring().length()-1).split("\\,"); log.i(tag, "---------------------------------------------------"); log.i(tag, "uuid: " + parts[0]); log.i(tag, "major: " + parts[1]); log.i(tag, "minor: " + parts[2]); log.i(tag, "rssi: "+ beacon.getrssi()); log.i(tag, "power: "+ beacon.gettxpower()); log.i(tag, "distance: "+ beacon.getdistance()); } } } }
there should no problem doing want if set properly.
i suspect "ranging notifier null" because did not set ranging notifier in onbeaconserviceconnect() callback. if set ranging notifier in method, need post code can assist further.
android ibeacon
No comments:
Post a Comment