Can we wakeup the Android app via bluetooth or BLE signal from a device - Android 8.0 and above

◇◆丶佛笑我妖孽 提交于 2019-12-10 12:21:58

问题


I am trying to wakeup my android app via nearby bluetooth devices. If I force close the app (in Android 8.0 and above), and when I bring my Android device near to a BLE device, can I get a callback or intent callback so that I can push a ForeGround service and make the app stay awake.

I tried scanning for the BLE devices nearby but when the app is force killed, the BLE scan stops and I cannot wake up the app via BLE nearby devices.


回答1:


Force stop kills application totally. It won't get FCMs, alarms in AlarmManager are removed too, etc. So app processes are totally killed, and any info is removed.




回答2:


Yes. On Android 8+ you can use an Intent-based scan tied to a BroadcastReceiver to wake up an app based on a BLE advertisement detection. The BroadcastReceiver will only be allowed to run for a few seconds, but you can use this time to start an immediate JobService that can run for up to 10 minutes. This is exactly what the Android Beacon Library does out-of-the-box to allow background detections. You may also be able to use a foreground service to run longer than 10 minutes in the background. Read more about the options 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);

The above code will set an Intent to fire that will trigger a call to a class called MyBroadcastReceiver when a matching bluetooth device is detected. You can then fetch the scan data like this:

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
      }
    }
}


来源:https://stackoverflow.com/questions/56599355/can-we-wakeup-the-android-app-via-bluetooth-or-ble-signal-from-a-device-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!