Search code examples
androidibeaconibeacon-androidaltbeaconandroid-ibeacon

Android AltBeacon ranging


I'd like to scan any kind of beacon around me. Basically if I understand correctly you'd need to know their id before you scan them? How would you scan any beacon around you and extract their UUID/Factory Id/etc.?

Here is my current code:

public class TestActivity extends Activity implements BeaconConsumer {

private BeaconManager beaconManager;

public static final String TAG = "TestActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startBeaconScan();
}

public void startBeaconScan() {
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.setDebug(true);
    beaconManager.bind(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.setMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(final Region region) {
            Log.d(TAG, "ENTERED REGION: "+region);
        }

        @Override
        public void didExitRegion(Region region) {}

        @Override
        public void didDetermineStateForRegion(int i, Region region) {}
    });
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
            Log.d(TAG, "BEACONS: "+collection.size());
        }
    });
}

@Override
public void onBeaconServiceConnect() {
    try {
        beaconManager.startMonitoringBeaconsInRegion(new Region("myRegion", null, null, null));
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

}

I get these messages in the output, but nothing else:

beacon detected : id1: f7826da5-4fa1-4e48-8024-bc5b70e0891a id2: 49089 id3: 34388

So the question is, how can I detect these beacons without knowing the ID beforehand?


Solution

  • Two points:

    1. Creating a Region definition with all null identifiers is essentially a wildcard that will match all beacon identifiers so you do not need to know them up front. The code shown already has this.

    2. To get a list of beacons detected, you need to start ranging. The code shown has a raging callback, but it does not start ranging.

    After this line:

    beaconManager.startRangingBeaconsInRegion(new Region("myRegion", null, null, null));
    

    Add this:

    beaconManager.startMonitoringBeaconsInRegion(new Region("myRegion", null, null, null));