gpt4 book ai didi

android - 在 Polidea 中记录 scanResult - RxAndroidBle 扫描

转载 作者:行者123 更新时间:2023-11-29 01:15:38 31 4
gpt4 key购买 nike

我正在尝试使用 RxAndroidBle 库 ( https://github.com/Polidea/RxAndroidBle )。我希望该应用程序启动并扫描 BLE 设备。我想在 LogCat 中打印找到的设备。我该怎么做?

RxBleClient rxBleClient;
RxBleScanResult rxBleScanResult;
private Subscription scanSubscription;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rxBleClient = RxBleClient.create(this);
Subscription scanSubscription = rxBleClient.scanBleDevices().subscribe(
rxBleScanResult.getBleDevice().getMacAddress());
}

最佳答案

来自 http://polidea.github.io/RxAndroidBle/ .

Subscription scanSubscription = rxBleClient.scanBleDevices().subscribe(
rxBleScanResult -> {
// Process scan result here.
Log.e("MainActivity","FOUND :"+ rxBleScanResult.getBleDevice().getName());
},
throwable -> {
// Handle an error here.
}
);

// When done, just unsubscribe.
scanSubscription.unsubscribe();

编辑:我注意到,这会破坏扫描。甚至像比较 if BleScanResult.getBleDevice().getName().equals("BleName")打破扫描。它只返回 3 或 5 个设备,然后没有其他的。

编辑 2:我将保留之前的编辑。可能有人会遇到同样的问题。某些手机​​(LG G4 Android 6)对于某些蓝牙设备返回 null。但是其他一些(Samsung J5 Android 6)不返回空值。这就是让我在不同地方寻找错误的原因。但它很简单,只需添加

if(BleScanResult.getBleDevice().getName()!=null) 

现在它不再中断扫描了。

关于android - 在 Polidea 中记录 scanResult - RxAndroidBle 扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39880516/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com