Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果未检测到,则从列表中删除信标_Java_Android_Ibeacon_Altbeacon - Fatal编程技术网

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

Java 如果未检测到,则从列表中删除信标,java,android,ibeacon,altbeacon,Java,Android,Ibeacon,Altbeacon,我正在尝试制作一个简单的应用程序,在ListView中列出所有找到的iBeacon,并根据用户与信标本身的距离更改RSSI值 该应用程序运行良好,但我遇到的问题是,如果信标无法触及,它不会从列表中删除。当信标不再在射程内时,有没有关于如何移除该物品的想法 我有以下代码: MainActivity.java: public class MainActivity extends Activity implements BeaconConsumer { public ListView li

我正在尝试制作一个简单的应用程序,在ListView中列出所有找到的iBeacon,并根据用户与信标本身的距离更改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);
        }
    }
}
public类MainActivity扩展活动实现BeaconConsumer{
公共列表视图列表;
公共信标适配器;
public ArrayList arrayL=新的ArrayList();
公共场所;充气机;
公共信标管理器;公共信标管理器;
公众出席;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.lijst);
mBeaconManager=BeaconManager.getInstanceForApplication(this.getApplicationContext());
mBeaconManager.getBeaconParsers().add(新的BeaconParser().setBeaconLayout(“s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19”);
mBeaconManager.SetForeGroundBetween扫描周期(100);
mBeaconManager.bind(这个);
适配器=新BeaconAdapter();
list.setAdapter(适配器);
充气器=(LayoutInflater)getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
beacerviceconnect()上的公共无效{
区域区域=新区域(“所有信标区域”,null,null,null);
试一试{
MBeaconsistenregion(区域)Startrangbeaconregion经理;
}捕获(远程异常){
e、 printStackTrace();
}
mBeaconManager.setRangeNotifier(新的RangeNotifier(){
@凌驾
公共无效范围信标区域(最终收集信标,区域){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
ArrayList allRangedBeacons=(ArrayList)信标;
ArrayList newRangedBeacons=新ArrayList();
ArrayList clonearaylistibeacon=(ArrayList)arrayL.clone();
ArrayListnonRangedBeacons=新的ArrayList();
int指数=0;
用于(信标显示信标:cloneArraylistIBeacon){
beaconPresent=false;
用于(信标eachRangedBeacon:allRangedBeacons){
if(当前信标等于(每个信标){
移除(索引);
arrayL.add(索引,eachRangedBeacon);
beaconPresent=true;
}
if(beaconPresent=false){
添加(当前信标);
}
}
索引++;
}
用于(信标eachRangedBeacon:allRangedBeacons){
beaconPresent=false;
用于(信标显示信标:cloneArraylistIBeacon){
if(eachRangedBeacon.equals(当前信标)){
beaconPresent=true;
}
}
如果(!beaconPresent){
添加(eachRangedBeacon);
}
}
arrayL.remove(非范围Beacons);
arrayL.addAll(newRangedBeacons);
adapter.notifyDataSetChanged();
}
});
}
});
}
受保护的void onPause(){
super.onPause();
mBeaconManager.unbind(此);
}
私有类BeaconAdapter扩展了BaseAdapter{
@凌驾
public int getCount(){
如果(arrayL!=null&&arrayL.size()>0){
返回arrayL.size();
}否则{
返回0;
}
}
@凌驾
公共信标获取项目(内部位置){
返回arrayL.get(位置);
}
@凌驾
公共长getItemId(int arg0){
返回arg0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
支架=新的视图支架(convertView=充气机。充气(R.layout.tupple_监控,null));
试一试{
holder.uuid.setText(“uuid:+arrayL.get(position.getId2());
holder.rssi.setText(“rssi:+arrayL.get(position.getRssi());
holder.txpow.setText(“txpow:+arrayL.get(position.gettpower());
返回视图;
}捕获(例外e){
e、 printStackTrace();
}
返回视图;
}
}
私有类视窗持有者{
私有文本视图uuid;
私有文本视图rssi;
私有文本视图txpow;
公共视图持有者(视图){
uuid=(TextView)view.findViewById(R.id.BEACON\u uuid);
rssi=(TextView)view.findViewById(R.id.BEACON\u rssi);
txpow=(TextView)view.findViewById(R.id.BEACON\u txpower);
view.setTag(this);
}
}
}

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

arrayL.clear();
arrayL.addAll(beacons);
adapter.notifyDataSetChanged();
为了避免在if列表项之间跳转,可以在显示它们之前通过它们的RSSI。

因为已跟踪可见信标列表并在测距回调中进行更新,您只需刷新整个列表即可
        @Override
        public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() { 
                    arrayL = new ArrayList<Beacon>(beacons);
                    adapter.notifyDataSetChanged();                   
                }
            });
        }