即使使用前台服务,测距和监视也会在一段时间后停止

时间:2018-10-03 10:50:51

标签: java android android-studio ibeacon altbeacon

我正在使用altbeacon库扫描信标。我正在使用Android 8.1 OnePlus 3T。即使我使用前台服务,测距和监视也会在5分钟后停止。 我可以看到有效的通知,但检测无法正常工作。

这是我的代码

这是主要的应用程序类

    @Override
      public void onCreate() {
          super.onCreate();
          SoLoader.init(this, /* native exopackage */ false);
          startBeacon();
          Notification.Builder builder = new Notification.Builder(this);
          builder.setSmallIcon(R.mipmap.ic_launcher);
          builder.setContentTitle("Scanning for Beacons");
          Intent intent = new Intent(this, MainApplication.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(
                  this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
          );


          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              NotificationChannel channel = new NotificationChannel(NotificationChannelId, "Beacons Scanning",
                      NotificationManager.IMPORTANCE_DEFAULT);
              channel.setShowBadge(true);
              channel.setSound(null, null);
              channel.setLightColor(Color.BLUE);
              NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
              assert mNotificationManager != null;
              mNotificationManager.createNotificationChannel(channel);
              builder = new Notification.Builder(this,
                      NotificationChannelId);
          } else {
              builder = new Notification.Builder(this);
          }

          builder.setContentIntent(pendingIntent);
          beaconManager.enableForegroundServiceScanning(builder.build(), 456);
          beaconManager.setEnableScheduledScanJobs(false);
          beaconManager.setBackgroundBetweenScanPeriod(0);
          beaconManager.setBackgroundScanPeriod(1100);

          Log.d(TAG, "setting up background monitoring for beacons and power saving");
          // wake up the app when a beacon is seen
          Region region = new Region("backgroundRegion",
                  null, null, null);

          regionBootstrap = new RegionBootstrap(this, region);
          backgroundPowerSaver = new BackgroundPowerSaver(this);
          Intent MyIntent = new Intent(this, MainActivity.class);
          this.startActivity(MyIntent);

      }

      private void startBeacon() {
          beaconManager = BeaconManager.getInstanceForApplication(getApplicationContext());
          //BeaconManager.setRegionExitPeriod(5000); //5 seconds

          beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(ALTBEACON));
          beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(EDDYSTONE_TLM));
          beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(EDDYSTONE_UID));
          beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(EDDYSTONE_URL));
          beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(IBEACON));
      }

    @Override
    public void didEnterRegion(Region region) {
      System.out.println("did enter region");
        if (!beaconManager.isBound(this)) {
            beaconManager.bind(this);
        }
        try {
            beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getNewandOldBeacons(MainApplication.ListofBeacons, "Entry");
            }
        }, 1000);
    }

    @Override
    public void didExitRegion(Region region) {
      System.out.println("did exit region");

        try {
            beaconManager.stopRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        getNewandOldBeacons(MainApplication.ListofBeacons, "Exit");

        if(beaconManager.isBound(this)) {
            beaconManager.unbind(this);
        }
    }

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

    }

      @Override
      public void onBeaconServiceConnect() {
          beaconManager.setRangeNotifier(new RangeNotifier() {
              @Override
              public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                  if (beacons.size() > 0) {
                      MainApplication.ListofBeacons = beacons;
                  }
              }
          });
      }

在MainActivity中,我只是要求获得ACCESS_COURSE_LOCATION和ACCESS_FINE_LOCATION的权限。该应用程序位于React Native中,这是android本机代码。

1 个答案:

答案 0 :(得分:0)

基于脱机提供的日志,这似乎是由OnePlus蓝牙堆栈中的Qualcomm驱动程序或固件问题引起的。如下日志摘录所示:

10-08 16:01:04.710 22646-22666/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 240) 10-08 16:01:04.711 22646-22666/? E/BtGatt.GattService: Exception: android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died 10-08 16:01:04.718 781-781/? I/vendor.qti.bluetooth@1.0-ibs_handler: DeviceWakeUp: Writing IBS_WAKE_IND

不幸的是,您可能无法采取任何措施来防止这种情况。当手机进入此状态时,您可以尝试关闭蓝牙然后再打开,以查看是否清除了这种情况(这相当于“重置”)。如果是这样,那么当问题出现时,您也许可以使用Android Beacon Library的BluetoothMedic类自动关闭/打开蓝牙。但这显然是一个不完美的解决方案。