question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incremental hits on didEnterRegion when app closed then re-opened

See original GitHub issue

Expected behavior

Open app and start monitoring, it should detect a beacon and didEnterRegion should hit once. (I am using only 1 beacon)

Actual behavior

Open app monitoring starts, didEnterRegion hits 1 time on first run but after unbind and destroyed activity then open app again didEnterRegion hits 2 times. (repeat same process then hits goes on increasing from 1, 2 , 3 ,4 , … )

Steps to reproduce this behavior

  • Open app monitoring starts, didEnterRegion hits 1 time on first run
  • Close the app (unbind and destroyed activity)
  • Open app again didEnterRegion now hits 2 times. (repeat same process then hits goes on increasing from 1, 2 , 3 ,4 , … )

Mobile device model and OS version

  • android 9.0 (Oneplus 6)

Android Beacon Library version

  • 2.15.4 (also could reproduce on 2.15.2)

My code is attached and below are onCreate() and onDestroy() code snippets:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "OnCreate() called.");
        setContentView(R.layout.activity_main);
        beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

 
        if (!beaconManager.isBound(this)) {
            Notification.Builder builder = new Notification.Builder(MainActivity.this);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle("Scanning for Beacons");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(TAG,
                        "BeaconPOC", NotificationManager.IMPORTANCE_HIGH);
                channel.setDescription("BeaconPOC");
                NotificationManager notificationManager = (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
                notificationManager.createNotificationChannel(channel);

                PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                        new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

                builder.setContentIntent(contentIntent);
                builder.setChannelId(channel.getId());
            }
            beaconManager.enableForegroundServiceScanning(builder.build(), FOREGROUND_NOTIFICATION_ID);
            beaconManager.setEnableScheduledScanJobs(false);
            beaconManager.setRegionStatePersistenceEnabled(false);
            beaconManager.bind(this);
            Log.i(TAG, "Beacon service just got bound");
            backgroundPowerSaver = new BackgroundPowerSaver(this);
        }
  }

  @Override
    protected void onDestroy() {
        super.onDestroy();
        stopRangingAndMonitoring();
        beaconManager.unbind(this);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(10001);
        beaconManager.disableForegroundServiceScanning();
    }
    
    @Override
    public void onBeaconServiceConnect() {
        Log.i(TAG, "onBeaconServiceConnect() : Connected.");
        beaconManager.addRangeNotifier(new RangeNotifier() {
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                for (Beacon beacon : beacons) {
                    if (beacon.getDistance() < 5.0) {

                        try {
                            beaconManager.stopRangingBeaconsInRegion(region);

                            BeaconInfo beaconInfo = new BeaconInfo(beacon.getId1().toString(), Integer.parseInt(beacon.getId2().toString()), Integer.parseInt(beacon.getId3().toString()), beacon.getDistance());
                            beaconInfoList.add(beaconInfo);

                            mAdapter.notifyDataSetChanged();

                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });


        beaconManager.addMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region region) {
                Log.i(TAG, "I just saw an beacon for the first time!");
                try {
                    Log.i(TAG, "Started Ranging");
                    beaconManager.startRangingBeaconsInRegion(new Region("uniqueId1", null, null, null));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

                Toast.makeText(MainActivity.this, "I see a Beacon entered.", Toast.LENGTH_LONG).show();

            }

            @Override
            public void didExitRegion(Region region) {
                Log.i(TAG, "I no longer see an beacon");
                Toast.makeText(MainActivity.this, "I see a Beacon exited.", Toast.LENGTH_LONG).show();

            }

            @Override
            public void didDetermineStateForRegion(int state, Region region) {

            }
        });

        try {
            stopRangingAndMonitoring();
            Log.i(TAG, "Started Monitoring.");
            beaconManager.startMonitoringBeaconsInRegion(new Region("uniqueId2", null, null, null));
        } catch (RemoteException e) {
            Log.i(TAG, e.toString());
        }
    }

    private void stopRangingAndMonitoring() {
        for (Region region : beaconManager.getMonitoredRegions()) {
            try {
                beaconManager.stopMonitoringBeaconsInRegion(region);
                Log.i(TAG, "Stop monitoring: " + region.toString());
            } catch (RemoteException e) {
            }
        }
        for (Region region : beaconManager.getRangedRegions()) {
            try {
                beaconManager.stopRangingBeaconsInRegion(region);
                Log.i(TAG, "Stop ranging: " + region.toString());
            } catch (RemoteException e) {
            }
        }
    }

MainActivity.txt

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
davidgyoungcommented, Jan 10, 2019

Can you please show stopRangingAndMonitoring and where you start ranging and monitoring?

0reactions
davidgyoungcommented, Jan 14, 2019

Glad you found a fix that worked for you.

The rangeNotifiers and monitorNotifiers on BeaconManager are contained in a Set, so they will not allow duplicates. However, if you end up with two instances of your MainActivity (quite possible in Android depending on how you set up your AndroidManifest.xml or use Intents to start your activity), then you might end up with multiple notifiers. Clearing the notifiers is always the safest bet to avoid duplicates.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get geofencing didEnterRegion delegate method call ...
Your app will be reopened (into the background) to handle the update. ... If your application is not running, then it can't get...
Read more >
iOS didEnterRegion and instantiati… | Apple Developer Forums
My understanding is that when an app enters the background, the app will run for a period of time and eventually will be...
Read more >
iOS Programming: The Big Nerd Ranch Guide
App Store, Apple, Bonjour, Cocoa, Cocoa Touch, Finder, Instruments, ... Open Xcode and, from the File menu, select New and then New Project....
Read more >
Jonathon Manning, Paris Buttield-Addison & Tim Nugent
Later, Apple largely consolidated them into one application, known as Xcode, though some of the applications (such as Instruments and the iOS.
Read more >
Learn Cocoa Touch - Springer Link
home. and later in grade school my dad bought me a copy of Visual Studio-the first app I ever wrote for someone else...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found