简体   繁体   中英

BLE Scan not working in Background with Scanfilters in android pie?

I am using blescan with scanfilters to detect beacons it's working very fine in foreground and background up to oreo version but when it comes to android pie it's not able to send pending broadcast in background.

  ScanSettings settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build();
            final List<ScanFilter> scanFilters = new ArrayList<>();
            scanFilters.add(getScanFilter());

            BluetoothAdapter bluetoothAdapter;
            final BluetoothManager bluetoothManager =   
                    (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
            bluetoothAdapter = bluetoothManager.getAdapter();
            Intent intent = new Intent(this.getApplicationContext(), MyBroadcastReceiver.class);
            intent.putExtra("o-scan", true);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            bluetoothAdapter.getBluetoothLeScanner().startScan(scanFilters, settings, pendingIntent);




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


Android 9 introduces several behavior changes, such as limiting background apps' access to device sensors and Wi-Fi scans.

These changes affect all apps running on Android 9, regardless of target SDK version.

Sensors that use the continuous reporting mode , such as accelerometers and gyroscopes, don't receive events.


Android 9 Limited access to sensors in background:

Android 9 limits the ability for background apps to access user input and sensor data. If your app is running in the background on a device running Android 9, the system applies the following restrictions to your app:

Sensors that use the continuous reporting mode, such as accelerometers and gyroscopes, don't receive events.

Sensors that use the on-change or one-shot reporting modes don't receive events .


Solution: If your app needs to detect sensor events on devices running Android 9 while the app is in the background, use a foreground service.

I an example test Android app using Oreo (API 26) and the the code above (slightly modified) to detect beacons. I am using the Pixel 3 XL (with Pie). I think that the hard part about this is to know for sure if the code in onRecieve() in MyBroadcastReceiver is actually being run upon detection of a beacon when the device is running on battery only (disconnected from Android-studio and Logcat (USB)).

Using Volley (com.android.volley) to submit a HTTP request to a local http server, I was able to demonstrate that it works as documented - ie. I am able to receive the http request when beacon(s) are detected. However, Volley only sends these these requests when Android is awake or when it periodically wakes up and connects to the network - which in my simple tests was about every 15 minutes (plus some variation), but I did get all the beacon ScanResults on my HTTP server, just in delayed up to 15 minutes. I was even able to remove the app from the list of running apps (you know; swiping up to remove the app) and still see that the onRecieve() in MyBroadcastReceiver was receiving BLE ScanResults.

How do you know that the onRecieve() in MyBroadcastReceiver is being killed? I am very interested to know how you know this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM