0

I'm trying to make a simple application in Android that displays all the beacons that are found in a ListView by using the AltBeacon library. The problem that I'm having is that no beacon is shown, I keep on getting a blank screen without any beacons.

Any ideas what could be wrong with my code? This is the code I have:

MainActivity.java:

   public class MainActivity extends Activity implements BeaconConsumer, RangeNotifier {

    public BeaconManager mBeaconManager;
    public List<Beacon> beaconlijst;
    ListView lijst;
    public BeaconListAdapter listAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        beaconlijst = new ArrayList<>();

        lijst = (ListView)findViewById(R.id.listViews);
        listAdapter = new BeaconListAdapter(this, beaconlijst);
        lijst.setAdapter(listAdapter);
        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.bind(this);

    }
    @Override
    public void onBeaconServiceConnect() {
        Region region = new Region("all-beacons-region", null, null, null);
        try {
            mBeaconManager.startRangingBeaconsInRegion(region);
        }catch(RemoteException e) {
            e.printStackTrace();
        }
        mBeaconManager.setRangeNotifier(this);
    }

    @Override
    public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    beaconlijst = new ArrayList<>(beacons);
                    listAdapter.notifyDataSetChanged();
                }

            });


    }

    public void onPause() {
        super.onPause();
        mBeaconManager.unbind(this);
    }
}

BeaconListAdapter.java:

public class BeaconListAdapter extends BaseAdapter {
    private Activity activity;
    private List<Beacon> beacons;
    private static LayoutInflater inflater = null;

    public BeaconListAdapter(Activity _activity, List<Beacon> _beacons) {
        this.activity = _activity;
        this.beacons = _beacons;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return beacons.size();
    }

    @Override
    public Object getItem(int position) {
        return beacons.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;

        if(convertView == null ) {
            view = inflater.inflate(R.layout.tupple_monitoring, null);
        }

        TextView uuid = (TextView)view.findViewById(R.id.BEACON_uuid);
        TextView rssi = (TextView)view.findViewById(R.id.BEACON_rssi);
        TextView txpower = (TextView)view.findViewById(R.id.BEACON_txpower);

        Beacon beacon = beacons.get(position);

        if(beacon != null) {
            uuid.setText(beacon.getId2().toString());
            rssi.setText(beacon.getRssi());
            txpower.setText(beacon.getTxPower());
        }
        return view;
    }
}

Any help would greatly be appreciated.

brasay
  • 125
  • 1
  • 2
  • 14

0 Answers0