2

I am trying to detect beacon devices using my device, and so monitoring them and also listening to the range notifications.

I am using this library and the library-reference app. I managed to listen to a custom beacon using the set beacon layout method.

Step1. I set the layout in the application class Step 2. I mke the baseactivity implement beaconconsume where it performs ranging.

When starting the range monitoring service/method we use "myRangeUniqueId" but the didexit and didenter methods use "backgroundId" I think. Why is that so?

So the situation is this I move the beacon device some few meters away , I still get I see a beacon notification and I do not see a beacon.. These messages keep changing alternatively even when the beacon is far.

Do I have to do anything to prevent this? Please help.

the code snippets include below:

  1. The application class implements BootstrapNotifier and the code is as under

    BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("LAYOUT_HERE"));
    
    Region region = new Region("backgroundRegion",
            null, null, null);
    regionBootstrap = new RegionBootstrap(this, region);
    
    // simply constructing this class and holding a reference to it in your custom Application
    // class will automatically cause the BeaconLibrary to save battery whenever the application
    // is not visible.  This reduces bluetooth power usage by about 60%
    backgroundPowerSaver = new BackgroundPowerSaver(this);
    
    
    
     @Override
     public void didEnterRegion(Region arg0) {
          // In this example, this class sends a notification to the user whenever a Beacon
         // matching a Region (defined above) are first seen.
        Log.d(TAG, "did enter region.");
        if (!haveDetectedBeaconsSinceBoot) {
            Log.d(TAG, "auto launching MainActivity");
            haveDetectedBeaconsSinceBoot = true;
        } else {
            // i am not sending out any notification here since there could be multiple beacons and i need to identify any one of them with a specific uuid
        }
    
    
    }
    
    
    @Override
    public void didExitRegion(Region region) {
        sendNotification("exited",2);
    }
    

i do nothing in didDetermineStateForRegion method,its just overridden

  1. I have a BaseActivity that implements BeaconConsumer

        private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
       @Override
       protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            beaconManager.bind(this);
       }
    
        @Override 
        protected void onDestroy() {
            super.onDestroy();
            beaconManager.unbind(this);
         }
        @Override 
        protected void onPause() {
             super.onPause();
                  if(beaconManager.isBound(this))beaconManager.setBackgroundMode(true);
             }
     @Override 
     protected void onResume() {
         super.onResume();
         if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(false);
      }
    
       @Override
       public void onBeaconServiceConnect() {
          beaconManager.setRangeNotifier(new RangeNotifier() {
             @Override 
             public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                if (beacons.size() > 0) {
                /*EditText editText = (EditText)RangingActivity.this
                        .findViewById(R.id.rangingText);
                Beacon firstBeacon = beacons.iterator().next();
                logToDisplay("The first beacon "+firstBeacon.toString()+" is about "+firstBeacon.getDistance()+" meters away.");    */
        CommonUtilities.sendNotification(BaseActivity.this,"entered",1);
                        }
                    }
                }
            }
        }
    });
    
    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId",null, null, null));
        } catch (RemoteException e) {   }
      }
    

P.S: When i move the device to a different location say some 5 meters away, i get random notification, that the beacon is in range and then immediately i get a notification that the beacon is out of range.

Thanks

1 Answers1

1

A few points:

  1. The unique identifier is used as a key to identify a Region so you can start and stop ranging and monitoring. Each region you construct and register with the system should have a different string identifier, but the value can be whatever you wasn't so long as it is unique.

  2. The didRangeBeaconsInRegion callback is made every second, and not just when a mobile device enters range of the beacon.

  3. If you are getting repeated didExitRegion callbacks when the beacon is within 20 meters or so, you may have a beacon that is not transmitting frequently enough. Knowing the beacon make, model and transmission frequency as well as your mobile device model might help solve this.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • I changed the new Region string to `new Region("keyhere",Identifier.parse(uuidhere)),null,null);` . Yes the beacon is within 20 meters of range, and so to prevent the entry and exit calls frequently, i changed the timeout in MonitorState to 20000l instead of 10000l, which i know is not appropriate, and the scan freq to 2000l as per [this](https://github.com/AltBeacon/android-beacon-library/issues/27) – Rat-a-tat-a-tat Ratatouille Mar 12 '15 at 11:50
  • We are using april beacon, i am afraid i donot know the transmission freq :(. – Rat-a-tat-a-tat Ratatouille Mar 12 '15 at 11:52
  • In case the April beacon is transmitting at < 1Hz, you can try reducing the scan frequency to see if the didExitRegion calls stop. The following code reduces the scan frequency in the foreground to 30 seconds from the default of 1.1 seconds: `beaconManager.setForegroundScanPeriod(30000l);` – davidgyoung Mar 12 '15 at 16:13
  • hi sir, i reduced the scan frequency to 2000, and i have tested it, if at all the exit call comes in i could try 30 seconds too. But will this affect how beacons are detected? – Rat-a-tat-a-tat Ratatouille Mar 13 '15 at 04:31
  • but i have even increased the timeout period from 10s to 20s. I am doing it the correct way? – Rat-a-tat-a-tat Ratatouille Mar 13 '15 at 04:56
  • Yes, this will affect the frequency of the ranging callbacks if you set it successfully. Make sure you but this line before you set up your RegionBootstrap. – davidgyoung Mar 13 '15 at 12:01
  • sir, yes, thats where i put in the scanning time (foreground)... i am marking this as the ans :). And +1 for all your time.. If there is anything, i will get back to you.. – Rat-a-tat-a-tat Ratatouille Mar 17 '15 at 10:39