0

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.

3 Answers3

3

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
      }
    }
}
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • This code requires app to be launched. It won't work after application force stop. https://altbeacon.github.io/android-beacon-library/resume-after-terminate.html See the last paragraph – Ufkoku Jun 14 '19 at 14:03
  • Don't confuse a "force stop" will killing the app with the task switcher. On all standard Android implementations, killing an app with the task switcher does NOT put the app into a stopped state, so you can continue to detect BLE after killing from the task switcher. The referenced document in the above comment (written by me, BTW) explicitly states this in points 2 and 5. If the user does go into settings to force stop, then yes, the app can never run again without a manual launch. – davidgyoung Jun 14 '19 at 16:35
  • @davidgyoung, I have tried the above approach. The problem I see is that when the device is in locked state, the BLE signals aren't detecting, When I just tap on the screen the ble signals are coming. Can we do something to wakeup app and send detect ble signals when the app sees the ble signals? – Praveen Kondapalli Jul 10 '19 at 14:48
  • I do not know for sure what you mean by a "locked state". Do you mean the screen. Is locked? If so. This is a completely different issue that this ona about killing the app. It may be worthy of its own question. If you decide to create a new question and link to it from here, be sure to include your Android hardware vendor, as that affects the answer. You may also want to include any OS-level log statements about scans being blocked when the phone is in this state – davidgyoung Jul 10 '19 at 19:21
2

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.

Ufkoku
  • 2,384
  • 20
  • 44
1

You can use Geofence to wake the app. Geofence will survive 'Force stop' of the application.