Altbeacon Library - 在Background和Foregorund扫描时不会触发didEnterRegion函数

时间:2016-02-25 19:44:57

标签: android background monitoring scanning altbeacon

我的目的是在申请被关闭时,申请人必须向用户发送通知。我为此使用了Altbeacon背景库。

参考:https://github.com/AltBeacon/android-beacon-library-reference/blob/master/app/src/main/java/org/altbeacon/beaconreference/BeaconReferenceApplication.java

虽然应用程序发送检测到信标的日志,但是BootErapNotifier不会触发didEnterRegion函数。我无法理解这种情况。

我的日志:

 02-25 21:15:39.906 3183-3203/com.beacon.beacondetection.beacondetection D/BluetoothAdapter: onScanResult() - Device=E1:B9:E3:76:91:50 RSSI=-50

 02-25 21:15:39.916 3183-3204/com.beacon.beacondetection.beacondetection D/BluetoothAdapter: onScanResult() - Device=CC:E0:4C:60:F8:C8 RSSI=-87

的AndroidManifest.xml

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<application
    android:name=".MyApplicationName"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">


    <activity
        android:launchMode="singleInstance"
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

BeaconDetection:

类BeaconDetection扩展Application实现BootstrapNotifier {

private static final String TAG = "BeaconDetection";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private MainActivity monitoringActivity = null;

public void onCreate() {
    super.onCreate();
    BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);

    beaconManager.getBeaconParsers().clear();
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));

    beaconManager.setBackgroundScanPeriod(1000l);
    beaconManager.setBackgroundBetweenScanPeriod(3000l);

    Region region = new Region("backgroundRegion", null, null, null);
    regionBootstrap = new RegionBootstrap(this, region);

    backgroundPowerSaver = new BackgroundPowerSaver(this);
}


@Override
public void didEnterRegion(Region arg0) {
    Log.d(TAG, "did enter region.");
    if (!haveDetectedBeaconsSinceBoot) {
        Log.d(TAG, "auto launching MainActivity");

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
        haveDetectedBeaconsSinceBoot = true;
    } else {
        if (monitoringActivity != null) {
        } else {

            Log.d(TAG, "Sending notification.");
            sendNotification();
        }
    }
}

@Override
public void didExitRegion(Region region) {
    if (monitoringActivity != null) {
    }
}

@Override
public void didDetermineStateForRegion(int state, Region region) {
    if (monitoringActivity != null) {
    }
}

private void sendNotification() {
    NotificationCompat.Builder builder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                    .setContentTitle("Beacon Reference Application")
                    .setContentText("An beacon is nearby.")
                    .setSmallIcon(R.mipmap.ic_launcher);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    builder.setContentIntent(resultPendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());
}

public void setMonitoringActivity(MainActivity activity) {
    this.monitoringActivity = activity;
}

}

1 个答案:

答案 0 :(得分:0)

一种可能性是您的信标没有发送AltBeacon数据包,这是所示代码设置为检测的唯一信标格式。

如果您的信标发射器正在发送Eddystone-UID或iBeacon格式的数据包,则需要添加一个新行:

beaconManager.getBeaconParsers().add(new BeaconParser().
        setBeaconLayout("...")

其中...是您的信标类型的正确布局表达式。您可以通过Google搜索轻松找到适合您的信标类型的布局字符串。