在前景和后台检测到iBeacons时,AltBeacon库不稳定

时间:2015-02-06 09:42:51

标签: android ibeacon altbeacon

我已完全按照AltBeacon' reference app及其教程中的步骤对我的应用进行前景和后台的iBeacon检测。

实际上,我的应用确实检测到了iBeacons。我会在检测到iBeacon后发送通知。

然而,无论我的应用程序处于前台还是后台(特别是当设备被锁定时),测距过程可能会在几分钟后突然停止。

有时,我会在重新开启设备中的蓝牙后重启检测(i),(ii)重新启动应用程序后。但有时它不会。

我在我的应用程序的自定义应用程序中实现了BootstrapNotifier和RangeNotifier。以下是一些代码片段

MyApplication.java

public class MyApplication extends Application implements BootstrapNotifier, RangeNotifier {
private static final String TAG = "MyApplication";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private int notificationID = 0;

@Override
public void onCreate() {
    super.onCreate();

    BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.setBackgroundScanPeriod(1100l);
    beaconManager.setBackgroundBetweenScanPeriod(1100l);

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

    backgroundPowerSaver = new BackgroundPowerSaver(this);
}

@Override
public void didEnterRegion(Region region) {
    BeaconManager.getInstanceForApplication(this).setRangeNotifier(this);
    try {
        BeaconManager.getInstanceForApplication(this).startRangingBeaconsInRegion(region);
    }
    catch (RemoteException e) {
        Log.e(TAG, "Can't start ranging");
    }
}

@Override
public void didExitRegion(Region region) {

}

@Override
public void didDetermineStateForRegion(int i, Region region) {

}

private void sendNotification(Beacon beacon) {
    String message = beacon.getId3().toString();

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle("Where")
            .setContentText(message).setAutoCancel(true);
    Intent intent = new Intent(this, RDMainActivity.class);
    intent.putExtra("Beacon", beacon);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), notificationID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notificationID++, mBuilder.build());
}

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
    for (Beacon beacon : beacons) {
        sendNotification(beacon);
    }
}

我的测试设备是带有Android4.4.4的Nexus 7(2013移动版)

有没有人对这个问题有任何想法?任何帮助将不胜感激。

更新:这是我在前景中最后一次检测后的logcat

02-06 20:25:28.795  19482-19482/com.myapp D/BluetoothAdapter﹕ stopLeScan()
02-06 20:25:28.815  19482-19482/com.myapp W/CycledLeScanner﹕ Internal Android exception scanning for beacons
    java.lang.NullPointerException
            at android.os.Parcel.readException(Parcel.java:1471)
            at android.os.Parcel.readException(Parcel.java:1419)
            at android.bluetooth.IBluetoothGatt$Stub$Proxy.stopScan(IBluetoothGatt.java:771)
            at android.bluetooth.BluetoothAdapter$GattCallbackWrapper.stopLeScan(BluetoothAdapter.java:1819)
            at android.bluetooth.BluetoothAdapter.stopLeScan(BluetoothAdapter.java:1722)
            at org.altbeacon.beacon.service.scanner.CycledLeScannerForJellyBeanMr2.finishScan(CycledLeScannerForJellyBeanMr2.java:67)
            at org.altbeacon.beacon.service.scanner.CycledLeScanner.finishScanCycle(CycledLeScanner.java:249)
            at org.altbeacon.beacon.service.scanner.CycledLeScanner.scheduleScanCycleStop(CycledLeScanner.java:234)
            at org.altbeacon.beacon.service.scanner.CycledLeScanner$1.run(CycledLeScanner.java:230)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
02-06 20:25:29.936  19482-19482/com.myapp D/BluetoothAdapter﹕ stopLeScan()
02-06 20:25:29.936  19482-19482/com.myapp D/BluetoothAdapter﹕ startLeScan(): null
02-06 20:25:29.956  19482-19493/com.myapp D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=8
02-06 20:25:31.087  19482-19482/com.myapp D/BluetoothAdapter﹕ stopLeScan()
02-06 20:25:31.097  19482-19482/com.myapp W/CycledLeScanner﹕ Internal Android exception scanning for beacons
    java.lang.NullPointerException
            at android.os.Parcel.readException(Parcel.java:1471)
            at android.os.Parcel.readException(Parcel.java:1419)
            at android.bluetooth.IBluetoothGatt$Stub$Proxy.stopScan(IBluetoothGatt.java:771)
            at android.bluetooth.BluetoothAdapter$GattCallbackWrapper.stopLeScan(BluetoothAdapter.java:1819)
            at android.bluetooth.BluetoothAdapter.stopLeScan(BluetoothAdapter.java:1722)
            at org.altbeacon.beacon.service.scanner.CycledLeScannerForJellyBeanMr2.finishScan(CycledLeScannerForJellyBeanMr2.java:67)
            at org.altbeacon.beacon.service.scanner.CycledLeScanner.finishScanCycle(CycledLeScanner.java:249)
            at org.altbeacon.beacon.service.scanner.CycledLeScanner.scheduleScanCycleStop(CycledLeScanner.java:234)
            at org.altbeacon.beacon.service.scanner.CycledLeScanner$1.run(CycledLeScanner.java:230)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
02-06 20:25:32.218  19482-19482/com.myapp D/BluetoothAdapter﹕ stopLeScan()
02-06 20:25:32.218  19482-19482/com.myapp D/BluetoothAdapter﹕ startLeScan(): null
02-06 20:25:32.228  19482-19493/com.myapp D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=9
02-06 20:25:33.360  19482-19482/com.myapp D/BluetoothAdapter﹕ stopLeScan()
02-06 20:25:33.370  19482-19482/com.myapp W/CycledLeScanner﹕ Internal Android exception scanning for beacons
    java.lang.NullPointerException
            at android.os.Parcel.readException(Parcel.java:1471)
            at android.os.Parcel.readException(Parcel.java:1419)
            at android.bluetooth.IBluetoothGatt$Stub$Proxy.stopScan(IBluetoothGatt.java:771)
            at android.bluetooth.BluetoothAdapter$GattCallbackWrapper.stopLeScan(BluetoothAdapter.java:1819)
            at android.bluetooth.BluetoothAdapter.stopLeScan(BluetoothAdapter.java:1722)
            at org.altbeacon.beacon.service.scanner.CycledLeScannerForJellyBeanMr2.finishScan(CycledLeScannerForJellyBeanMr2.java:67)
            at org.altbeacon.beacon.service.scanner.CycledLeScanner.finishScanCycle(CycledLeScanner.java:249)
            at org.altbeacon.beacon.service.scanner.CycledLeScanner.scheduleScanCycleStop(CycledLeScanner.java:234)
            at org.altbeacon.beacon.service.scanner.CycledLeScanner$1.run(CycledLeScanner.java:230)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
02-06 20:25:34.521  19482-19482/com.myapp D/BluetoothAdapter﹕ stopLeScan()
02-06 20:25:34.521  19482-19482/com.myapp D/BluetoothAdapter﹕ startLeScan(): null
02-06 20:25:34.551  19482-19493/com.myapp D/BluetoothAdapter﹕ onClientRegistered() - status=133 clientIf=0
02-06 20:25:35.652  19482-19482/com.myapp D/BluetoothAdapter﹕ stopLeScan()

0 个答案:

没有答案