I'm basically attempting to accomplish this, using the first answer provided. The question has answers around the web and SO, but I'm having trouble getting it to work. Is there something I need to enable, add to my manifest, ect.? I want to react to a beacon coming within range of the phone. I'm working in Android Studio, targeting Android 4.3 and working with the Android Beacon Library. Based on their documentation, all I need is to implement BootstrapNotifier
, set a Region
, and then anytime it scans a beacon, it will automatically call didEnterRegion
. My Region is Region region = new Region("all-beacons-region", null, null, null);
.
I have also built a very simple app which scans for and finds beacons in the foreground. So no problems there, I am definitely able to pick up my beacons and pull basic info from them.
My main activity looks like this:
package com.example.justin.backgroundscantest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
And my class looks like this:
import android.app.Application;
import android.content.Intent;
import android.util.Log;
import com.example.justin.backgroundscantest.MainActivity;
import org.altbeacon.beacon.startup.BootstrapNotifier;
import org.altbeacon.beacon.startup.RegionBootstrap;
import org.altbeacon.beacon.BeaconManager;
import org.altbeacon.beacon.BeaconParser;
import org.altbeacon.beacon.Region;
public class TestApp extends Application implements BootstrapNotifier {
private static final String TAG = ".TestApp";
private RegionBootstrap regionBootstrap;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "App started up");
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
// wake up the app when any beacon is seen (you can specify specific id filers in the parameters below)
Region region = new Region("com.example.myapp.boostrapRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
}
@Override
public void didEnterRegion(Region arg0) {
Log.d(TAG, "Got a didEnterRegion call");
// This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched)
// if you want the Activity to launch every single time beacons come into view, remove this call.
regionBootstrap.disable();
Intent intent = new Intent(this, MainActivity.class);
// IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances
// created when a user launches the activity manually and it gets launched from here.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
}
(Edit) Finally, AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.justin.backgroundscantest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
It all feels pretty straightforward, which is I'm curious if I'm missing something simple or silly. I've found this example in several places around the web, but no mention of anything that I might have missed. I have a pretty solid coding background but am new to Android and very new to Eddystone/BLE technology. And just to clarify the actual problem: there is no reaction from my app when I move the phone within range of the beacon. My expectation is that it will "wake up" and begin MainActivity. I am definitely in range of the beacon, the beacon is definitely on, and again, I am able to scan it in the foreground. It just won't wake up my app. Thanks!
When creating a custom Android Application
class like TestApp
, you must declare it in your manifest with a name
attribute. Like this:
<application
android:name="TestApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
If you don't do that, Android won't use your custom TestApp
class, and will instead default to its built-in base Application
class, causing none of the TestApp
code to get executed.