java - Tunring my android to beacon and detecting -
is right way of doing? using samples https://altbeacon.github.io/android-beacon-library/samples.html
public class app extends application implements bootstrapnotifier, beaconconsumer, rangenotifier { private final string tag = "application "; protected static final region beaconregion = new region("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6", null, null, null); protected beaconmanager beaconmanager = null; private regionbootstrap regionbootstrap; private backgroundpowersaver backgroundpowersaver; protected static string slog = ""; @override public void oncreate() { super.oncreate(); logit(tag, beaconregion.getid1()+"oncreate - in"+beaconregion.getuniqueid()); beaconmanager = org.altbeacon.beacon.beaconmanager.getinstanceforapplication(this); beaconmanager.getbeaconparsers().clear(); beaconmanager.getbeaconparsers().add(new beaconparser(). setbeaconlayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));//ibeacon (tutti) //--- wake app when beacon seen regionbootstrap = new regionbootstrap(this, beaconregion); //--- activate power saver backgroundpowersaver = new backgroundpowersaver(this); beaconmanager.bind(this); logit(tag, "oncreate - out"); } private void logit (string tag, string msg) { slog += tag + msg + "\n"; log.w(tag, msg); } //-------------------------// //--- bootstrapnotifier ---// //-------------------------// @override public void diddeterminestateforregion(int state, region region) { string msg = "diddeterminestateforregion "; switch(state) { case monitornotifier.inside: msg +="(inside)"; break; case monitornotifier.outside: msg +="(outside)"; break; default: msg +="(state=" +state +")"; break; } logit(tag, msg); } @override public void didenterregion(region arg0) { logit(tag, "didenterregion - in"); try { beaconmanager.startrangingbeaconsinregion(beaconregion); logit(tag,"der - startrangingbeaconsinregion ok"); } catch (remoteexception e) { logit(tag, "der - startrangingbeaconsinregion err " +e); } logit(tag, "didenterregion - out"); } @override public void didexitregion(region region) { logit(tag, "didexitregion - in"); try { beaconmanager.stoprangingbeaconsinregion(beaconregion); logit(tag,"dxr - stoprangingbeaconsinregion ok"); } catch (remoteexception e) { logit(tag, "dxr - stoprangingbeaconsinregion err " +e); } logit(tag, "didexitregion - out"); } //----------------------// //--- beaconconsumer ---// //----------------------// @override public void onbeaconserviceconnect() { logit(tag, "onbeaconserviceconnect - in"); beaconmanager.setrangenotifier(this); logit(tag, "onbeaconserviceconnect - out"); } //---------------------// //--- rangenotifier ---// //---------------------// @override public void didrangebeaconsinregion(collection<beacon> beacons, region region) { logit(tag, "didrangebeaconsinregion - " +beacons.size() +" beacons"); toast.maketext(getapplicationcontext(), beaconregion.getid1()+" beacon detected "+beacons.size(), toast.length_short).show(); for(beacon beac: beacons) { system.out.println(beac.getid1()+"id 1"+tag); if(beac.getid1().equals("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6")/send notification } } }
so class, extends application listening beacons around. below, how turning phone beacon.i doing inside activity on click of button. there 2 phones app downloaded on both, once clicks on 1 app button want other phone detect since have implemented in extends application class.
the turn ur android beacon code.
beacon beacon = new beacon.builder() .setid1("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6") // uuid beacon .setid2("1") // major beacon .setid3("5") // minor beacon .setmanufacturer(0x004c) // radius networks.0x0118 change other beacon layouts//0x004c iphone .settxpower(-56) // power in db .setdatafields(arrays.aslist(new long[]{0l})) // remove beacon layouts without d: fields .build(); beaconparser beaconparser = new beaconparser() .setbeaconlayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"); beacontransmitter = new beacontransmitter(menuactivity.this, beaconparser); beacontransmitter.startadvertising(beacon, new advertisecallback() { @override public void onstartfailure(int errorcode) { log.e("tag", "advertisement start failed code: " + errorcode); } @override public void onstartsuccess(advertisesettings settingsineffect) { log.i("tag", "advertisement start succeeded."); toast.maketext(menuactivity.this, "advertisement start succeeded", toast.length_short).show(); system.out.println("startedddddddddddd"); } }); // beacontransmitter.stopadvertising(); } catch(exception o) { system.out.println("affda "+o.getmessage()); }
i havig issue, didenterregion , didrangebeaconsinregion fired many times, sending many notification user multiple times. not user friendly.
the didrangebeaconsinregion
callback supposed called many times -- that's how ranging works. called approximately once per second when beacon detected tell there , give distance estimate.
if want fire notification once first time call method, can set flag specific beacon.
here example of code might use that:
// add top of class private hashmap<string,boolean> malreadysentnotification = new hashmap<string,boolean>(); ... // add inside of didrangebeaconsinregion method if (malreadysentnotification.get(beacon.tostring())) { malreadysentnotification.put(beacon.tostring(), true); // send notification here. }
Comments
Post a Comment