I am using altbeacon: https://altbeacon.github.io/android-beacon-library/ I bind my beaconManager. and then I call this:
public void startListeningForBeacons(RangeNotifier rangeNotifier) {
try {
Region region = new Region("all-beacons-region", null, null, null);
// Tells the BeaconService to start looking for beacons that match the passed Region object
beaconManager.startRangingBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
// Specifies a class that should be called each time the BeaconService gets ranging data, once per second by default
beaconManager.addRangeNotifier(rangeNotifier);
}
Which starts calling this function inside my application class:
@Override
public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
Log.i("BluetoothService", "BluetoothService region The first beacon I see is about " + beacons.iterator().next().getDistance() + " meters away.");
if (didRangeBeaconsCallback != null)
didRangeBeaconsCallback.rangeCalled(beacons, region);
}else {
Log.i("BluetoothService", "BluetoothService region NO BEACONS: " + beacons.size());
if(PSBluetoothService.getInstance(PSApplicationClass.this).beaconUUID != null){
if (didRangeBeaconsCallback != null)
didRangeBeaconsCallback.rangeCalled(null, null);
}
}
}
But a lot of times, I get back o collection of size 0. Even thought I have 2 beacons in range. I also tried with a LG Nexus 5 and a LeEco LePro2 and both return the correct list of beacons. What might go wrong when it comes to the Huawei?
If it helps, this is the list of beacon layouts I configure:
//sets the types of beacons that the library is searching for
public void setNeededTypesOfBeacons() {
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT));
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.ALTBEACON_LAYOUT));
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.getBeaconParsers().add(new BeaconParser()
.setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
}
And I use this beacons: https://nl.aliexpress.com/item/3-pcs-BLE-4-0-Base-Station-Ebeoo-iBeacon-USB/32752285433.html?spm=a2g0s.9042311.0.0.g1aZkv
This is a picture showing what it finds, compared to the others: https://s3.amazonaws.com/uploads.hipchat.com/39260/829560/5FNgB6ARACpywWv/upload.png
Made a sample app, just with this logic, if it helps: https://www.dropbox.com/s/4ddt9mhn7zdi9gd/Beacon.zip?dl=0
Even when leaving it scan for a long time, still no results, when it doesn't find them:
03-20 12:30:24.542: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:25.644: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:26.749: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:27.855: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:28.960: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:30.071: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:31.174: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:32.277: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:33.379: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:34.486: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:35.588: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:36.696: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:37.803: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:38.906: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:40.012: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:41.210: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:42.313: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:43.418: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:44.522: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:45.628: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:46.734: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:47.839: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:48.943: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:50.061: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:50.061: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:51.165: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:52.268: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:53.372: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
03-20 12:30:54.476: I/BluetoothService(10806): BluetoothService region NO BEACONS: 0
Based on comments below the question, the detection is intermittent on Huawei and the code only has a 5-10% chance of detecting a beacon in 5 seconds of scanning, at which point the code gives up.
There are some bluetooth chips that are slow to detect the first beacon after starting scanning. This may be a hardware limitation that you must learn to live with.
I would suggest you rework the code to:
Look for beacons for much longer than 5 seconds and show results as soon as you find one. This way it will be fast on fast devices and slow on slow devices.
Don't unbind from the beacon manager until the app goes to the background. This way, it is already scanning in case you hit retry, and there won't be a delay in starting up scanning.
If you have control over the configuration of the hardware beacon, make sure it is transmitting at the highest rate possible. Some beacons are set to transmit only once every 1-5 seconds, which decreases the chance of detecting a packet within a short time interval. The more often you make the beacon transmit, the greater the odds of it getting detected within 5 seconds.