0
votes

I have an app that will detect Ibeacons using android beacon library

I am able to detect beacons in entry region .But the exit region code is not all working.

1.First i will detect all the beacons around my app 2.Will start monitoring each beacon

public void onBeaconServiceConnect() {
            // TODO Auto-generated method stub
            albeaconMnager.setRangeNotifier(new RangeNotifier() {

                @Override
                public void didRangeBeaconsInRegion(
                           Collection<org.altbeacon.beacon.Beacon> beacons,org.altbeacon.beacon.Region arg1) {
                           if(beacons.size()>0)
                           {
                              try {
                                       albeaconMnager.startMonitoringBeaconsInRegion(new  org.altbeacon.beacon.Region("Regionid", beacons.iterator().next().getId1(), beacons.iterator().next().getId2(), beacons.iterator().next().getId3()));
                        } catch (RemoteException e) {
                            // TODO Auto-generated catch bl
                           e.printStackTrace();
                        }


                    }

                    // TODO Auto-generated method stub

                }
            });

            albeaconMnager.setMonitorNotifier(new MonitorNotifier() {

                @Override
                public void didExitRegion(org.altbeacon.beacon.Region arg0)                  {
                    // TODO Auto-generated method stub
                    Log.i("tAG", "Exited Beacon!"+arg0.getUniqueId());   
                }

                @Override
                public void didEnterRegion(org.altbeacon.beacon.Region arg0) {
                    // TODO Auto-generated method stub
                                  connectToAppHandler(arg0.getId1().toString(),"entry");



                }

                @Override
                public void didDetermineStateForRegion(int state,
                        org.altbeacon.beacon.Region arg1) {
                    // TODO Auto-generated method stub

                    }
            });
            try {
                albeaconMnager.startRangingBeaconsInRegion(new org.altbeacon.beacon.Region("Regionid", null, null, null));
                //final Context context = this.getApplicationContext();

                } 
                catch (RemoteException e)
                    {  

                    }
            }
}

Please help

1

1 Answers

0
votes

There are a couple of problems in the code block below. The didRangeBeaconsInRegion method gets called approximately once per second when beacons are visible. Inside that method, a new Region is defined matching the first detected beacon, and monitoring is started on it.

Two problems:

  1. Monitoring is started once every second with the same region identifier (the first parameter to Region), effectively restarting monitoring on the region defined for that beacon. Because monitoring is restarted for a single region every second, it is impossible to have a region exit fire as long as any beacons are visible. Exiting a region happens when beacons within the defined region have not been seen for 10 seconds.

  2. The way the code block uses beacons.iterator.next() to access a beacon will get only the first beacon in the ranging callback. If multiple beacons are visible, this may change from one method callback to the next, causing non-deterministic results.

           @Override
            public void didRangeBeaconsInRegion(
                       Collection<org.altbeacon.beacon.Beacon> beacons,org.altbeacon.beacon.Region arg1) {
                       if(beacons.size()>0)
                       {
                          try {
                                   albeaconMnager.startMonitoringBeaconsInRegion(new  org.altbeacon.beacon.Region("Regionid", beacons.iterator().next().getId1(), beacons.iterator().next().getId2(), beacons.iterator().next().getId3()));
                    } catch (RemoteException e) {
                        // TODO Auto-generated catch bl
                       e.printStackTrace();
                    }
    
    
                }
    
                // TODO Auto-generated method stub
    
            }
        });
    

Rather than start monitoring inside the didRangeBeaconsInRegion callback, I suggest simply changing the code to start monitoring once at the same time ranging is started. Like this:

albeaconMnager.startRangingBeaconsInRegion(new org.altbeacon.beacon.Region("Regionid", null, null, null));
albeaconMnager.startMonitoringBeaconsInRegion(new org.altbeacon.beacon.Region("Regionid", null, null, null));