Search code examples
androidibeaconestimote

how to set different ids for different estimote beacons?


Suppose, Android app is detecting 3 beacons with in range with default UUID value. So, how can i set/change the name for each beacons in Anndroid (to identify each beacon)?

Appreciations for any suggestions... Thanks


Solution

  • First of all you need estimote sdk Then you can create a beaconDetection service something like this

    public class BeaconMyService extends Service
    {
        private static final String TAG = "BeaconMyService";
    
        private final Handler handler = new Handler();
    
        private BeaconManager beaconManager;
    
        private static final Region ALL_ESTIMOTE_BEACONS_REGION = new Region("rid", null, null, null);
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId)
        {
            if (beaconManager.isBluetoothEnabled())
            {
    
                connectToService();
            }
            return Service.START_NOT_STICKY;
    
        }
    
        private void connectToService()
        {
            beaconManager.connect(new BeaconManager.ServiceReadyCallback()
            {
                @Override
                public void onServiceReady()
                {
                    try
                    {
                        beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
                    }
                    catch (RemoteException e)
                    {
                        Log.e("Myservice", "Cannot start ranging, something terrible happened");
                        Log.e("", "Cannot start ranging", e);
                    }
                }
            });
        }
    
    
        @Override
        public void onCreate()
        {
            super.onCreate();
    
            beaconManager = new BeaconManager(this);
            beaconManager.setRangingListener(new BeaconManager.RangingListener()
            {
    
                @Override
                public void onBeaconsDiscovered(Region region, final List<Beacon> beacons)
                {
    
                    // Note that beacons reported here are already sorted by
                    // estimated
                    // distance between device and beacon.
                    for (int index = 0; index < beacons.size(); index++)
                    {
                        Beacon beacon = beacons.get(index);
    
                        if (beacon != null)
                        {
                            Log.v("Beacon MacAddress", beacon.getMacAddress() + "");
                            if (!Constants.BEACONSDETECTEDLIST.containsKey(beacon.getMacAddress()))
                            {//Constants.BEACONSDETECTEDLIST is your list of beacon mac addresses
                        //public static HashMap<String, Long> BEACONSDETECTEDLIST = new HashMap<String, Long>(); 
                            //to check if beacon is detected for the first time
    
                                if (Constants.BEACON1.equalsIgnoreCase(beacon.getMacAddress()))
                                {//Constants.BEACON1 is mac address of beacon 1 assigned in constants
                                    Constants.BEACONSDETECTEDLIST.put(beacon.getMacAddress(), System.currentTimeMillis());
                                    handler.postDelayed(beacon1Detection, 2000);
                                }
                                else if (Constants.BEACON2.equalsIgnoreCase(beacon.getMacAddress()))
                                {
                                    Constants.BEACONSDETECTEDLIST.put(beacon.getMacAddress(), System.currentTimeMillis());
                                    handler.postDelayed(beacon2Detection, 2000);
                                }
                            }
                            else
                            {/*Do Nothing*/
                            }
    
                        }
                    }
    
                }
            });
    
        }
    
        private Runnable beacon1Detection = new Runnable()
        {
            public void run()
            {
    
                beacon1Info();
            }
        };
    
        private Runnable beacon2Detection = new Runnable()
        {
            public void run()
            {
    
                beacon2Info();
            }
        };
    
        private void beacon1Info()
        {
            Intent intent = new Intent(Constants.BEACON1BROADCAST_ACTION);
            sendBroadcast(intent);
        }//in Constants 
        //public static final String BEACON1BROADCAST_ACTION = "beacon1Action";
    
    
    
        private void beacon2Info()
        {
            Intent intent = new Intent(Constants.BEACON2BROADCAST_ACTION);
            sendBroadcast(intent);
        }// in Constants
        //public static final String BEACON2BROADCAST_ACTION = "beacon2Action";
    
        @Override
        public IBinder onBind(Intent intent)
        {
            return null;
        }
    
        @Override
        public void onDestroy()
        {
            handler.removeCallbacks(beacon1Detection);
            handler.removeCallbacks(beacon2Detection);
            super.onDestroy();
        }
    }
    

    And then finally you need a BeaconBroadcastReceiver to receive the broadcasts in the service and open respective Activity

    public class BeaconBroadCastReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Constants.BEACON1BROADCAST_ACTION)) {
                    Intent intent2 = new Intent(context, Beacon1Layout.class);
                    context.startActivity(intent2);
    
                } else if (intent.getAction().equals(Constants.BEACON2BROADCAST_ACTION)) {
                    Intent intent2 = new Intent(context, Beacon2Layout.class);
                    context.startActivity(intent2);
    
                }
            }
    
        }
    

    Hope this will help you, Good luck :)