Beacon only detected upon app restart

50 Views Asked by At

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

0

There are 0 best solutions below