Chintan Soni
Chintan Soni

Reputation: 25267

android: not detecting beacon

I have an activity that holds just single line in onCreate()

startService(new Intent(this, MyBeaconService.class));

MyBeaconService.class

public class MyBeaconService extends Service implements IBeaconConsumer{

    private IBeaconManager iBeaconManager = IBeaconManager
            .getInstanceForApplication(this);

    public MyBeaconService() {
        Log.i("MyBeaconService", "MyBeaconService");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw null;
    }

    @Override
    public void onIBeaconServiceConnect() {
        Log.i("MyBeaconService", "onIBeaconServiceConnect");
        iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
            @Override
            public void didEnterRegion(Region region) {
                Log.e("BeaconDetactorService", "didEnterRegion");
                 Log.i("BeaconDetactorService", "I just saw an iBeacon for the first time!");

                Intent dialogIntent = new Intent(MyBeaconService.this, MyAdActivity.class);
                dialogIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(dialogIntent);
            }

            @Override
            public void didExitRegion(Region region) {
                Log.e("BeaconDetactorService", "didExitRegion");
                // logStatus("I no longer see an iBeacon");
            }

            @Override
            public void didDetermineStateForRegion(int state, Region region) {
                Log.e("BeaconDetactorService", "didDetermineStateForRegion");
                // logStatus("I have just switched from seeing/not seeing iBeacons: "
                // + state);
            }

        });

        try {
            iBeaconManager.startMonitoringBeaconsInRegion(new Region(
                    "myMonitoringUniqueId", null, null, null));
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.i("MyBeaconService", "onCreate");

        iBeaconManager.bind(this);
    }
}

The problem is that I am not able to detect any beacon.. I mean.. didEnterRegion method is never called..

Please help me with it..

Thanks.

Upvotes: 1

Views: 280

Answers (1)

davidgyoung
davidgyoung

Reputation: 64916

A couple of tips:

  • Make sure your mobile device can see your beacon with an off the shelf app like Locate.

  • Make sure your debug lines show that onIBeaconServiceConnect is getting called. Is your service even starting?

  • If the above method is getting called, try turning on low level debug in the library with: iBeaconManager.setDebug(true) and paste the output of what you see in LogCat in your question.

  • The Library you are using was deprecated and replaced by the Android Beacon Library in July 2014. If you intend to continue doing development with beacons, it would be wise to switch to the newer library. It has a very similar syntax and design and gets bug fixes and ongoing support. It also detects beacons in the background out of the box without the need to build your own service.

Note: I have revised this answer to remove a tip about method chaining which should not be necessary in this case.

Full disclosure: I am lead developer on the Android Beacon Library open source project and the author of the original Library used by the code in the question.

Upvotes: 2

Related Questions