How to detect Region Enter/Exit for multiple beacons using AltBeacon android-beacon-library?

一笑奈何 提交于 2019-11-28 18:57:50

Two options:

  1. Set up a different region to match only each individual beacon, specifying all their identifiers, and monitor for each. You will get a different entry and exit callback for each region.

    Region region1 = new Region("myIdentifier1", Identifier.parse("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"), Identifier.parse("1"), Identifier.parse("1"));        
    Region region2 = new Region("myIdentifier2", Identifier.parse("2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"), Identifier.parse("1"), Identifier.parse("2"));   
    
    beaconManager.startMonitoringBeaconsInRegion(region1);
    beaconManager.startMonitoringBeaconsInRegion(region2);    
    
  2. Enable ranging, and put code in the didRangeBeaconsInRegion callback to track individual beacons. You can use a java.util.HashMap to keep track of all beacons that are visible (with a timestamp for the latest time each was seen), and then if you haven't seen a beacon in, say, five seconds, you can remove the beacon from the HashMap and execute your exit logic for that beacon.

Option 1 is great for a small number of beacons where you know their identifiers up front. Option 2 is more involved, but better for a large number of beacons or if you do not know their identifiers in advance.

Lalit kumar

/***************This Code for estimote Beacons *****************/

private  final Region ALL_ESTIMOTE_BEACONS_REGION = new Region("beaconall", null, null, null);

private BeaconManager beaconManager;

public  onCreate()
{

    beaconManager.connect(new BeaconManager.ServiceReadyCallback()
    {
        @Override
        public void onServiceReady()
        {
            Log.d("Lalit", "Beacon service Ready");

            beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
            beaconManager.startMonitoring(ALL_ESTIMOTE_BEACONS_REGION);
        }
    });
    beaconManager.setRangingListener(new BeaconManager.RangingListener() {
        @Override
        public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {

            int index = beacons.size();
            // UUID uuid = UUID.fromString("");
            if (beacons.size() > 0) {
                Beacon SelectedBeacon = beacons.get(index-1);

                Log.d("Lalit", "Beacon Id :- " + SelectedBeacon.getProximityUUID());
                Log.d("Lalit", "Beacon major :- " + SelectedBeacon.getMajor());
                Log.d("Lalit", "Beacon miner :- " + SelectedBeacon.getMinor());
                Log.d("Lalit", "Beacon total :- " + beacons.size());
                Log.d("Lalit","Distance :- "+ getDistance(SelectedBeacon.getRssi(),SelectedBeacon.getMeasuredPower()));
            }

        }
    });

    beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
        @Override
        public void onEnteredRegion(Region region, List<Beacon> list) {
            Calendar calendar = Calendar.getInstance();
            Date entertime = calendar.getTime();
            Log.d("Lalit", "Region Enter :- " + entertime);
            Log.d("List", "Region  UUID id :- " + region.getProximityUUID());
        }

        @Override
        public void onExitedRegion(Region region) {
            Calendar calendar = Calendar.getInstance();
            Date entertime = calendar.getTime();
            Log.d("Lalit", "Region exit :- " + entertime);
            Log.d("List", "Region  UUID id :- " + region.getProximityUUID());
        }
    });
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!