1

Making an application, where it scans in background only for one specific iBeacon. But in Activity I can change it to what ever I want it to scan for. I'm using the altbeacon library and the sample code. Already made it scan for a specific iBeacon, but I can't get it to change the Region.

One way I could think of, is, to get a Global static String. That way I can edit and beaconManager after bind will have the new Region. (How to create a global variable in android?) But I can only get.. not set.. , after I destroy Activity its empty.

Other way is to do it straight in the Activity, after click Save it just changes it. But that didn't seem to work.

iBeaconReader.java

public class iBeaconReader extends Application implements BootstrapNotifier {

private static final String TAG = "BeaconReferenceApp";
private BeaconManager beaconManager;
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;

public void onCreate() {
    super.onCreate();
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.setBackgroundMode(true);
    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(1000l);
    beaconManager.setBackgroundScanPeriod(1000L);
    beaconManager.bind(this);

    Identifier identifier = Identifier.parse("9B749762-760E-4AD8-A221-000000000000");   // I dont want to find this. 
    Region region = new Region("null", null, null, null);                               // But I have to set it up

    regionBootstrap = new RegionBootstrap(this, region);

    backgroundPowerSaver = new BackgroundPowerSaver(this);

}

@Override
public void didEnterRegion(Region region) {
    if(!region.getUniqueId().equals("null")) {
        Log.d(TAG, "In Range- " + region);
    }
}

@Override
public void didExitRegion(Region region) {
    if(!region.getUniqueId().equals("null")) {
        Log.d(TAG, "Out of Range- " + region);
    }
}

@Override
public void didDetermineStateForRegion(int i, Region region) {

}
}

MainActivity.java

public class MainActivity extends Activity implements BeaconConsumer {
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.bind(this);

...

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

...
@Override
public void onBeaconServiceConnect() {
    try {
        beaconManager.stopMonitoringBeaconsInRegion(new Region("backgroundRegion", null, null, null));
        Identifier identifier = Identifier.parse("9B749762-760E-4AD8-A221-91E0F210134C"); //mini beacon
        beaconManager.startMonitoringBeaconsInRegion(new Region("backgroundRegion", identifier, null, null));
    } catch (RemoteException e) {
        Log.e("BeaconReferenceApp", e.getMessage(), e);
    }
}

I got it working. When the onBeaconServiceConnect() function gets called I have to do beaconManager.stopMonitoringBeaconsInRegion(new Region("backgroundRegion", null, null, null)). When I didn't stopMonitoring, it didn't change the monitoring region.

Community
  • 1
  • 1
paakjis
  • 369
  • 4
  • 16

0 Answers0