2

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
                }
Bolt UIX
  • 5,988
  • 6
  • 31
  • 58
  • I enable "show on lock screen" permission on Xiaomi phones, now it is working android 11 : https://i.stack.imgur.com/f6nUn.png – Bolt UIX Nov 05 '21 at 12:19

2 Answers2

1

You also need to add ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions. Have a look at the links below for more information:-

Youssif Saeed
  • 11,789
  • 4
  • 44
  • 72
1

Battery restrictions can also affect BLE work in background (noticed on my device, but it was Android 10 at that time).

Settings => Apps => Manage Apps => Your App => Battery saver => set to "No restrictions".

In my case, this was necessary to make the app scan when dislay is off.

alexander.cpp
  • 747
  • 6
  • 15
  • i tested this on 2 phones, working in android 10, but not working in android 11 – Bolt UIX Nov 05 '21 at 11:58
  • I enable "show on lock screen" permission on Xiaomi phones, now it is working android 11 : https://i.stack.imgur.com/f6nUn.png – Bolt UIX Nov 05 '21 at 12:19