2

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.

  • What happens when you run your code? Where is the code where you start ranging and monitoring? Where do you set the values for `arraylistIBeacon`? – davidgyoung Dec 08 '14 at 12:44
  • You have to use `Custom Adapter`. When the beacons are discovered you add all beacons to the list(`yourList.addAll(beacons)`) and bind them in the custom adapter – hrskrs Jan 13 '15 at 14:45

0 Answers0