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