首页 > 解决方案 > 使用 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
    }
}

标签: javaandroidibeaconbeacon

解决方案


推荐阅读