i am developing a android app and detect beacons every 1 seconds, I use altbeacon library to implement the service, I test this app using 200 beacons just put aside from me, but it seems that my app only detect 40-80 number of beacons, not nearly 200 beacons, I thought it might because of beacon's advertising interval, so I adjust all the beacon's interval as 300ms, and finally I still get similar results(less than 80), Is anyone knows what's wrong happens with it?
here I attach my main code:
I first init beacomanager with layout and bind the service:
private void initManager(){
beaconManager = BeaconManager.getInstanceForApplication(getActivity().getApplicationContext());
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"));
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:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
beaconManager.setForegroundScanPeriod(1000);
beaconManager.setForegroundBetweenScanPeriod(0);
beaconManager.bind(this);
try {
if (beaconManager.isAnyConsumerBound()) {
beaconManager.updateScanPeriods();
}
} catch (RemoteException e) {
Log.e("RECO", "update scan periods error", e);
}
}
I then get the result from onBeaconServiceConnect
and send it to mainthread (I create a beacon to startranigng)
@Override
public void onBeaconServiceConnect() {
Log.d(TAG, "onBeaconServiceConnect called");
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Log.i("RECORangingActivity", "didRangeBeaconsInRegion() region: " + region.getUniqueId() + ", number of beacons ranged: " + beacons.size());
// send bacon to main_activity to update UI
ArrayList<Beacon> scanedBeacon = new ArrayList<Beacon>(beacons);
// rank beacon by RSSI
Collections.sort(scanedBeacon, new Comparator<Beacon>() {
public int compare(Beacon one, Beacon other) {
return Integer.compare(other.getRssi(), one.getRssi());
}
});
Intent intent = new Intent(action);
intent.putParcelableArrayListExtra("RecoBeacon", scanedBeacon);
getActivity().sendBroadcast(intent);
}
});
}
and finally, I create startranging and stopranging function to control it and put startranging into a thread inside onclicklistener;
private void startRanging(){
Log.d(TAG, "start Ranging beacons");
try {
beaconRegion = new Region("MyBeacons", Identifier.parse(RECO_UUID), null, null);
beaconManager.startMonitoringBeaconsInRegion(beaconRegion);
beaconManager.startRangingBeaconsInRegion(beaconRegion);
} catch (RemoteException e) {
e.printStackTrace();
}
}
private void stopRanging(){
Log.d(TAG, "stop Ranging beacons");
try {
beaconManager.stopMonitoringBeaconsInRegion(beaconRegion);
beaconManager.stopRangingBeaconsInRegion(beaconRegion);
} catch (RemoteException e) {
e.printStackTrace();
}
}
startBT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startBT.setText("Running");
startBT.setEnabled(false);
Thread t1 = new Thread(){
@Override
public void run() {
startRanging();
}
};
t1.start();
}
})
and the detected result is as follows:
08-21 06:33:51.373 10408-11779/com.example.haoch.groupsI/RECORangingActivity: didRangeBeaconsInRegion() region: MyBeacons, number of beacons ranged: 60
08-21 06:33:52.394 10408-11780/com.example.haoch.groupse I/RECORangingActivity: didRangeBeaconsInRegion() region: MyBeacons, number of beacons ranged: 49
08-21 06:33:53.416 10408-11781/com.example.haoch.groupse I/RECORangingActivity: didRangeBeaconsInRegion() region: MyBeacons, number of beacons ranged: 43
08-21 06:33:54.475 10408-11782/com.example.haoch.groupse I/RECORangingActivity: didRangeBeaconsInRegion() region: MyBeacons, number of beacons ranged: 58
08-21 06:33:55.515 10408-11783/com.example.haoch.groupse I/RECORangingActivity: didRangeBeaconsInRegion() region: MyBeacons, number of beacons ranged: 64
08-21 06:33:56.541 10408-11784/com.example.haoch.groupse I/RECORangingActivity: didRangeBeaconsInRegion() region: MyBeacons, number of beacons ranged: 64
08-21 06:33:57.566 10408-11785/com.example.haoch.groupse I/RECORangingActivity: didRangeBeaconsInRegion() region: MyBeacons, number of beacons ranged: 46
08-21 06:33:58.599 10408-11786/com.example.haoch.groupse I/RECORangingActivity: didRangeBeaconsInRegion() region: MyBeacons, number of beacons ranged: 68
08-21 06:33:59.637 10408-11788/com.example.haoch.groupse I/RECORangingActivity: didRangeBeaconsInRegion() region: MyBeacons, number of beacons ranged: 68
08-21 06:34:00.665 10408-11789/com.example.haoch.groupse I/RECORangingActivity: didRangeBeaconsInRegion() region: MyBeacons, number of beacons ranged: 72
Apparently, this is the wrong detecting result, it should detect nearly 200 beacons but I only get quite small number of beacons.