0

I'm using Android Beacon Library to detect beacon and start a simple application. Currently I'm able to start beacon service,which detects beacon after every 5 min(default time).But the thing is after every 5 mins my application just get started without giving me the notification in status bar of mobile.Required scenario - after detection, a notification should come up on screen and by clicking on that notification a activity should get start. Following is the class code, which contains a notification code.

BeaconReferenceApplication

public class BeaconReferenceApplication extends Application implements BootstrapNotifier {

private static final String TAG = "BeaconReferenceApp";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private MonitoringActivity monitoringActivity = null;


public void onCreate() {
    super.onCreate();
    BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);

    // By default the AndroidBeaconLibrary will only find AltBeacons.  If you wish to make it
    // find a different type of beacon, you must specify the byte layout for that beacon's
    // advertisement with a line like below.  The example shows how to find a beacon with the
    // same byte layout as AltBeacon but with a beaconTypeCode of 0xaabb.  To find the proper
    // layout expression for other beacon types, do a web search for "setBeaconLayout"
    // including the quotes.
    //
    //beaconManager.getBeaconParsers().clear();
    //beaconManager.getBeaconParsers().add(new BeaconParser().
    //        setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));

    beaconManager.setBackgroundScanPeriod(60000l);
    beaconManager.setBackgroundBetweenScanPeriod(60000l);
    try {
        beaconManager.updateScanPeriods();
    } catch (RemoteException e) {
        e.printStackTrace();
    }


    beaconManager.getBeaconParsers().
            add(new BeaconParser().
                    setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

    Log.d(TAG, "setting up background monitoring for beacons and power saving");
    // wake up the app when a beacon is seen
    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);

    // If you wish to test beacon detection in the Android Emulator, you can use code like this:
    // BeaconManager.setBeaconSimulator(new TimedBeaconSimulator() );
    // ((TimedBeaconSimulator) BeaconManager.getBeaconSimulator()).createTimedSimulatedBeacons();
}

@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");

        // The very first time since boot that we detect an beacon, we launch the
        // MainActivity
        Intent intent = new Intent(this, WebLayoutActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // Important:  make sure to add android:launchMode="singleInstance" in the manifest
        // to keep multiple copies of this activity from getting created if the user has
        // already manually launched the app.
        this.startActivity(intent);
        haveDetectedBeaconsSinceBoot = true;
    } else {
        if (monitoringActivity != null) {
            // If the Monitoring Activity is visible, we log info about the beacons we have
            // seen on its display
            monitoringActivity.logToDisplay("I see a beacon again" );
        } else {
            // If we have already seen beacons before, but the monitoring activity is not in
            // the foreground, we send a notification to the user on subsequent detections.
            Log.d(TAG, "Sending notification.");
            sendNotification();
        }
    }


}

@Override
public void didExitRegion(Region region) {
    if (monitoringActivity != null) {
        monitoringActivity.logToDisplay("I no longer see a beacon.");
    }
}

@Override
public void didDetermineStateForRegion(int state, Region region) {
    if (monitoringActivity != null) {
        monitoringActivity.logToDisplay("I have just switched from seeing/not seeing beacons: " + state);
    }
}

private void sendNotification() {
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setContentTitle("Beacon Reference Application")
                    .setContentText("Please click on this notification to access discount")
                    .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());
}

public void setMonitoringActivity(MonitoringActivity activity) {
    this.monitoringActivity = activity;
}

}

Can anyone tell me, what's wrong in this code? Thanks

cryptic
  • 168
  • 1
  • 10
  • Create Service... monitor beacon in your service. generate notification from service and click on notification open activity. – Ketan Parmar Mar 01 '16 at 12:18
  • Actually,there is inbuilt service support in this library and I'm able to achieve it. I just have problem with the notification functionality – cryptic Mar 01 '16 at 12:57
  • what do you see ""did enter region." in LogCat? If so, what comes after it? – davidgyoung Mar 02 '16 at 12:04
  • Yes.."did enter region" and then "auto launching mainactivity" for the first time. Rest "sending notification". Notification functionality is working now. Just want to reduce the backgroundScanningPeriod to one min. It is not happening currently. Thanks in advance – cryptic Mar 02 '16 at 14:14
  • I can also confirm that set BackgroundBetweenScanPeriod in the Application Class also doesnt seem to do anything i am running code the in a Nexus 5 with android 6.0 – Steve Apr 13 '16 at 14:07

0 Answers0