0

Hello I just started with estimote beacons and trying to build a sample application for finding the beacons range and to send notifications to mobile devices when the beacons are near to the range.

Manasa
  • 49
  • 1
  • 4

2 Answers2

3

This is how I usually set up my beacons. First you will want a service class:

public class BeaconServicing extends Service {

private static Context context;
private static final Region ALL_BEACONS = new Region(BeaconConfig.GLOBAL_REGION_ID, BeaconConfig.UUID, BeaconUtils.ALL_MAJOR, BeaconUtils.ALL_MINOR);
private static BeaconManager beaconManager;

public static BeaconManager getBeaconManager() {
    if (beaconManager == null) {
        beaconManager = new BeaconManager(context);
    }
    return sBeaconManager;
}

@Override
public void onCreate() {
    super.onCreate();
    context = this;
    startBeaconing();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void startBeaconing() {
    if (BeaconUtils.isCapable()) {
        beaconManager = getBeaconManager();
        beaconManager.setErrorListener(new BeaconError());
        beaconManager.setMonitoringListener(new BeaconMonitoring());
        beaconManager.setRangingListener(new BeaconRanging());
        beaconManager.setBackgroundScanPeriod(BeaconConfig.BACKGROUND_SCAN_PERIOD, BeaconConfig.BACKGROUND_WAIT_PERIOD);
        beaconManager.setForegroundScanPeriod(BeaconConfig.FOREGROUND_SCAN_PERIOD, BeaconConfig.FOREGROUND_WAIT_PERIOD);
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                beaconManager.startMonitoring(ALL_BEACONS);
            }
        });
    }
}
}

That is what you will call to start the beacon service. From here you will want your monitoring class:

public class BeaconMonitoring implements BeaconManager.MonitoringListener {

private static final String TAG = BeaconMonitoring.class.getSimpleName();

@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
    Log.d(TAG, "Entering region: " + region.getIdentifier());
    BeaconServicing.getBeaconManager().startRanging(region);
    // send notification that you have entered range of beacon
}

@Override
public void onExitedRegion(Region region) {
    Log.d(TAG, "Exiting region: " + region.getIdentifier());
    BeaconServicing.getBeaconManager().stopRanging(region);
}
}

That is what will monitor when you enter and exit a region. From those methods you can do whatever you need to do. Most of the other classes simply implement the appropriate listener and override the methods, like this:

public class BeaconRanging implements BeaconManager.RangingListener {

@Override
public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
}
}

Then my beacon config classes contains the scan periods and ids that I need:

public class BeaconConfig {

public static final long FOREGROUND_SCAN_PERIOD = 1000;
public static final long FOREGROUND_WAIT_PERIOD = 0;

public static final long BACKGROUND_SCAN_PERIOD = 5000;
public static final long BACKGROUND_WAIT_PERIOD = 5000;

public static final String GLOBAL_REGION_ID = App.getContext().getPackageName();
public static final String UUID = "your uuid";
}

Then to start it, you just do:

startService(new Intent(this, BeaconServicing.class));

and you are ready to go. This should be enough to at least start you in the right direction.

Zamereon
  • 376
  • 1
  • 2
  • 17
1

I recommend reading:

Ziem
  • 6,579
  • 8
  • 53
  • 86