I was able to get the code above working with a few modifications:
I commented out the
verifyBluetooth((Activity) context);
line because it was crashing myMainActivity
with aNullPointerException
. If you see your activity launch properly, you may not need to do this. If you don't see it launch, thenBeaconUtils
will be disposed of by Android along with yourMainActivity
and can't get any callbacks when it sees iBeacons.I had to change the
createNotification
method to get it to work -- the original code did not display a notification for me, although I am not exactly clear why. The code I got to work is:private void createNotification() { NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setContentTitle("New beacon in range") .setContentText("You are currently in the range of a new beacon.") .setSmallIcon(R.drawable.ic_launcher); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addNextIntent(new Intent(context, MainActivity.class)); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); builder.setContentIntent(resultPendingIntent); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, builder.build()); }
Once I made this change, the log displayed the following line after launch:
01-21 12:52:43.112 I/BeaconUtils﹕ I have just switched from seeing/not seeing iBeacons: null
And the following notification was displayed:
In general, the best way to troubleshoot problems like this is to add log messages. In the case where you are not seeing many log messages, I would add them at the top of each lifecycle and callback method, including onCreate
, onIBeaconServiceConnect()
, etc. Once you do this, any messages you should see but don't give you a good idea where something is going wrong.
A few other tips:
Each time you launch your app from Eclipse/Android Studio, be sure to make some code change, otherwise the app won't be uninstalled and reinstalled, and the iBeacon Service won't restart. Unless the service is restarted, you won't get new entered region notifications for iBeacons that were already detected.
Be careful that you only have one monitorNotifier or rangingNotifier on your iBeaconManager. Whatever is the last notifier set is the one that will get all the callbacks.
If you don't see that your on
onIBeaconServiceConnect()
method is being called (best to do this with a log line), then stop everything until you get that working.In general, the
IBeaconConsumer
interface is designed to work with anActivity
,Service
orApplication
instance. There's nothing wrong doing this with a custom class like yourBeaconUtils
, but you have to be extra careful that your context is set properly, and that whatever is holding a reference to your custom object doesn't dispose of it during the Android lifecycle. Edit: Also, when making custombindService
andunbindService
methods, the methods must chain to the equivalent methods on the context. I am surprised this works at all as-is. See my related answer here: https://stackoverflow.com/a/21298560/1461050