嗨,我正在使用ALT信标库开发一个Android应用程序。我需要基于距离在回收站视图中显示检测到的信标。附近的距离信标需要在列表中首先显示。我已经做了evrything但我得到了空白页。信标未显示在recyclerview中。请帮助我。
ArrayList<Beacon> data;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
recyclerView = findViewById(R.id.Recyclerview_tab);
bt_stop = findViewById(R.id.bt_stop);
bt_start = findViewById(R.id.bt_start);
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
beaconManager.setBackgroundBetweenScanPeriod(30000l);
beaconManager.setForegroundBetweenScanPeriod(10000l);
region = new Region("myMonitoringUniqueId", null, null, null);
adapter = new Beaconadapter(this); recyclerView.setAdapter(adapter);
bt_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
beaconManager.bind(Main2Activity.this);
Toast.makeText(Main2Activity.this, "clicked", Toast.LENGTH_SHORT).show();
}
});
bt_stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
beaconManager.unbind(Main2Activity.this);
}
});
}
@Override
public void onBeaconServiceConnect() {
final Region region = new Region("myBeaons", Identifier.parse("e2c56db5-dffb-48d2-b060-d0f5a71096e0"), null, null);
beaconManager.addMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
try {
Log.d(TAG, "didEnterRegion");
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didExitRegion(Region region) {
try {
Log.d(TAG, "didExitRegion");
beaconManager.stopRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
}
});
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
adapter.initAll(beacons);
Log.i(标记,“我看到的第一个信标是关于“ + beacons.iterator()。next()。getDistance()+”米之遥。“); } adapter.notifyDataSetChanged();
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
我的适配器代码:
public class Beaconadapter extends RecyclerView.Adapter<Beaconadapter.Myviewholder> {
Context mcontext;
public static ArrayList<Beacon> beacons;
private LayoutInflater inflater;
public Beaconadapter(Context mcontext) {
this.mcontext = mcontext;
this.beacons = new ArrayList<Beacon>();
this.inflater = LayoutInflater.from(mcontext);
}
@NonNull
@Override
public Myviewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v= LayoutInflater.from(mcontext).inflate(R.layout.beaconrecyclerview,viewGroup,false);
Myviewholder myviewholder=new Myviewholder(v);
return myviewholder; }
@Override
public void onBindViewHolder(@NonNull Myviewholder myviewholder, int i) {
myviewholder.tv_uuid.setText(beacons.get(i).getServiceUuid());
myviewholder.tv_macid2.setText(beacons.get(i).getBluetoothAddress());
myviewholder.tv_rssi2.setText((int) beacons.get(i).getDistance());
}
@Override
public int getItemCount() {
return beacons.size();
}
public void initAll(Collection newBeacons){
this.beacons.clear();
this.beacons.addAll(newBeacons);
}
公共静态类Myviewholder扩展了RecyclerView.ViewHolder { TextView tv_uuid,tv_macid2,tv_rssi2;
public Myviewholder(@NonNull View itemView) {
super(itemView);
this.tv_uuid=itemView.findViewById(R.id.tv_uuid);
this.tv_macid2=itemView.findViewById(R.id.tv_macid2);
this.tv_rssi2=itemView.findViewById(R.id.tv_rssi2);
}
}
}
答案 0 :(得分:0)
一些提示:
移动您的代码,其范围从didDetermineStateForRegion
开始。如果开始监视时已经“处于区域内”,则不会收到新的didEnterRegion
回调。您总是会收到对didDetermineStateForRegion
的回调。
在每个回调方法的顶部添加调试行。然后运行您的代码以确定哪些方法被调用:onBeaconServiceConnected
,didEnterRegion
,didDetermineStateForRegion
,didRangeBeaconsInRegion
被调用。
如果未调用事件链中的一个方法,请尝试找出原因。每次您执行一个回调时,请重复上述步骤2。这是调试程序的可靠方法。