0

I'm developing an application where users will check-in to the location only when the mobile app is opened. I followed the sample code at https://altbeacon.github.io/android-beacon-library/samples.html, the beacon managed to be scanned.. only when the user clicks the back button on the MainActivity and opens the app again. The app is not able to detect the beacon despite being very close to the device without restarting.

This is my MainActivity code:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, BeaconConsumer {

    BeaconManager beaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // some code //
        beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.bind(this);
    }

    public void onBeaconServiceConnect() {
        final String module_code = "FI 3011_ITO P02"; // TODO: replace with latest lecture
        final UUID uuid = UUID.nameUUIDFromBytes(module_code.getBytes());

        beaconManager.addRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {

                for (Beacon beacon : collection) {
                    // Do something 
                }
            }
        });

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

            Region region = new Region(uuid.toString(), Identifier.fromUuid(uuid), Identifier.fromInt(1), Identifier.fromInt(2));
            beaconManager.startRangingBeaconsInRegion(region);


        } catch (RemoteException e) {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (beaconManager.isBound(this)) beaconManager.unbind(this);
    }
}

Can somebody please advice me on how should I go about resolving this problem?

Best regards

dot
  • 11
  • 3
  • If you add a log line inside the didRangeBeaconsInRegion method, does it show up one a second? You should see it both with the activity in the foreground and after the activity is no longer in the foreground. Do you see this? – davidgyoung Feb 04 '18 at 16:17
  • @davidgyoung yes, the log showed up one per second. There is one after the activity is not in the foreground. – dot Feb 04 '18 at 17:55
  • So this is good, yes? So if you get these callbacks every second, then what is the problem? Perhaps you can show the logs for your problem case to explain what you see. – davidgyoung Feb 05 '18 at 00:32
  • There isn't a log to show this problem but for some reason the beacon collection in didRangeBeaconsInRegion is always size 0 despite being right beside the beacon. I double checked the UUID and identifiers with the beacon. The only way it seem to be able to find the beacon would be to rebind when the activity returns to the foreground. – dot Feb 05 '18 at 04:45
  • So you are saying the count of detected beacons > 0 when the activity is in the foreground, yes? Exactly when does the count change to 0? – davidgyoung Feb 05 '18 at 12:59

0 Answers0