0
votes

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();
3

3 Answers

1
votes

Your problem is that every time a beacon is found, you are adding it to the list, without clearing it. I guess you should implement some sort of verification before adding the new element to the list, or just remove all the elements there, like:

@Override
public void run() {
    adapter.clear();
    // element or whatever
    adapter.add(element);
}
1
votes

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());
0
votes

Maybe it's happening because you don't clear the data inside the adapter before adding new items

public void updateList(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                 adapter.clear(); //this should wipe out all existing adapter data
                 adapter.add(data);

            }
});