I would like the simplest program to add in my app. The program should only take beacon information after a dialog box option is clicked, I tried many times to write the code but didn't get what are the minimum steps required to start ranging.
public class SecondClass extends Activity implements BeaconConsumer {
private BeaconManager beaconManager;
protected static final String TAG = "RangingActivity";
RegionBootstrap regionBootstrap;
Region region;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
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"));
beaconManager.bind(this);
region = new Region("backgroundRegion", null, null, null);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(101); //closes notification
//opens the alert dialog
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(this);
}
builder.setTitle("Send data")
.setMessage("Are you sure you want to send your data to the server?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// if yes was pressed, send the data
try {
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
Log.d(TAG, "Can't start ranging");
}
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// if cancel was pressed, do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
@Override
public void onBeaconServiceConnect() {
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i(TAG, "The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
}
}
});
}
}
Can anyone explain how the onBeaconServiceConnect runs? And do I need before to start ranging to do the monitoring process, check if there is a beacon and if there is a beacon to start ranging or is okay just to have a program only ranging?
I tried to do as described in Android Beacon Library sample code section, but didn't work
Best regards
EDIT
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Added these permissions to the code, but still not work
Another thing I did was to check if ACCESS_COARSE_LOCATION was enabled by using the function described in here (2nd top most voted answer)