首页 > 解决方案 > 使用 BLE 扫描扫描过滤器

问题描述

我研究了一个使用 BLE 扫描到前台服务的应用程序。我使用库 RxBle Android 来执行扫描。</p>

private fun scanBleDevices(): Observable<ScanResult>? {
    val rxBleClient = RxBleClient.create(this)
    val scanSettings = ScanSettings.Builder()
        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
        .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
        .build()

    val scanFilter = ScanFilter.Builder()
        //.setServiceData(ParcelUuid.fromString("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"), ByteArray(1))
        //.setDeviceAddress("XX:XX:XX:XX:XX:XX")
        .build()

    return rxBleClient.scanBleDevices(scanSettings, scanFilter)
}

private fun startScan(){
    Timber.e("Start SCAN")
    scanBleDevices()?.let {
        it.observeOn(AndroidSchedulers.mainThread())
            .subscribe({ scanResult ->
                //Handle scan result
            }, {
                Timber.e("Scan failed")
            })
    }
}

当我的手机屏幕被锁定时,我使用 ScanFilter 来获得扫描结果。





当我使用带有 mac 地址的 ScanFilter 时效果很好,但这不是一个真正的解决方案,因为我不想为我的所有信标指定所有 mac 地址。</p>

所以我尝试使用带有服务数据 ID 的 ScanFilter,除非我的手机屏幕被锁定,否则它可以工作。问题只是当我将我的应用程序与三星设备一起使用时,因为我有一个 Pixel 3 并且我没有这个问题。

你有解释或解决方案吗?</p>

谢谢 !

标签: androidkotlinbluetooth-lowenergysamsung-mobile

解决方案


推荐阅读