简体   繁体   English

蓝牙 LE - 如何设置自定义扫描延迟?

[英]Bluetooth LE - How to set custom scan delay?

I am making an app to scan all tags using Bluetooth LE.我正在制作一个使用蓝牙 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?我尝试实施 TimerTask,但它仍然在一秒钟内扫描多个设备 有什么方法可以实施间隔,例如每秒或更长时间扫描一个标签?

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(){
            @RequiresApi(Build.VERSION_CODES.N)
            @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(result.device.name ?: "Unbekannt", result.device.address, result.rssi , result.scanRecord?.bytes, "")
                        deviceList.add(newTag)
                        newTag
                    }
                    tag.name = result.device.name ?: "Unbekannt"
                    tag.rssi = result.rssi
                    tag.advertisementData = result.scanRecord?.bytes
                }
    
                deviceList.sortBy {result.rssi }
    
                menu.findItem(R.id.count).title = "Geräte: " + deviceList.size
    
                super.onScanResult(callbackType, result)
            }
    
            override fun onScanFailed(errorCode: Int) {
                super.onScanFailed(errorCode)
                Log.e("Scan failed","")
            }
        }
    
    
    
        @SuppressLint("MissingPermission")
        @RequiresApi(Build.VERSION_CODES.M)
        override fun onCreate(savedInstanceState: Bundle?) {
    
            super.onCreate(savedInstanceState)
    
            tagBinding = ListbluethoothBinding.inflate(layoutInflater)
            val view = tagBinding.root
            setContentView(view)
    
            recyclerView = tagBinding.list
    
            val layoutManager = LinearLayoutManager(this)
            recyclerView.layoutManager = layoutManager
            recyclerView.adapter = RecyclerViewAdapter(deviceList)
    
            addMenuProvider(this)
    
            val bluetoothManager: BluetoothManager = getSystemService(BluetoothManager::class.java)
            val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.adapter
            bluetoothLeScanner = bluetoothAdapter?.bluetoothLeScanner
    
            interval = getSharedPreferences("interval", MODE_PRIVATE).getLong("interval", 1000)
    
            bluetoothLeScanner!!.startScan(scanCallback)
    
        }
    
    
        override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
            MainActivity@this.menu = menu
            menuInflater.inflate(R.menu.items, menu)
        }
    
        @RequiresApi(Build.VERSION_CODES.M)
        @SuppressLint("MissingPermission")
        override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
    
            when(menuItem.itemId) {
    
                R.id.stop -> {
                    bluetoothLeScanner!!.stopScan(scanCallback)
                    Snackbar.make(findViewById(android.R.id.content),"Scan gestoppt", 2000).show()
                }
                R.id.start -> {
                    bluetoothLeScanner!!.startScan(scanCallback)
                    Snackbar.make(findViewById(android.R.id.content),"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 .不幸的是 Android 没有给你这个粒度,值是在源代码中设置的,可以在这里看到。 In terms of setting it via an API, the only thing you can do is set SCAN_MODE_LOW_LATENCY via the ScanSettings class .在通过 API 进行设置方面,您唯一可以做的就是通过ScanSettings class 设置 SCAN_MODE_LOW_LATENCY

Some people have found a few workarounds around this which you can find in the links below:-有些人已经找到了一些解决方法,您可以在下面的链接中找到这些方法:-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM