使用Android Beacon库进行后台信标检测

时间:2019-04-19 08:06:38

标签: java android ibeacon beacon

我正在使用Android Beacon库扫描iBeacon。我正在使用 HM-10 BLE模块作为iBeacon。我的问题是当我使用Android Beacon库示例代码时,什么都没有发生。 如在后台启动应用程序的示例代码中所述,我创建了一个名为“ Backgroud”的新Java类和MainActivity类。

我希望在未打开应用程序时检测到信标时,我的应用程序启动。或在应用打开时显示通知(吐司)

我也想知道,我们在 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
    }
}

0 个答案:

没有答案