Search code examples
androidibeaconibeacon-android

Android - How to display properly a list of beacons?


I am trying to display beacons and their respective values on a listview. List is displayed but 2 instances of a beacon are displayed; for instance

UUID: e2c56db5-dffb-48d2-b060-d0f5a71096e0 MAJOR: 2 MINOR: 236 RSSI: -59 TX: -59 DISTANCE: 0.9449177660225923

Is displayed twice in the list view:

UUID: e2c56db5-dffb-48d2-b060-d0f5a71096e0 MAJOR: 2 MINOR: 236 RSSI: -59 TX: -59 DISTANCE: 0.9449177660225923

UUID: e2c56db5-dffb-48d2-b060-d0f5a71096e0 MAJOR: 2 MINOR: 236 RSSI: -59 TX: -59 DISTANCE: 0.9449177660225923

The code that I am using is the following:

beaconManager.addRangeNotifier(new RangeNotifier() {

        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

            if (beacons.size() > 0) {
                data.clear();
                for (Beacon beacon : beacons) {
                    data.add("UUID: " + beacon.getId1()
                            + "\nMAJOR: " + beacon.getId2()
                            + "\nMINOR: " + beacon.getId3()
                            + "\nRSSI: " + beacon.getRssi()
                            + "\nTX: " + beacon.getTxPower()
                            + "\nDISTANCE: " + beacon.getDistance());
                }
                updateList();
            }

        }

    });

updateList()

public void updateList(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                 adapter.add(data);

            }
});

listView

List<String> data = new ArrayList();    
listView = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();

Solution

  • I have used below method because beacons are detected continuously, it worked for me. One suggestion: avoid the use of ListView.

    for (Beacon beacon : beacons) {
        if (!beaconsSeen.contains(beacon)) {
            beaconsSeen.add(beacon);
            //TODO update ListView/RecyclerView here
        }
    }
    beacons.clear();
    beacons.addAll(rangedBeacons);
    Collections.sort(beacons, new BeaconComparator());