在recyclerview中显示检测到的信标

时间:2016-09-06 07:43:39

标签: android altbeacon beacon

我需要在recyclerview中列出检测到的信标。到目前为止,我可以列出每个信标,但同一个信标将出现多次。我需要帮助。

这是我的代码

beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
                Log.i(TAG,"didRangeBeaconsInRegion, number of beacons detected = "+beacons.size());

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (Beacon beacon : beacons) {
                           DiscoveredDevices devices = new DiscoveredDevices(beacon.getId1().toString(),beacon.getId2().toString(),beacon.getId3().toString(),Integer.toString(beacon.getRssi()),beacon.getBluetoothAddress(),Double.toString(beacon.getDistance()));
                            Beacon.setHardwareEqualityEnforced(true);
                           devicesList.add(devices);

                        }
                        mAdapter.notifyDataSetChanged();
                    }

                });

           }
        });

修改

这是适配器类

public class DevicesAdapter extends RecyclerView.Adapter<DevicesAdapter.MyViewHolder> {

    private List<DiscoveredDevices> devicesList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView uuid_lbl, major_lbl, minor_lbl,ble_address_lbl,rssi_lbl;

        public MyViewHolder(View view) {
            super(view);
            uuid_lbl = (TextView) view.findViewById(R.id.uuid_lbl);
            major_lbl = (TextView) view.findViewById(R.id.major_lbl);
            minor_lbl = (TextView) view.findViewById(R.id.minor_lbl);
            ble_address_lbl = (TextView) view.findViewById(R.id.ble_address_lbl);
            rssi_lbl = (TextView) view.findViewById(R.id.rssi_lbl);
        }
    }


    public DevicesAdapter(List<DiscoveredDevices> devicesList) {
        this.devicesList = devicesList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.devices_list, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        DiscoveredDevices devices = devicesList.get(position);
        holder.uuid_lbl.setText(devices.getUuid());
        holder.major_lbl.setText(devices.getMajor());
        holder.minor_lbl.setText(devices.getMinor());
        holder.ble_address_lbl.setText(devices.getBle_address());
        holder.rssi_lbl.setText(devices.getRssi());
    }

    @Override
    public int getItemCount() {
        return devicesList.size();
    }
}

谢谢!

0 个答案:

没有答案