kolo
kolo

Reputation: 85

Sorting array by beacon streight

I have few beacons for fun. I want list them on view by strength rssi. So basically they will change place on list depend on distance receiver from beacon in a period time for example every 10 sec.. What's the better way, use sqllite and save address beacon and straight rssi, then sort them, or do this in some array? Is there some way to sort items in array, in my example addresses of beacons depend on rssi?

Upvotes: 0

Views: 484

Answers (2)

davidgyoung
davidgyoung

Reputation: 64916

Using the Android Beacon Library, you can sort the ranged beacons by RSSI with code like this:

public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
    ArrayList<Beacon> sortedBeacons = new ArrayList<Beacon>(beacons);
    Collections.sort(sortedBeacons, new Comparator<Beacon>() {

        @Override
        public int compare(Beacon beacon0, Beacon beacon1) {
            return new Integer(beacon0.getRssi()).compareTo(new Integer(beacon1.getRssi()));
        }

    });

    // Add code here to update display with sortedBeacons
}

Upvotes: 2

Victory
Victory

Reputation: 5890

Use an array as the data is pretty short lived so the database overhead is not worth it. Then use Collections.sort(List, Comparator) where the Comparator compares the single strengths. To run every 10 seconds use Handler's postDelayed.

Upvotes: 0

Related Questions