Skip to content

Bluetooth LE – How to set custom scan delay?

I am making an app to scan all tags using Bluetooth LE. I have to implement a interval feature where an user can enter seconds for scan delay, but i have not found any method to do so. I tried implementing TimerTask but it still scans more than one device in a second Is there any way to implement a interval such as scanning one tag every second or more?

My code:

class BluetoothActivity : AppCompatActivity(), MenuProvider{
        private lateinit var recyclerView: RecyclerView
        private var bluetoothLeScanner: BluetoothLeScanner? = null
        private val deviceList = mutableListOf<BleTag>()
        private val deviceMap = mutableMapOf<String, BleTag>()
        private var interval: Long = 0
        private lateinit var menu: Menu
        private lateinit var tagBinding: ListbluethoothBinding
        val before = DateFormat.getDateTimeInstance().format(Date(System.currentTimeMillis()))
        private val scanCallback = object : ScanCallback(){
            @SuppressLint("MissingPermission", "NotifyDataSetChanged")
            override fun onScanResult(callbackType: Int, result: ScanResult) {
               val scanJob = CoroutineScope(Dispatchers.Main).launch {
                    val tag = deviceMap.computeIfAbsent(result.device.address) {
                        val newTag = BleTag( ?: "Unbekannt", result.device.address, result.rssi , result.scanRecord?.bytes, "")
           = ?: "Unbekannt"
                    tag.rssi = result.rssi
                    tag.advertisementData = result.scanRecord?.bytes
                deviceList.sortBy {result.rssi }
                menu.findItem( = "Geräte: " + deviceList.size
                super.onScanResult(callbackType, result)
            override fun onScanFailed(errorCode: Int) {
                Log.e("Scan failed","")
        override fun onCreate(savedInstanceState: Bundle?) {
            tagBinding = ListbluethoothBinding.inflate(layoutInflater)
            val view = tagBinding.root
            recyclerView = tagBinding.list
            val layoutManager = LinearLayoutManager(this)
            recyclerView.layoutManager = layoutManager
            recyclerView.adapter = RecyclerViewAdapter(deviceList)
            val bluetoothManager: BluetoothManager = getSystemService(
            val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.adapter
            bluetoothLeScanner = bluetoothAdapter?.bluetoothLeScanner
            interval = getSharedPreferences("interval", MODE_PRIVATE).getLong("interval", 1000)
        override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
   = menu
            menuInflater.inflate(, menu)
        override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
            when(menuItem.itemId) {
       -> {
                    Snackbar.make(findViewById(,"Scan gestoppt", 2000).show()
       -> {
                    Snackbar.make(findViewById(,"Scan gestartet", 2000).show()
            return true



Unfortunately Android does not give you this granularity, and the values are set in the source code as can be seen here. In terms of setting it via an API, the only thing you can do is set SCAN_MODE_LOW_LATENCY via the ScanSettings class.

Some people have found a few workarounds around this which you can find in the links below:-

User contributions licensed under: CC BY-SA
1 People found this is helpful