I'm trying to make a simple app that lists all the found Ibeacons in a ListView and changes the RSSI values according to the distance the user is from the beacons itself.
The app works fine, but the problem I'm having is that if a beacon is out of reach it does not get removed from the list. Any ideas on how to remove the item when the beacon isn't in range anymore?
I have the following code:
MainActivity.java:
public class MainActivity extends Activity implements BeaconConsumer {
    public ListView list;
    public BeaconAdapter adapter;
    public ArrayList<Beacon> arrayL = new ArrayList<>();
    public LayoutInflater inflater;
    public BeaconManager mBeaconManager;
    public boolean beaconPresent;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView)findViewById(R.id.lijst);
        mBeaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
        mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
        mBeaconManager.setForegroundBetweenScanPeriod(100);
        mBeaconManager.bind(this);
        adapter = new BeaconAdapter();
        list.setAdapter(adapter);
        inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    public void onBeaconServiceConnect() {
        Region region = new Region("all-beacons-region", null, null, null);
        try {
            mBeaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        mBeaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<Beacon> allRangedBeacons = (ArrayList<Beacon>) beacons;
                        ArrayList<Beacon> newRangedBeacons = new ArrayList<>();
                        ArrayList<Beacon> cloneArraylistIBeacon = (ArrayList<Beacon>) arrayL.clone();
                        ArrayList<Beacon>nonRangedBeacons = new ArrayList<>();
                        int index = 0;
                        for (Beacon presentBeacons : cloneArraylistIBeacon) {
                             beaconPresent = false;
                            for (Beacon eachRangedBeacon : allRangedBeacons) {
                                if (presentBeacons.equals(eachRangedBeacon)) {
                                    arrayL.remove(index);
                                    arrayL.add(index, eachRangedBeacon);
                                  beaconPresent = true;
                                }
                                if(beaconPresent = false) {
                                    nonRangedBeacons.add(presentBeacons);
                                }
                            }
                            index++;
                        }
                        for (Beacon eachRangedBeacon : allRangedBeacons) {
                            beaconPresent = false;
                            for (Beacon presentBeacons : cloneArraylistIBeacon) {
                                if (eachRangedBeacon.equals(presentBeacons)) {
                                    beaconPresent = true;
                                }
                            }
                            if (!beaconPresent) {
                                newRangedBeacons.add(eachRangedBeacon);
                            }
                        }
                       arrayL.remove(nonRangedBeacons);
                        arrayL.addAll(newRangedBeacons);
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }
    protected void onPause() {
        super.onPause();
        mBeaconManager.unbind(this);
    }
    private class BeaconAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            if (arrayL != null && arrayL.size() > 0) {
                return arrayL.size();
            } else {
                return 0;
            }
        }
        @Override
        public Beacon getItem(int position) {
            return arrayL.get(position);
        }
        @Override
        public long getItemId(int arg0) {
            return arg0;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoring, null));
            try {
                    holder.uuid.setText("UUID: " + arrayL.get(position).getId2());
                    holder.rssi.setText("RSSI: " + arrayL.get(position).getRssi());
                    holder.txpow.setText("TXPOW: " + arrayL.get(position).getTxPower());
                return convertView;
            }catch(Exception e) {
                e.printStackTrace();
            }
        return convertView;
        }
    }
    private class ViewHolder {
        private TextView uuid;
        private TextView rssi;
        private TextView txpow;
        public ViewHolder(View view) {
            uuid = (TextView)view.findViewById(R.id.BEACON_uuid);
            rssi = (TextView)view.findViewById(R.id.BEACON_rssi);
            txpow = (TextView)view.findViewById(R.id.BEACON_txpower);
            view.setTag(this);
        }
    }
}
 
     
     
    