AltBeacon Android Beacon Library region id is null - android

I'm using AltBeacon android library to detect Eddystone beacons on my app.
I'm setting the region like this:
Region region = new Region("backgroundRegion", null, null, null);
Whenever my app detects a beacon in range, I'm logging it like this:
#Override public void didEnterRegion(Region region) {
Log.v(TAG, "didEnterRegion() called with: region = [" + region.toString() + "]");
}
id1, id2 and id3 return null.
Instead, if I do this:
Region region = new Region("backgroundRegion", Identifier.parse("0x0b85497366bad1356d69"), null, null);
id1 returns `b85497366bad1356d69``
I want to detect all Eddystone devices. Can I get the device identifier if I do not set the region for that identifier?

The Ranging object in the callback for didEnterRegion is simply a copy of the one you used to start monitoring, so the identifiers will be all null if the initial Region has identifiers all null.
The solution? Use startRangingBeaconsInRegion() and the didRangeBeaconsInRegion callback. That callback will give you a list of detected beacons matching the region, so you can read all the actual identifiers.

Related

How to make didRangeBeaconsInRegion call once for all the uuid

My Problem
I need to make the combined call for all the region on didRangeBeaconsInRegion right now for each uuid eg: if i have 2 uuid the didRangeBeaconsInRegion is called 2 times and for 4 uuid didRangeBeaconsInRegion called 4 times likewise it goes on. I want didRangeBeaconsInRegion to be called once for all the uuid that i set on region
My Code:
for (beaconparser.Beacon beacon : iBeacon.getBeacons()) {
try {
Identifier uuid = Identifier.parse(iBeacon.getUUID());
Identifier major = Identifier.parse("" + iBeacon.getMajor());
Identifier minor = Identifier.parse("" + iBeacon.getMinor());
beaconManager.startRangingBeaconsInRegion(new Region(iBeacon.getUUID() + major + minor, uuid, null, null));
setMonitoring(false);
} catch (RemoteException e) {
e.printStackTrace();
}
}
Sorry, the API just does not work like that. The easiest solution is to set up a wildcard Region that sets all identifiers to null. This will give you one callback for all beacons.
You can the use if statements to compare the beacon identifiers to ones you care about and only process them if they match one of your regions.

How to increase the delay in the data of the region notifier?

I have using Android Beacon Library for one of my beacon solution .Is there a way put a delay in data produced by the range notifier because it is giving data very frequently .
Below is the code which I am referring to. To see complete code you can go to this thread "Android iBeacon App not working in the background"
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if(beacons.size() > 0) {
for (Beacon beacon : beacons) {
if (beacon.getDistance() < 1.0) {
Log.d(TAG, "I see a beacon transmitting a : " +
" approximately " + beacon.getDistance() + " meters away.");
Log.d(TAG, "BEACON DATA : " +beacon.getBluetoothAddress()+":"+beacon.getBluetoothName()+":"+beacon.getId1());
showNotification("Treewalker","You are near beacon range");
Intent intent = new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
}
}
}
There are two methods which can help you
mBeaconManager.setBackgroundBetweenScanPeriod(10000);
mBeaconManager.setForegroundBetweenScanPeriod(10000);
Check official javadocs for details

Store the scanned value in array and find the largest value in Android

In my Beacon application I am getting different values of RSSI through beacon.getRSSI(). Now I want to get the max value of RSSI of all the scanned beacons. So I thought the possible solution will be array. Can anyone helpout how to do this?
Put all the Beacon objects into a Collection (List, Set), then use Collections.max to get the beacon with the highest RSSI. javadoc
List<Beacon> beacons = new ArrayList<Beacon>();
// add all the beacons
beacons.add(beacon);
(...)
Beacon maxBeacon = Collections.max(beacons, new Comparator<Beacon>(){
public int compare(Beacon b1, Beacon b2) {
return Integer.compare(b1.getRSSI(), b2.getRSSI);
}
}

How to detect multiple beacons using Altbeacon android library?

I am using the AltBeacon sample app on my android device - the sample app provided by altbeacon.org is here: https://github.com/AltBeacon/android-beacon-library-reference
However, the app when launched is detecting and displaying only one beacon. I have about 5 beacons near my Android device. How do I detect all the beacons?
In RangingActivity.java, I noticed this method that is being called when a beacon comes in sight:
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
EditText editText = (EditText) RangingActivity.this.findViewById(R.id.rangingText);
Beacon firstBeacon = beacons.iterator().next();
logToDisplay("The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
}
}
}
I modified the iterator to read from the collection in a while loop as follows:
Beacon firstBeacon;
while(beacons.iterator().hasNext()){
firstBeacon = beacons.iterator().next();
logToDisplay("The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
}
However, the app crashes with this modification.
My questions:
(1) How can I display all beacons that are near my Android device?
(2) How can I detect beacons that go out of region?
For 1. I think you need to use a For loop. Like this.
for (Beacon beacon : beacons) {
logToDisplay("The beacon " + beacon.toString() + " is about " + beacon.getDistance() + " meters away.");
}
For 2. I was having trouble detecting that, but it might be a long timeout. So be very patient. I think the code in the Monitoring activity can be altered to display a message for that. Or you can review the logcat from a device.
A simple logToDisplay can be used inside the didExitRegion section of BeaconReferenceApplication.
public void didExitRegion(Region region) {
if (monitoringActivity != null) {
monitoringActivity.logToDisplay("I no longer see a beacon in the "+region.getUniqueId());
}
}

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

I am working with iBeacons and using the AltBeacon library.
beaconManager.getBeaconParsers()
.add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
I would like my Android app to detect and generate an event when beacons enter and exit visibility.
This works just fine fine with a single beacon using the library using methods.
public void **didEnterRegion**(Region region)
and
public void **didExitRegion**(Region region)
My problem is when multiple beacons are visible at the same time.
I am trying to maintain an array with all beacons visible.
I want to generate an event each time a beacon enters and exits.
The event should identify the beacon that generated the event by it's unique Identifier.
My beacons are uniquely identifiable using the beacon.getIdentifiers() or (UUID, Major and Minor)
The problem is that the didExitRegion method does not get executed until all beacons exit the region.
Can anyone think of a simple way for me to achieve my goals using AltBeacon library?
Any suggestions would be greatly appreciated.
Two options:
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);
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.
/***************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());
}
});
}

Categories

Resources