How to make didRangeBeaconsInRegion call once for all the uuid - android

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.

Related

AltBeacon Android Beacon Library region id is null

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.

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);
}
}

onLeScan callback returns oddly positive RSSI values

using Bluetooth Low Energy (BLE) scan on Android, I noticed that sometimes RSSI values are incorrect.
My code simply calls the start scan function:
mBluetoothAdapter.startLeScan(mLeScanCallback);
and then I read results in the callback and save results in a file:
private static BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
#Override
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
String objScanRec = bytesToHex(scanRecord);
outStr = rssi + ";" + objScanRec + ";" + device.getName() + ";" + beaconLocation + ";\n";
try {
Raw_log.write(outStr);
Raw_log.flush();
} catch (IOException e) {
e.printStackTrace();
}
// }
}
};
the problem is that I read positive RSSI values, also if the beacon is at a fixed distance.
E.g. I have the beacon 30 cm from the phone (or smartwatch) I read a values around -45 which are realistic, but also values around +80 or +100 (which are not realistic) those values are around 20% of measurements.
Is there something that I'm missing?
thanks
thanks for your help I figured out it's a problem related only to Samsung Gear Live. I came up with this s solution:
if(rssi > 0){
rssi = rssi - 128;
}
I've tested the solution and it works fine. (e.g. the obtained positive values after correction are now similar to negative values, for example I read
-44 -45 -43 84 82
that after correctio become:
-44 -45 -43 -44 -46)
This is definitely not normal. I have never seen a rssi value in that callback be positive. Typical values are from -30 to -120.
I suspect there is something wrong with the way the data are written out to the log, or read back. What happens if you just do a regular Log.d(TAG, "rssi="+rssi); Do you ever see positive values? If so, can you share an excerpt, along with the device model you are using to detect and the device you are detecting?

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());
}
});
}

How to determine if two phone numbers are the same?

If we have a phone number like 358541321 without a country code, sometimes when phone rings it says (+56 - 358541321) or +56358541321.
How to detect whether the ringed number is first number?
The number is not saved in phone memory in order to phone lookup.
https://developer.android.com/reference/android/telephony/PhoneNumberUtils.html
provides a neat solution:
import android.telephony.PhoneNumberUtils;
...
String one = "+51 - 3245678";
String two = "+513245678";
boolean isSame = PhoneNumberUtils.compare(one, two);
The usual solution to this problem is just to compare the last X (e.g. 7 or 8, depending on your country) digits of the number. In rare cases, this can lead to false positives, but usually it's a good approximation and it avoids the problem of different or missing country or area codes.
Java regular expression and String function replaceAll can do this easily.
this way,
String one = "+51 - 3245678";
String two = "+513245678";
one = one.replaceAll("[^0-9]","");
two = two.replaceAll("[^0-9]","");
Toast.makeText(this, one+" -- "+two, Toast.LENGTH_LONG).show();
if(one.equalsIgnoreCase(two))
{
Toast.makeText(this, "Both Are Equal", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "Different", Toast.LENGTH_LONG).show();
}

Categories

Resources