Reputation: 11
Hi i am developing an android app using ALT beacon library.I need to display the detected beacons in a recycler view based on the distance.The nearby distance beacon need to display first in list.I had done evrything But i am getting blank page.Beacons are not displaying in recyclerview.Please help me.
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(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away."); } adapter.notifyDataSetChanged();
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
My adapter code:
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);
}
public static class Myviewholder extends 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);
}
}
}
Upvotes: 1
Views: 100
Reputation: 64916
A few tips:
Move your code that starts ranging to didDetermineStateForRegion
. If you are already "in region" when you start monitoring, you won't get a new callback to didEnterRegion
. You always get a callback to didDetermineStateForRegion
.
Add debug lines to the top of each callback method. Then run your code to determine which methods get called: onBeaconServiceConnected
, didEnterRegion
, didDetermineStateForRegion
, didRangeBeaconsInRegion
get called.
If one method in the chain of events does not get called, try to figure out why. Each time you get one callback working, repeat step 2 above. This is the tried and true means of debugging a program.
Upvotes: 0