List the detected beacons in listview in android

743 views Asked by At

I am currently working on Android beacon application.I just want to list out the detected beacons. When user clicks the button, ListView should appear with the list of detected beacons. It may be a minor problem but it's giving me a real trouble. Below are my code for the ranging and monitoring notification:

@Override
public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

                    ArrayList<Beacon> allRangedBeacons = (ArrayList<Beacon>) beacons;
                    ArrayList<Beacon> newRangedBeacons = new ArrayList<Beacon>();
                    ArrayList<Beacon> cloneArraylistIBeacon = (ArrayList<Beacon>) arraylistIBeacon.clone();

                    int index = 0;
                    for (Beacon presentBeacons : cloneArraylistIBeacon) {
                        boolean beaconPresent = false;
                        for (Beacon eachRangedBeacon : allRangedBeacons) {

                            if (presentBeacons.equals(eachRangedBeacon)) {
                                arraylistIBeacon.remove(index);
                                arraylistIBeacon.add(index, eachRangedBeacon);
                                // Toast.makeText(MainActivity.this,"U detected a beacon",
                                // Toast.LENGTH_LONG).show();
                                beaconPresent = true;
                                break;
                            }

                        }

                        index++;
                    }

                    for (Beacon eachRangedBeacon : allRangedBeacons) {
                        boolean beaconPresent = false;
                        for (Beacon presentBeacons : cloneArraylistIBeacon) {
                            if (eachRangedBeacon.equals(presentBeacons)) {
                                beaconPresent = true;

                                break;
                            }
                        }
                        if (!beaconPresent) {
                            newRangedBeacons.add(eachRangedBeacon);
                        }
                    }

                    // arraylistIBeacon.remove(nonRangedBeacons);
                    arraylistIBeacon.addAll(newRangedBeacons);
                    info.notifyDataSetChanged();

                }
            });
        }
    });


    beaconManager.setMonitorNotifier(new MonitorNotifier() {

        @Override
        public void didExitRegion(Region region) {

            ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
            toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);                 
            Toast.makeText(getApplicationContext(), "u just went out of the region - Monitor mode", Toast.LENGTH_LONG).show();

            }

        @Override
        public void didEnterRegion(Region region) {

            ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
            toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200); 
            Toast.makeText(getApplicationContext(), "u just enterd the region - Monitor mode", Toast.LENGTH_LONG).show();

        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "u just enterd the didDetermineStateForRegion - Monitor mode", Toast.LENGTH_LONG).show();
        }
    });

}

Any type of help will be appreciated.

0

There are 0 answers