0

I have 2 Rad beacons from the Radius Network. I have configured them to Eddystone with the Locate application. Now I have written a small program to send notifications in the background i.e. When the app is not running. I need to send notifications when the app is in the background. I am using the Android beacon library to achieve this. I have tried almost all the links but I am not able to detect it.

I am pasting my code here

public class BeaconReferenceApplication extends Application implements BootstrapNotifier, RangeNotifier {
    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);

        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.setBackgroundBetweenScanPeriod(1000);

        Log.i(TAG, "setting up background monitoring for beacons and power saving");

        //Toast.makeText(getApplicationContext(), "Called!!!" , Toast.LENGTH_LONG).show();

        // 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.i(TAG, "did enter region.");

        //sendNotification();

        if (!haveDetectedBeaconsSinceBoot) {
            Log.i(TAG, "auto launching MainActivity");

            // The very first time since boot that we detect an beacon, we launch the
            // MainActivity
            Intent intent = new Intent(this, MonitoringActivity.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.i(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("An beacon is nearby in application.")
                        .setSmallIcon(R.drawable.ic_launcher);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(new Intent(this, MonitoringActivity.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) {
        Log.i("Log", "TEST ONLY");

        this.monitoringActivity = activity;
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) {
        // TODO Auto-generated method stub

        sendNotification();

        //Log.i("Log", "TEST ONLY");
    }
Furkan Varol
  • 252
  • 2
  • 8
Sangie
  • 49
  • 8
  • it's easier for people to help you if they have some relevant **piece of code** to work on - please show us what you have tried so far – Bö macht Blau Nov 30 '15 at 18:17
  • I updated my code. Please let me know if anything can be done. – Sangie Dec 02 '15 at 04:23
  • I don't have experience with beacons (yet), sorry. But I can see that you use NotificationCompat.Builder on the one hand and NotificationManager (not NotificationManagerCompat) on the other. Does that work? – Bö macht Blau Dec 02 '15 at 06:50

2 Answers2

0

From the layout that you are using, I can say that you are scanning for AltBeacon not Eddystone. So you should change (or add) the beacon layout with following;

s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19

or

beaconManager.getBeaconParsers().add(BeaconParser.EDDYSTONE_UID_LAYOUT);

Also, you can check following links for more info about using AltBeacon Library for Eddystone Beacons;

By the way, you can setup Locate Application to detect only Eddystone Beacons so that you can narrow down problem and see that whether your code is not working or your beacon.

Furkan Varol
  • 252
  • 2
  • 8
  • No.. Still the same. I am not being notified in the background. – Sangie Dec 05 '15 at 10:05
  • According to your code, if you can detect beacons when app is in foreground, you should have detect them in background too. Your code seems like okay. However, Manifest files can cause this error. Can you share it? – Furkan Varol Dec 07 '15 at 12:57
0

I got same issue ,Spend more than one day to resolve this.there is no problem with the code.

(Just made a silly mistake ,as a developer).

This will use full to some one..

In my testing device i did not enable the location services.so can not able to receive beacon in background mode..so enable location services to get beacon notification on background , on other side foreground does not require this settings..

Thanks..

abdul sathar
  • 2,395
  • 2
  • 28
  • 38