Java 基于Android信标库的背景信标检测

Java 基于Android信标库的背景信标检测,java,android,ibeacon,beacon,Java,Android,Ibeacon,Beacon,我正在使用Android Beacon库扫描iBeacons。我使用HM-10BLE模块作为iBeacon。我的问题是,当我使用Android Beacon库示例代码时,什么都没有发生。 正如在后台启动应用程序的示例代码所述,我创建了一个名为“Background”的新java类和MainActivity类 我希望我的应用程序在未打开时检测到信标时启动。或在应用程序打开时显示通知(Toast) 我还想知道,我们在MainActivity课程中放了什么 任何帮助都将不胜感激 这是我的Android

我正在使用Android Beacon库扫描iBeacons。我使用HM-10BLE模块作为iBeacon。我的问题是,当我使用Android Beacon库示例代码时,什么都没有发生。 正如在后台启动应用程序的示例代码所述,我创建了一个名为“Background”的新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>
这是我的背景Java类:

public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate( savedInstanceState );
            setContentView( R.layout.activity_main );
        }
    }
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
    }
}

您运行了哪些特定测试,但结果没有达到预期效果?运行程序时,您在LogCat中看到了什么?你看到“应用程序启动”了吗?你看到“接到电话”了吗?你知道你的信标正在工作吗?通过一个现成的信标检测器应用程序来查看它?你好,大卫,事实上我没有看到“有一个didEnterRegion呼叫”。是的,我的信标正在工作,我可以通过扫描应用程序检测到它。我想要的是,当应用程序检测到信标时,会显示一条toast消息。您是否会收到一个到
dideterministateforregion
的回调?如果是,arg0的值是多少?这应该是您是否已经在该区域内的指标。如果已经进入该区域,则必须先退出该区域,然后才能下注第二个didEnterRegion事件,即使在应用程序重新启动时也是如此。