Search code examples
androidibeaconibeacon-androidestimoteandroid-ibeacon

How to get Unique Id of a Beacon using Estimote SDK in Android


I am developing an application in which I am using Beacons. I have gone through the related Estimote SDK for the same. Now the thing is that I need to know the Unique Id of a particular Beacon. Suppose I am having 3 Beacons and all are present in the range of a device. Now I want to perform different functions based on the Unique Id of the Beacons. For this I need to know the Unique Id of each and every Beacon. Currently I am giving UUID for a Beacon and just checking whether its in the region of the device or not. My code is as follows :-

private static final String ESTIMOTE_PROXIMITY_UUID = "XYZ";
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId",ESTIMOTE_PROXIMITY_UUID, null, null);

beaconManager.setMonitoringListener(new MonitoringListener() {

            @Override
            public void onExitedRegion(Region region) {
            //Region Exited
            }

            @Override
            public void onEnteredRegion(Region arg0, List<Beacon> arg1) {
                // TODO Auto-generated method stub
                //Do something ...
            }
        });

Now I need to get the Unique Id of the Beacon programmatically so that I can take decision depending on the IDs. Please help me to sort out the same. Any help would be appreciable. Thanks.


Solution

  • Every beacon has ID.
    ID is 20 bytes long and is divided into three sections:

    • proximityUUID (16 bytes)

    • major number (2 bytes)

    • minor number (2 bytes).

    How to use them to identify particular beacon?

    First, you have to be aware there can be a lot of beacons around you. These beacons might come from other vendors, companies or developers, so you want to filter them out.
    To filter beacons, you should use proximityUUID to group beacons provided by you or your company. Give the same proximityUUID to all your beacons.
    After that you can start monitoring and ranging by:

    private final Region MY_BEACONS = new Region("MY_BEACONS","YOUR_PROXIMITY_UUID", null, null);
    
    beaconManager.startMonitoring(MY_BEACONS);
    beaconManager.startRanging(MY_BEACONS);
    

    Now, in your listeners you will be notified when your beacons will be in neighborhood.

    Now let's assume you are writing an app for cinemas in the city. Each cinema has two different beacons to perform different actions. How to make a distinction between beacons for cinemas and how to do that for a particular beacon in a specyfic cinema?
    Use major and minor fields. For example:

    • use major for identifying all beacons in a particular cinema,
    • use minor for identifying specific beacon in a particular cinema.

    When you create ID for beacons in such way, you can get specific beacon and perform action:

     private final int SCARY_CINEMA_ID = 1111;
     private final int SCARY_CINEMA_BEACON_1 = 1;
     private final int SCARY_CINEMA_BEACON_2 = 2;
     private final int COMEDY_CINEMA_ID = 2222;
     private final int COMEDY_CINEMA_BEACON_1 = 1;
     private final int COMEDY_CINEMA_BEACON_2 = 2;
    
    
     @Override
     public void onEnteredRegion(Region region, List<Beacon> beacons) {
        for(Beacon beacon : beacons){
            if(beacon.getMajor() == SCARY_CINEMA_ID){
               if(beacon.getMinor == SCARY_CINEMA_BEACON_1){
                   //do something for this beacon
               } else{
                   //do something for second beacon in cinema
               }
            } else if(beacon.getMajor == COMEDY_CINEMA_ID){
              ...
            }
     }
    

    Hope, that will help you :)