-1

I am using altbeacon for android and i cant't detect my beacon. I have tried https://github.com/AltBeacon/android-beacon-library-reference sample code also but can't detect beacon. Here is my code:

public class BeaconApplication extends Application implements BootstrapNotifier {

private Region region;
private RegionBootstrap regionBootstrap;
private BeaconManager beaconManager;

@Override
public void onCreate() {
    super.onCreate();
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=beac,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.setBackgroundScanPeriod(5000);
    beaconManager.setBackgroundBetweenScanPeriod(10000);
    region = new Region("backgroundRegion",
            Identifier.parse("EBEFD083-70A2-47C8-9837-E7B5634DF524"), null, null);
    regionBootstrap = new RegionBootstrap(this, region);
    Log.i("selfBeacon", "Bootstrap created");
}

@Override
public void didEnterRegion(Region region) {
    Log.i("selfBeacon", "Bootstrap didEnterRegion");
    regionBootstrap.disable();
    Intent intent = new Intent(this, SelfBeaconService.class);
    if (Build.VERSION.SDK_INT >= 26) {
        startForegroundService(intent);
    } else {
        startService(intent);
    }
    Log.i("selfBeacon", "Service start commanded");
}

@Override
public void didExitRegion(Region region) {

}

@Override
public void didDetermineStateForRegion(int i, Region region) {

}

public void resumeScanning () {
    beaconManager.setBackgroundScanPeriod(5000);
    beaconManager.setBackgroundBetweenScanPeriod(10000);
    regionBootstrap = new RegionBootstrap(this, region);
    Log.i("selfBeacon", "scanning resumed");
}

}

public class SelfBeaconService extends Service implements BeaconConsumer {

private BeaconManager beaconManager;
private Region region;
private int noBeaconDetectedCount = 0;

@Override
public void onCreate() {
    super.onCreate();
    Log.i("selfBeaconService", "onCreate");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("selfBeaconService", "onStartCommand");
    broadcastSystemLog("Beacon Service onCreate triggered.");
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.setEnableScheduledScanJobs(false); //Stop scanning
    beaconManager.setForegroundScanPeriod(2000);
    beaconManager.setForegroundBetweenScanPeriod(2000);
    beaconManager.bind(this);
    region = new Region("foreground region",
            Identifier.parse("EBEFD083-70A2-47C8-9837-E7B5634DF524"), null, null);

    startForeground();

    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onBeaconServiceConnect() {
    Log.i("selfBeaconService", "onBeaconServiceConnect");
    broadcastSystemLog("onBeaconServiceConnect triggered.");
    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
            if (collection.size() > 0) {
                noBeaconDetectedCount = 0;
                double closestDistance = -1;
                for (Beacon each : collection) {
                    if (closestDistance == -1) {
                        closestDistance = each.getDistance();
                    } else if (each.getDistance() < closestDistance) {
                        closestDistance = each.getDistance();
                    }
                }
                Log.i("selfBeaconService", "didRangeBeaconsInRegion, the closest beacon is about " +
                        closestDistance + " meters away.");
                broadcastToActivity("The closest beacon is about " +
                        closestDistance + " meters away.");
            } else {
                noBeaconDetectedCount++;
                broadcastToActivity("No beacon has been detected for " + noBeaconDetectedCount + " times");
                if (noBeaconDetectedCount > 10) { //10*(2000ms+2000ms) = 40 seconds
                    stopForeground(true);
                    terminate();
                }
            }
        }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(region);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

@Override
public void onDestroy() {
    Log.i("selfBeacon", "onDestroy");
    broadcastSystemLog("BeaconService onDestroy triggered.");
    super.onDestroy();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void broadcastToActivity (String message) {
    Intent intent = new Intent();
    intent.setAction("beaconBroadcast");
    intent.putExtra("beaconService", message);
    sendBroadcast(intent);
}

private void broadcastSystemLog (String message) {
    Intent intent = new Intent();
    intent.setAction("beaconBroadcast");
    intent.putExtra("beaconServiceLog", message);
    sendBroadcast(intent);
}

private void startForeground () {
    NotificationCompat.Builder notificationBuilder = null;
    Notification notification = null;
    if (Build.VERSION.SDK_INT >= 26) {
        notificationBuilder = new NotificationCompat.Builder(this, "rangingService")
                .setContentTitle("Active Scanning")
                .setContentText("App is scanning for nearby beacons");
        notification = notificationBuilder.build();
    } else {
        notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("Active Scanning")
                .setContentText("App is scanning for nearby beacons");
        notification = notificationBuilder.build();
    }
    startForeground(1234, notification);
}

private void terminate () {
    try {
        beaconManager.stopRangingBeaconsInRegion(region);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
    beaconManager.removeAllRangeNotifiers();
    beaconManager.unbind(this);
    beaconManager.setEnableScheduledScanJobs(true);
    ((BeaconApplication)getApplication()).resumeScanning();
    stopSelf();
    Log.i("selfBeaconService", "service stopped");
    broadcastSystemLog("BeaconSevice stopped.");
}

}

  • 1
    Please describe why this code does not work as you expect. How do you test it? What methods get called? what methods do not get called? What log lines do you see during your tests? – davidgyoung Aug 29 '18 at 11:31
  • I have test with ibeacon. I have put it near my device but it doesn't show the beacon it always return Collection size 0. – Pritesh Patel Aug 30 '18 at 05:25

1 Answers1

0

A few things to check:

  1. Ensure you have added code to dynamically request location permission at runtime. See here for how to do that.
  2. After adding the above, make sure this permission has been properly obtained. Go to. Settings->Apps->{your app name}->Permissions to see
  3. Make sure Bluetooth and Location are enabled on your phone.
  4. If you have checked all of the above and it still does not work, try an off the shelf beacon detector like BeaconScope. If it does not detect, there may be a problem with either your phone hardware or your beacon hardware.
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Thanks for your answer. I can see beacon now with activity class. But the problem is with Application class it did not call didEnterRegion(Region region) so i can start my background service. – Pritesh Patel Aug 31 '18 at 05:51