java - 使用 Android 信标库进行背景信标检测
问题描述
我正在使用 Android 信标库来扫描 iBeacons。我将HM-10 BLE 模块用作 iBeacon。我的问题是当我使用 Android 信标库示例代码时,什么都没有发生。正如在后台启动应用程序的示例代码中所述,我创建了一个名为“Backgroud”的新 java 类和 MainActivity 类。
我希望我的应用程序在未打开应用程序时检测到信标时启动。或者在应用打开时显示通知(Toast) 。
我也想知道,我们在MainActivity类中放了什么。
任何帮助将不胜感激。
这是我的 AndroidManifest.xml 文件:
<application
android:allowBackup="true"
android:name=".Background"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
这是我的 MainActivity Java 类:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
}
}
这是我的背景 Java 类:
public class Background extends Application implements BootstrapNotifier {
private static final String TAG = ".Background";
private RegionBootstrap regionBootstrap;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "App started up");
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
// To detect proprietary beacons, you must add a line like below corresponding to your beacon
// type. Do a web search for "setBeaconLayout" to get the proper expression.
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
// 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 didDetermineStateForRegion(int arg0, Region arg1) {
// Don't care
}
@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.
Toast.makeText(getApplicationContext(), "A Beacon is detected", Toast.LENGTH_LONG).show();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
@Override
public void didExitRegion(Region arg0) {
// Don't care
}
}
解决方案
推荐阅读
- ios - iOS 意外的平台条件(预期为“os”、“arch”或“swift”)- 可达性
- c# - 发现 Automapper 未映射的成员
- html - 如何摆脱 CSS 打印媒体中的八边形边框?
- angular - 将数据或 html 注入 app.component
- django - Rest Framework 中的嵌套视图
- twilio - 启用机器检测的调用 url 和 statusCallback 的 Twilio Voice api 顺序
- django - 运行 manage.py runserver 时出现 django 问题
- javascript - UnhandledPromiseRejectionWarning:在猫鼬中未定义
- python - while 循环 - 将输出发送到 csv 文件
- azure - 如何在 Azure Function App 中设置“X-Content-Type-Options”标头?