如果未检测到,则从列表中删除信标

时间:2015-09-23 12:43:45

标签: java android ibeacon altbeacon

我试图创建一个简单的应用程序,列出ListView中所有找到的Ibeacons,并根据用户与信标本身的距离更改RSSI值。

该应用程序工作正常,但我遇到的问题是,如果信标不可及,则不会从列表中删除。关于如何在信标不在范围内时删除项目的任何想法?

我有以下代码:

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);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

如果您只想在范围内显示信标,则每次收到信标列表时,只需更改适配器源列表即可。

arrayL.clear();
arrayL.addAll(beacons);
adapter.notifyDataSetChanged();

为了避免在列表项目中跳转,可能在显示它们之前由其RSSI sort the beacons

答案 1 :(得分:0)

因为Android Beacon Library已经跟踪了可见信标列表并在测距回调中更新了它,所以每次只需刷新BeaconAdapter中的整个列表即可。像这样:

        @Override
        public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() { 
                    arrayL = new ArrayList<Beacon>(beacons);
                    adapter.notifyDataSetChanged();                   
                }
            });
        }