Piodo Piodo - 4 days ago 5
Android Question

Refresh distance to selected iBeacon device using Altbeacon library on Android

I want to display current distance computed by getDistance() function from AltBeacon library on selected beacons.

For example I have 3 beacons in range and 2 of them are saved in SavedBeacons - this is collection of beacons object. There are beacons that User has selected from

public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region)
function.

I would like to have displayed them(Only the selected ones) with auto 1 second update on thier current distance.

Now The output is just the beacons information and the distance saved at the time I've added it to SavedBeacons Collection. Its distance isnt updating.

Here's my class with activity to show selected beacons:

public class SelectBeacon extends Activity {

private ListView listview;
private AdapterBeaconDevices bAdapter;
private TextView noBeaconsFound;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_beacon);

this.bAdapter = new AdapterBeaconDevices(this, SavedBeacons);

listview = ((ListView) findViewById(R.id.listView2));
listview.setAdapter(bAdapter);


noBeaconsFound = (TextView) findViewById(R.id.empty2);
SetEmptyButtonVisiblity(SavedBeacons.size());


listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//nothing yet
}
});

refresh();
}

private void refresh(){

final Handler handler = new Handler();
handler.postDelayed( new Runnable() {

@Override
public void run() {

Toast.makeText(SelectBeacon.this, "test_refresh_msg",
Toast.LENGTH_LONG).show();

bAdapter.notifyDataSetChanged();

handler.postDelayed( this, 1000 );
}
}, 1000 );
}

private void SetEmptyButtonVisiblity(final int a)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
if (a == 0) {
noBeaconsFound.setVisibility(View.VISIBLE);
} else {
noBeaconsFound.setVisibility(View.GONE);
}
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
}

@Override
protected void onPause() {
super.onPause();
}

@Override
protected void onResume() {
super.onResume();
}
}

Answer

I've solved this problem by defining unique range containing specific ids to selected beacon

beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", id1, id2, id3));

While working with multiple beacons, I scan for all beacons then just print only those whose ids are the same as the beacons in SavedList.

Maybe it will help someone.

Comments