【问题标题】:Can we wakeup the Android app via bluetooth or BLE signal from a device - Android 8.0 and above我们可以通过来自设备的蓝牙或 BLE 信号唤醒 Android 应用吗 - Android 8.0 及更高版本
【发布时间】:2019-06-14 13:37:09
【问题描述】:

我正在尝试通过附近的蓝牙设备唤醒我的 Android 应用。如果我强制关闭应用程序(在 Android 8.0 及更高版本中),并且当我将我的 Android 设备靠近 BLE 设备时,我能否获得回调或意图回调,以便我可以推送 ForeGround 服务并使应用程序保持唤醒状态。

我尝试扫描附近的 BLE 设备,但当应用被强制终止时,BLE 扫描停止,我无法通过附近的 BLE 设备唤醒应用。

【问题讨论】:

    标签: android android-intent bluetooth ibeacon wakeup


    【解决方案1】:

    Force stop 完全杀死应用程序。它不会得到 FCM,AlarmManager 中的警报也被删除等等。所以应用程序进程被完全杀死,所有信息都被删除。

    【讨论】:

      【解决方案2】:

      是的。在 Android 8+ 上,您可以使用绑定到 BroadcastReceiver 的基于 Intent 的扫描来根据 BLE 广告检测唤醒应用程序。 BroadcastReceiver 只允许运行几秒钟,但您可以利用这段时间立即启动一个最多可以运行 10 分钟的 JobService。这正是Android Beacon Library 开箱即用以允许背景检测所做的事情。您还可以使用前台服务在后台运行超过 10 分钟。阅读更多关于选项here

      ScanSettings settings = (new ScanSettings.Builder().setScanMode(
                                      ScanSettings.SCAN_MODE_LOW_POWER)).build();
      // Make a scan filter matching the beacons I care about
      List<ScanFilter> filters = getScanFilters(); 
      BluetoothManager bluetoothManager =
                  (BluetoothManager) mContext.getApplicationContext()
                                             .getSystemService(Context.BLUETOOTH_SERVICE);
      BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
      Intent intent = new Intent(mContext, MyBroadcastReceiver.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent,
                                                               PendingIntent.FLAG_UPDATE_CURRENT);
      bluetoothAdapter.getBluetoothLeScanner().startScan(filters, settings, pendingIntent);
      

      上面的代码将设置一个 Intent 触发,当检测到匹配的蓝牙设备时,它会触发一个名为 MyBroadcastReceiver 的类的调用。然后您可以像这样获取扫描数据:

      public class MyBroadcastReceiver extends BroadcastReceiver {
          @Override
          public void onReceive(Context context, Intent intent) {
            int bleCallbackType = intent.getIntExtra(BluetoothLeScanner.EXTRA_CALLBACK_TYPE, -1);
            if (bleCallbackType != -1) {
              Log.d(TAG, "Passive background scan callback type: "+bleCallbackType);
              ArrayList<ScanResult> scanResults = intent.getParcelableArrayListExtra(
                                                     BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT);
              // Do something with your ScanResult list here.
              // These contain the data of your matching BLE advertising packets
            }
          }
      }
      

      【讨论】:

      • 此代码需要启动应用程序。应用强制停止后将不起作用。 altbeacon.github.io/android-beacon-library/…见最后一段
      • 不要混淆“强制停止”会杀死应用程序和任务切换器。在所有标准的 Android 实现中,使用任务切换器终止应用程序不会使应用程序进入停止状态,因此您可以在从任务切换器终止后继续检测 BLE。上述评论中引用的文档(由我编写,顺便说一句)在第 2 点和第 5 点中明确说明了这一点。如果用户确实进入设置强制停止,那么是的,如果没有手动启动,应用程序将永远无法再次运行。跨度>
      • @davidgyoung,我已经尝试过上述方法。我看到的问题是,当设备处于锁定状态时,没有检测到 BLE 信号,当我只是点击屏幕时,BLE 信号就来了。当应用看到 ble 信号时,我们可以做一些事情来唤醒应用并发送检测 ble 信号吗?
      • 我不确定您所说的“锁定状态”是什么意思。你说的是屏幕吧。锁住了?如果是这样的话。这是关于杀死应用程序的完全不同的问题。它可能值得自己提出问题。如果您决定创建一个新问题并从此处链接到它,请务必包括您的 Android 硬件供应商,因为这会影响答案。您可能还需要包含有关在手机处于此状态时阻止扫描的任何操作系统级别的日志语句
      【解决方案3】:

      您可以使用地理围栏唤醒应用。地理围栏将在应用程序的“强制停止”后继续存在。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-22
        • 1970-01-01
        • 1970-01-01
        • 2017-08-19
        相关资源
        最近更新 更多