I have used the basic sample application provided by Altbeacon
(Radio Network
) on the Github repo.
Everything seems to work for my iBeacon right from Monitoring
to the Ranging
but only in Foreground
I have done these following steps to get it working in Foreground.
public class BeaconReferenceApplication extends Application implements BootstrapNotifier, RangeNotifier
{
private static final String TAG = "BeaconReferenceApplication";
private BeaconManager mBeaconManager;
private Region mAllBeaconsRegion;
private BackgroundPowerSaver mBackgroundPowerSaver;
private RegionBootstrap mRegionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
@Override
public void onCreate()
{
mAllBeaconsRegion = new Region(getPackageName(), null, null, null);
mBeaconManager = BeaconManager.getInstanceForApplication(this);
mBackgroundPowerSaver = new BackgroundPowerSaver(this);
mRegionBootstrap = new RegionBootstrap(this, mAllBeaconsRegion);
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
backgroundPowerSaver = new BackgroundPowerSaver(this);
}
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1)
{
System.out.println("app-> didRangeBeaconsInRegion");
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1)
{
System.out.println("app-> didDetermineStateForRegion");
}
@Override
public void didEnterRegion(Region arg0)
{
System.out.println("app-> didEnterRegion");
try
{
Log.d(TAG, "entered region. starting ranging");
mBeaconManager.setRangeNotifier(this);
mBeaconManager.startRangingBeaconsInRegion(mAllBeaconsRegion);
}
catch(RemoteException e)
{
Log.e(TAG, "Cannot start ranging");
}
Intent intentMainActivity = new Intent(this,MonitoringAndRangingActivity.class);
intentMainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentMainActivity);
}
@Override
public void didExitRegion(Region arg0)
{
System.out.println("app-> didExitRegion");
}
public void setMonitoringAndRangingActivity(MonitoringAndRangingActivity activity)
{
mBeaconManager.setBackgroundMode((activity==null)?true:false);
}
}
Edit:
I have added the method mBeaconManager.setBackgroundMode(true);
but no help, i just get the stopLE scan() message from BlutoothAdapter
and everything stops, do i need to implement this in a Service or anything else?
But as soon as I go to background(pressing back or home) the logs and the callback methods
stop responding.
Is there anything that i am missing to activate the background iBeacon detection.
Any inputs would be like a boon.
If you are using the BackgroundPowerSaver
it will transition the Android Beacon Library into background mode automatically. There is no need to call setBackgroundMode
manually.
When the library is in background mode, it only does a scan every 5 minutes to save power (this timing is configurable) so it may take up to five minutes to detect a beacon. I would watch LogCat for five minutes while your aspp is in the background and see if you get any detections in that time. If not, adding a LogCat excerpt to your question would be helpful.