0

I am working in a application where we need to make some bluetooth scans while the screen is turned off. I set up the receivers using a foreground service that runs every minute using the AlarmManager class. The thing is that with my Oneplus 6 (Android version 9.0), it doesn't run with the screen off but when the screen is on everything works perfectly fine.

Do you know if it is something related with the Android version?

If you need more part of the code just tell me.

Thanks!

Here is where I register the receiver


public void registerReceivers(Context context) {

            IntentFilter intentFilter = new IntentFilter();

            intentFilter.addAction(BluetoothDevice.ACTION_FOUND);                         

            intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

            context.registerReceiver(btReceiver,intentFilter);                      

}


Then here is how I start the discover after I register the receiver


public void startScan(Context context){

    registerReceivers(context)

    btReceiver.startScan(context);

}


Where btReceiver is the following object


public class BtReceiver extends BroadcastReceiver {

    

    //BtConn is an object that store some information about the founded device

    public final List<BtConn> btConns;

    public  BluetoothAdapter bluetoothAdapter;


    public BtReceiver() {

        super(ScanType.BLUETOOTH);

        this.btConns = new ArrayList<>();

        this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    }


    @Override

    public void onReceive(Context context, Intent intent) {

        String action =intent.getAction();

        //Case when a device has been found

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {

            //Getting device values

            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            String deviceName = device.getName();

            String macAddress = device.getAddress();

            int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);

            //Checking if it is a Phone by default is True

            boolean isMobile = true;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {

                isMobile = BluetoothDevice.DEVICE_TYPE_CLASSIC == device.getType();;

            }

            //adding object to list

            if(deviceName != null && deviceName.length() > 0) { btConns.add(new BtConn(deviceName,rssi, macAddress,isMobile)); }

            return;

        }


        if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { scanFinished(context);return; }


    }


    public void startScan(Context context) {

        this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        bluetoothAdapter.startDiscovery();

    }


    public void scanFinished(context) {

        //Here there is some stuff to upload the information to the database

    }


}


Then in the onStartCommand of my Service I run:



public int onStartCommand(Intent intent, int flags, int startId) {

    startForeground(ID_NOTIFICATION, myCustomNotification()));    

    startScan(this);

    return START_STICKY;

}    

[EDIT]

I have tried, using handlers instead the AlarmManager but it has the same behaviour. When the screen is off, once I call the startDiscovery method it doesn't seem to be working. Also, instead of using the discovery method I have tested with the BLE scan. This kind of scan works when the screen goes off, but I want to find other Android devices so is useless...

  • 1
    Take into accoint android 9 introduces some changes in power management, maybe it affects bluetooth connectivity https://developer.android.com/about/versions/pie/power – Ignacio Tomas Crespo Apr 17 '19 at 10:37
  • I don't really know if it is something related with the new battery buckets. The thing is that I also have a receiver to get the Wifi beacons near the phone and it is fully working... – Hugo Ramon Apr 25 '19 at 09:55

1 Answers1

0

You may be experiencing your AlarmManager being suspended when the Android goes into Doze mode. Android 6 introduced Doze for screen off motionless phones and Android 7 introduced a light Doze mode that starts just a few seconds after the screen going off even if the phone is not motionless (e.g. in your pocket).

If this is the issue, the best way to avoid it is to stop using the AlarmManager for timed processing. Consider using a Handler with postDelayed, which is not affected by Doze. If you must use AlarmManager, see here for possible kludges to force them to fire. The Android Beacon Library, for example, uses Handlers to cycle scans even in Doze mode.

OnePlus also has some proprietary battery saving features which will ultimately kill your foreground service after extended app running time. But it sounds like that is not the problem you are facing just yet..

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • The AlarmManger seems to be working fine, I print some text (using Log) everytime the alarm is fired and it works either with the screen on or off. I will take a look to the Android Beacon Library, to see how the scans are performed. Thanks for the response! – Hugo Ramon Apr 25 '19 at 13:52