我第一次在kotlin上尝试使用该库,因为我读到要首先进行扫描,我需要使用
implementation "com.polidea.rxandroidble2:rxandroidble:1.10.0"
,创建一个RxBleClient并设置扫描参数,但是方法scanBleDevices()似乎有问题。
我尝试了出现在存储库描述中的代码以及kotlin示例中的代码。
使用描述中的代码,scanBleDevices上只有一个错误
rxBleClient = RxBleClient.create(this)
val scanSubscription = rxBleClient.scanBleDevices(
new ScanSettings.Builder()
// .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // change if needed
// .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // change if needed
.build()
// add filters if needed
)
.subscribe(
{
// Process scan result here.
},
{
// Handle an error here.
}
);
// When done, just dispose.
scanSubscription.dispose();
使用kotlin示例中的代码,Observable也出现错误,显示No type arguments expected for class Observable
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")
// add custom filters if needed
.build()
return rxBleClient.scanBleDevices(scanSettings, scanFilter)
}
方法scanBleDevices()出现问题,该方法提示警报,要求他们正确使用了scanSettings和scanFilter:
None of the following functions can be called with the arguments supplied.
scanBleDevices(ScanSettings!, vararg ScanFilter!) defined in com.polidea.rxandroidble2.RxBleClient
scanBleDevices(vararg UUID!) defined in com.polidea.rxandroidble2.RxBleClient
答案 0 :(得分:0)
如果编译器抱怨ScanSettings
和ScanFilter
,那么如果您从com.polidea.rxandroidble2
或android.bluetooth.le
(或类似)中导入这些类,请检查这些类的包
该库仅接受ScanSettings
包中的ScanFilter
和com.polidea.rxandroidble2
类。