This is my first try to make an app with beacons. Im using the AltBeacon Library. All i want now is to be able to receive the UUID from a beacon. For this purpose i wanted to follow the Altbeacon example.
https://altbeacon.github.io/android-beacon-library/samples.html
So I did and this is my code.
public class MenuActivity extends AppCompatActivity implements View.OnClickListener, BeaconConsumer {
protected final String TAG = "BeaconSearch";
private BeaconManager beaconManager;
private Region region = new Region("myUniqueRegion", null,null,null);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=02150215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.bind(this);
Button scanButton = (Button) findViewById(R.id.scan_btn);
scanButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.scan_btn:
onBeaconServiceConnect();
break;
}
}
@Override
public void onDestroy(){
super.onDestroy();
beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
Log.i(TAG,"1");
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Log.i(TAG, "2");
if (beacons.size() > 0) {
Log.i(TAG, "Im Interested in this Beacon: " + beacons.iterator().next().getId1());
}
}
});
try {
Log.i(TAG,"3");
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) {
Log.i(TAG,"4");
}
}
}
So my understanding is that this should give me the UUID from a beacon if one is nearby. But I don't understand where I have to call the onBeaconServiceConnect() Method. I tried it in the OnCreate() Method as well via Button Click but either way the app crashes.
I really want to know what I'm doing wrong and what I'm not understanding here. Im thankfull for any help!