You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RxAndroidBle库scanBleDevices方法调用报错问题求助

Hey there! Let's break down the issues you're facing with RxAndroidBle 1.10.0 in Kotlin and fix them step by step.

First Issue: Error in Java-style scanBleDevices call

The core problem here is Kotlin doesn't use Java's new keyword for object instantiation. Your original code uses new ScanSettings.Builder() which is invalid in Kotlin. Here's the corrected version:

// Initialize RxBleClient correctly
val rxBleClient = RxBleClient.create(this)

// Build scan settings without the 'new' keyword
val scanSettings = ScanSettings.Builder()
    // Uncomment these if needed
    // .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
    // .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
    .build()

// Start scan - no filters added here, but you can add them later
val scanSubscription = rxBleClient.scanBleDevices(scanSettings)
    .subscribe(
        { scanResult ->
            // Process your scan result here
            println("Discovered device: ${scanResult.bleDevice.macAddress}")
        },
        { error ->
            // Handle scan errors here
            error.printStackTrace()
        }
    )

// Don't forget to dispose when you're done scanning
// scanSubscription.dispose()

Second Issue: Observable type error & scanBleDevices parameter mismatch

You're hitting two separate problems here:

  1. Observable type error: Make sure you're importing the correct RxJava 2 Observable (RxAndroidBle 1.x relies on RxJava 2). Double-check your imports include:

    import io.reactivex.Observable
    import com.polidea.rxandroidble2.scan.ScanResult
    

    Using the wrong Observable class (like RxJava 3 or Java's built-in Observable) will cause the "No type arguments expected" error.

  2. Parameter mismatch for scanBleDevices: The method expects ScanSettings plus a vararg ScanFilter (variable number of filters). In Kotlin, to pass a single filter to a vararg parameter, you need to use the spread operator (*). Here's the fixed function:

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

    val scanFilter = ScanFilter.Builder()
        // .setDeviceAddress("B4:99:4C:34:DC:8B") // Uncomment if filtering by address
        .build()

    // Use spread operator to pass the single filter to vararg parameter
    return rxBleClient.scanBleDevices(scanSettings, *arrayOf(scanFilter))
    
    // If you don't need any filters, you can simplify to:
    // return rxBleClient.scanBleDevices(scanSettings)
}

Quick Additional Check

Don't forget to ensure your app has the necessary BLE permissions in AndroidManifest.xml (and request runtime location permission for Android 6.0+):

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

内容的提问来源于stack exchange,提问作者Alejandro Gorgues

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:16:43