I try to scan the nearby Bluetooth device in background mode, it is not working on some devices like android 11+.
here is my sample code, working in the foreground very well
// granted all permission
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!--BLUETOOTH PERMISSION-->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- Needed only if your app looks for Bluetooth devices.
If your app doesn't use Bluetooth scan results to derive physical
location information, you can strongly assert that your app
doesn't derive physical location. -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<!-- Needed only if your app makes the device discoverable to Bluetooth
devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<!-- Needed only if your app communicates with already-paired Bluetooth
devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<!--bibo01 : hardware option-->
<uses-feature android:name="android.hardware.bluetooth" android:required="false"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>
// for Scanning Ble Device in api version above 21
private var settings: ScanSettings? = null
private val scanFilters = java.util.ArrayList<ScanFilter>()
private var mLEScanner: BluetoothLeScanner? = null
private var adapter: BluetoothAdapter? = null
val adapterBlu = applicationContextNew.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
adapter = adapterBlu.adapter
mLEScanner = adapter!!.bluetoothLeScanner
settings = ScanSettings.Builder()
//.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
.build()
//scan specified devices only with ScanFilter
val scanFilter = ScanFilter.Builder()
//.setDeviceName("BIBO 1")
.build()
scanFilters.add(scanFilter)
mLEScanner = adapter!!.bluetoothLeScanner
settings = ScanSettings.Builder()
.build()
//scan specified devices only with ScanFilter
val scanFilter = ScanFilter.Builder()
.build()
scanFilters.add(scanFilter)
// scan:
mLEScanner!!.startScan(scanFilters, settings, highScanCallback)
//high Scan callback
private val highScanCallbackClackmass =
object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
if (result.device.name != null && result.device.address != null){
if ("BIBO 1" == result.device.name.toString()) {
//globalDeviceAddress = result.device.address.toString()
}
}
}
}
also tried screen wake solution: (working below android 10)
val pm: PowerManager = getSystemService(POWER_SERVICE) as PowerManager
val isScreenOn: Boolean = pm.isInteractive // check if screen is on
if (!isScreenOn) {
val wl: PowerManager.WakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP, "myApp:notificationLock")
wl.acquire(9000) //set your time in milliseconds
}