[英]How to use device name in ble scan filter in Kotlin
我已經在這個上燒了幾個小時,似乎無法克服這一點。 我在舊的堆棧溢出帖子(大約 5 年前)中看到,對設備名稱的 ble 掃描過濾在 Android 中被破壞,因此您必須對每個返回的掃描結果手動執行此操作。 我希望它現在已經修復,我只是犯了一個容易被發現的新手錯誤。
我正在使用 Kotlin 執行 ble 掃描。 當過濾器為 null 時,我的代碼工作正常,但我無法使用設備名稱(廣告設備名稱)作為過濾器使其工作。 下面帶有過濾器的代碼可以編譯,但在我開始 ble 掃描時在我的 Nexus 7 平板電腦(Android 6.0.1)上崩潰(為了清晰起見,我只包含了我希望相關的內容)。
我正在使用以下代碼(都在 MainActivity.kt 中)為名為“RCTL003”的設備創建過濾器:
import android.bluetooth.le.ScanFilter as LeScanFilter
// After the class declaration for MainActivity
// Here's the scanner.
private val bleScanner by lazy {
bluetoothAdapter.bluetoothLeScanner
}
// Here's the device name "RCTL003"
private val filter = LeScanFilter.Builder().setDeviceName("RCTL003").build()
private lateinit var devfilters: MutableList<LeScanFilter>
...
// I init devfilters just before I start the scan in my startBleScan function
devfilters.add(filter)
bleScanner.startScan(devfilters, scanSettings, scanCallback)
bleScanner.startScan 采用 ScanFilter 類型的可變列表:
startScan((Mutable)List<ScanFilter!>!, ScanSettings!, ScanCallback!) defined in android.bluetooth.le
任何指針將不勝感激!
吉姆
通過更改 devFilters 的定義方式,按設備名稱掃描過濾器確實有效!
上面的 devFilters 應定義為:
private devfilters: MutableList<LeScanFilter> = ArrayList()
我使用的是 23 的 minSdk。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.