Search code examples
ibeaconibeacon-androidaltbeacon

How to use ranging in didRangeBeaconsInRegion


-------EDIT 2--------

Still using the post of Davidgyoung and these comments, now I have a FatalException :

E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[BeaconIntentProcessor] Process: databerries.beaconapp, PID: 19180 java.lang.NullPointerException at databerries.beaconapp.MyApplicationName.didEnterRegion(MyApplicationName.java:76) at org.altbeacon.beacon.BeaconIntentProcessor.onHandleIntent(BeaconIntentProcessor.java:83) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.os.HandlerThread.run(HandlerThread.java:61)

This error is due to the calling of setRangeNotifier()?

-------EDIT--------

After the post of Davidgyoung and these comments, I tried this method, but still not working :

public class MyApplicationName extends Application implements BootstrapNotifier {
private static final String TAG = ".MyApplicationName";
private RegionBootstrap regionBootstrap;
private BeaconManager beaconManager;

List region_list = new ArrayList();

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "App started up");
    // wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
    List region_list = myRegionList();
    regionBootstrap = new RegionBootstrap(this, region_list);

    BeaconManager 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.setBackgroundScanPeriod(3000l);
    beaconManager.setBackgroundBetweenScanPeriod(5000l);
}

@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
    // Don't care
}

@Override
public void didEnterRegion(Region region) {
    Log.d(TAG, "Got a didEnterRegion call");
    // This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
    // if you want the Activity to launch every single time beacons come into view, remove this call.
    regionBootstrap.disable();
    Intent intent = new Intent(this, MyActivity.class);
    // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
    // created when a user launches the activity manually and it gets launched from here.
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(intent);

    String zone = region.toString();
    Log.d(TAG, "Enter in region");
    String text = "Enter in " + zone;
    Log.d(TAG, text);
    String uuid = "UUID : " + region.getId1();
    Log.d(TAG, uuid);
    //This part is not working
    beaconManager.setRangeNotifier(this);
    beaconManager.startRangingBeaconsInRegion(region);
}

@Override
public void didExitRegion(Region arg0) {
    // Don't care
}

The errors are about the input in setRangeNotifier and an exception for startRangingBeaconsInRegion

This isn't my main class, my main class :

public class MyActivity extends Activity{
public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.d("myActivity","onCreate");

}

}

I want all the IDs of the beacons in the region. With this method if I understand, the app is wake-up in the background when she detected a region and normally the "startRangingBeaconsInRegion" can give me a list of beacons with this one I can take the Ids.

-------Original--------

I would like to know all the beacon around me. I know the UUID of this beacons and I can get it with 'region.toString();'. But, I need the others id of the beacons. And, I don't have "Beacon" on didRangeBeaconsInRegion.

How to know the beacons in the region?

And last question, it's possible to make that in the background?

Thanks


Solution

  • You can see an example of ranging for beacons in the "Ranging Sample Code" section here: http://altbeacon.github.io/android-beacon-library/samples.html

    This will allow you to read all the identifiers by looking at each Beacon object returned in the Collection<Beacon> beacons in the callback. Like this:

            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                for (Beacon beacon: beacons) {
                    Log.i(TAG, "This beacon has identifiers:"+beacon.getId1()+", "+beacon.getId2()+", "+beacon.getId3());        
                }
            }
    

    Once you start ranging, it will continue to do so in the background, provided you don't exit the activity that starts the ranging. Under some uses of the library, ranging slows down in the background, but this only happens if using the BackgroundPowerSaver class. If you don't want ranging to slow down in the background, simply don't enable background power saving with the library.