• 4

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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<>();

            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 {
    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(
            // 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.

  • 1
Reply Report
      • 2
    • Thanks for your answer.I even created foreground service too after got to know about this there also I am facing same issue.When the app is going to be dead that time I am creating foreground service and starting scan for beacons using some library there it's behaving odd not even starting scan but if i turnoff bluetooth or location due broadcast receivers it's working fine.any idea how to resolve this issue??
      • 2
    • I created ble scanner app. I'm using, foreground service, intervals, setManufacturerData filter etc. First off all device with Android 9 retrieve 4-10x less frames than device with Android 6. Second when I minimize app app stops scan after about 60-90 seconds.

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.

  • 0
Reply Report