在后台对 Eddystone Beacon 做出反应
Reacting to Eddystone Beacon in the Background
我基本上是在尝试完成 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。根据他们的文档,我只需要实现BootstrapNotifier
,设置一个Region
,然后每当它扫描到信标时,它就会自动调用didEnterRegion
。我的地区是 Region region = new Region("all-beacons-region", null, null, null);
。
我还构建了一个非常简单的应用程序,可以在前台扫描并找到信标。所以没问题,我绝对能够拿起我的信标并从中提取基本信息。
我的主要 activity 看起来像这样:
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);
}
}
我的 class 看起来像这样:
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);
}
}
(编辑)
最后,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>
一切都感觉很简单,我很好奇我是否遗漏了一些简单或愚蠢的东西。我在网上的几个地方找到了这个例子,但没有提到我可能错过的任何东西。我有相当扎实的编码背景,但我是 Android 的新手,也是 Eddystone/BLE 技术的新手。只是为了澄清实际问题:当我将 phone 移动到信标范围内时,我的应用程序没有任何反应。我的期望是它将 "wake up" 并开始 MainActivity。我肯定在信标的范围内,信标肯定是开着的,而且我能够在前台扫描它。它只是不会唤醒我的应用程序。谢谢!
创建自定义 Android Application
class 时,如 TestApp
,您必须在清单中使用 name
属性声明它。像这样:
<application
android:name="TestApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
如果您不这样做,Android 将不会使用您的自定义 TestApp
class,而是默认使用其内置基础 Application
class,导致 TestApp
代码中的 none 被执行。
我基本上是在尝试完成 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。根据他们的文档,我只需要实现BootstrapNotifier
,设置一个Region
,然后每当它扫描到信标时,它就会自动调用didEnterRegion
。我的地区是 Region region = new Region("all-beacons-region", null, null, null);
。
我还构建了一个非常简单的应用程序,可以在前台扫描并找到信标。所以没问题,我绝对能够拿起我的信标并从中提取基本信息。
我的主要 activity 看起来像这样:
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);
}
}
我的 class 看起来像这样:
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);
}
}
(编辑) 最后,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>
一切都感觉很简单,我很好奇我是否遗漏了一些简单或愚蠢的东西。我在网上的几个地方找到了这个例子,但没有提到我可能错过的任何东西。我有相当扎实的编码背景,但我是 Android 的新手,也是 Eddystone/BLE 技术的新手。只是为了澄清实际问题:当我将 phone 移动到信标范围内时,我的应用程序没有任何反应。我的期望是它将 "wake up" 并开始 MainActivity。我肯定在信标的范围内,信标肯定是开着的,而且我能够在前台扫描它。它只是不会唤醒我的应用程序。谢谢!
创建自定义 Android Application
class 时,如 TestApp
,您必须在清单中使用 name
属性声明它。像这样:
<application
android:name="TestApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
如果您不这样做,Android 将不会使用您的自定义 TestApp
class,而是默认使用其内置基础 Application
class,导致 TestApp
代码中的 none 被执行。