This is the code that is supposed to background scan according to the sample code in the altbeacon page, but this does not happen I don't even get the Did enter region tag. RangingActivity, which implements BeaconConsumer ranges beacons without any issue. The only log i see is Background Scanning Initiated! when i launch the application for the first time. Thanks in advance.
public class BackgroundScanner extends Application implements BootstrapNotifier {
private String TAG="Background Scanner";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private RangingActivity rangingActivity;
@Override
public void onCreate() {
Log.d(TAG,"Background Scanning Initiated!");
super.onCreate();
BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT));
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
Region region = new Region("backgroundRegion",
null, null, null);
beaconManager.setBackgroundBetweenScanPeriod(60000L);
try {
beaconManager.updateScanPeriods();
} catch (RemoteException e) {
e.printStackTrace();
}
regionBootstrap = new RegionBootstrap(this, region);
backgroundPowerSaver = new BackgroundPowerSaver(this);
}
@Override
public void didEnterRegion(Region region) {
Log.d(TAG, "did enter region.");
sendNotification();
}
@SuppressLint("NewApi")
private void sendNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setContentTitle("Notification!")
.setContentText("Beacon Nearby!")
.setSmallIcon(R.mipmap.ic_launcher);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(new Intent(this, RangingActivity.class));
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
@Override
public void didExitRegion(Region region) {
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
}
The library may think it is already in the region. Starting with version 2.8, the library keeps track of region state across app restarts to prevent multiple didEnterRegion
callbacks. You can test this by turning off all beacons in the vicinity (or turning off Bluetooth) until you get a didExitRegion
callback. Then turn them back on and see if you get a didEnterRegion
callback.