gpt4 book ai didi

java - 在后台对 Eddystone Beacon 使用react

转载 作者:行者123 更新时间:2023-11-30 00:38:34 24 4
gpt4 key购买 nike

我基本上是在尝试完成 this ,使用提供的第一个答案。这个问题在网上和 SO 都有答案,但我无法让它发挥作用。有什么我需要启用、添加到我的 list 等吗?我想对手机范围内的信标使用react。我在 Android Studio 中工作,针对 Android 4.3 并使用 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 技术的新手。只是为了澄清实际问题:当我将手机移到信标范围内时,我的应用程序没有任何反应。我的期望是它将“醒来”并开始 MainActivity。我肯定在信标的范围内,信标肯定是开着的,而且我能够在前台扫描它。它只是不会唤醒我的应用程序。谢谢!

最佳答案

当创建自定义 Android Application 类(如 TestApp)时,您必须在 list 中使用 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 类,而是默认使用其内置的 Application 基类,从而导致 none要执行的 TestApp 代码。

关于java - 在后台对 Eddystone Beacon 使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42960386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com