我试图在Android中创建一个简单的应用程序,通过使用AltBeacon库显示ListView中的所有信标。我遇到的问题是没有显示信标,我继续在没有任何信标的情况下获得空白屏幕。
任何想法我的代码可能出错? 这是我的代码:
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;
}
}
非常感谢任何帮助。