- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用很棒的 rxandroidble 库进行 BLE 控制。
我保持 Activity 之间的联系。在开始扫描之前,我想先断开所有连接的设备。如果有很多连接,有时它不起作用。这是我正在使用的解决方案:
public void doScan() {
if (isScanning()) return;
// disconnect all connected devices first:
while(BleController.getDefault().getDisconnectTriggerSubject().hasObservers()){
BleController.getDefault().getDisconnectTriggerSubject().onNext(null);
}
scanSubscription = rxBleClient.scanBleDevices(
new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.build(),
new ScanFilter.Builder()
// add custom filters if needed
.build()
)
.filter(rxBleScanResult -> !TextUtils.isEmpty(rxBleScanResult.getBleDevice().getName()))
.observeOn(AndroidSchedulers.mainThread())
.doOnUnsubscribe(this::clearSubscription)
.subscribe(resultsAdapter::addScanResult, this::onScanFailure);
updateButtonUIState();
}
BleController
使用主应用程序的上下文进行初始化并保留 connectionObservable
, disconnectTriggerSubject
, rxBleClient
.
什么是更好的解决方案?任何帮助将不胜感激!
最佳答案
从您的帖子中我可以看出您将 BLE 扫描/连接逻辑与 UI/Activity 逻辑混合在一起。这可能是正确管理连接的问题。
您可以做的是将所有 BLE 逻辑放入您的 BleController
它已经有了一个好名字,但在你的情况下似乎是一个 BleObjectsContainer
.
例如,您只能从 BleController
中公开只有以 Activities
的方式满足您的特定用例的可观察对象不需要处理。即你的 BleController
可以处理扫描:
private final BehaviorRelay<Boolean> isScanningPublishRelay = BehaviorRelay.create(false); // a relay (that cannot emit an error) that emits when a scan is ongoing
private Observable<ScanResult> scanDevicesWithNonNullNameObs = rxBleClient.scanBleDevices(new ScanSettings.Builder().build())
.filter(scanResult -> !TextUtils.isEmpty(scanResult.getBleDevice().getName()))
.doOnSubscribe(() -> isScanningPublishRelay.call(true)) // when scan is subscribed/started emit true
.doOnUnsubscribe(() -> isScanningPublishRelay.call(false)) // when scan is stopped emit false
.subscribeOn(AndroidSchedulers.mainThread()) // the above emissions will happen on the same thread. should be serialized
.unsubscribeOn(AndroidSchedulers.mainThread()) // the above emissions will happen on the same thread. should be serialized
.share(); // share this observable so no matter how many places in the code will subscribe the scan is started once and `isScanningPublishRelay` is called once
public Observable<ScanResult> scanDevicesWithNonNullName() { // getter for the scan observable
return scanDevicesWithNonNullNameObs;
}
除了扫描之外,它还会处理每个 Activity
的特定用例。需要它:
class ScanInProgress extends Throwable {
// ...
}
public Observable<YourActivityUseCaseModel> doYourSpecificStuff(Observable<RxBleConnection> connectionObservable) {
return Observable.merge(
connectionObservable,
isScanningPublishRelay
.takeFirst(aBoolean -> aBoolean)
.flatMap(ignored -> Observable.error(new ScanInProgress())) // this will only emit an error when a scan is ongoing
)
.flatMap(...); // do the rest of your stuff
}
通过这种方式,在您的 Activity 中,您只需订阅他们需要的任何模型,并在一个专门用于它的地方处理 BLE (BleController
)。
在上面的示例中,您需要提供 Observable<RxBleConnection>
但它可以通过许多不同的方式实现,并且可以在 BleController
中进行管理也不会暴露在界面中。
关于android - RxAndroidBle - 如何断开所有连接的设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48272987/
我正在尝试使用在 Android 手机和使用 Android Things 的 Raspberry Pi 上运行的 rxBleAndroid 与 BLE 数据记录器/传感器进行通信。 但是,我目前遇到
我说英语有点抱歉。我需要你的帮助,因为我投入了很多时间,但我无法完成最后一个细节:_( 我必须将带有蓝牙 LE 的手机连接到继电器,才能打开或关闭门。提供原生蓝牙和 socket ,但没有办法做任何有
我是 RxJava 的新手。我现在尝试 RxAndroidBle 并将其与 BLE 的 Android API 实现进行比较。我的外围设备一次发送大量通知(512b block 中大约 30kB)。在
我正在使用 RxAndroidBle 库: 此代码按预期正常工作,单击 UI 上的“连接”按钮后,它建立了连接。 当我想要在设备返回范围内时自动连接到设备时出现问题。我不想再次点击“连接”按钮。 是否
我正在使用很棒的 rxandroidble 库进行 BLE 控制。 我保持 Activity 之间的联系。在开始扫描之前,我想先断开所有连接的设备。如果有很多连接,有时它不起作用。这是我正在使用的解决
我正在连接到一个已发现的设备并定期读取 RSSI。我需要确切知道连接何时丢失。有什么方法可以指定连接超时,之后我将收到 RxBleConnectionState.DISCONNECTED?现在,即使连
我正在使用 Polidea's RxAndroidBle library与我的 Android 应用程序中的设备通信。 我是响应式编程的新手,所以我无法弄清楚如何执行以下操作: 在一个特征(特征 A)
我需要帮助。当我使用 rxandroidble 时如何设置扫描超时,例如 10 秒。 scanDisposable = rxBleClient.scanBleDevices(
我目前正在尝试使用 rxandroidble 来替换我们其中一款应用的 Android 原生 BLE API。 如何禁用通知?我可以使用示例代码启用它,这个: device.establishConn
我是 Rx 编程的新手。我现在正在尝试使用 RxAndroidBle 来发现 BLE 设备的服务并从设备中读取一些特征。 我可以用 device.establishConnection(false)
我正在连接的 BLE 设备在其 GATT 特征之一上发出字节以响应对特征的写入。客户端应该启用该特征的通知,并解释该特征的更改字节。 (我正在控制的行为是打开附近无线网络的扫描服务,然后监听服务输出。
我在设置多个特征的通知时遇到问题。我查看了文档,发现许多示例仅涵盖非常细化的情况。 我的用例如下:1.扫描设备2. 用户选择要连接的设备(连接持续到应用程序关闭)3.订阅多种特性的通知4. 一次读/写
我正在尝试使用 RxAndroidBle 设置通知,但是当与外围设备的连接丢失时,我需要重新创建我设置的通知。我正在创建这样的通知: connectionObservable
我目前正在开发一个 xplattform Android/iOS 应用程序并评估使用 RxBluetoothKit 的 iOS 设备是否可以与运行 RxAndroidBle 的 Android 设备通
我目前正在使用 RxAndroidBle 将固件写入蓝牙设备。它的工作方式我必须将数据 block (每 block 580 字节)发送到一个 WRITE_CHARACTERISTIC_UUID 上的
我想知道 RxAndroidBle 库使用哪种策略来写确认:WRITE_TYPE_DEFAULT或 WRITE_TYPE_NO_RESPONSE ? 此外,如果我想设置(或查询)这个设置,在 RxAn
我正在尝试实现这个写入/通知处理示例 (Using RxAndroidBle, how do I subscribe to responses from writing to a characteri
在我的应用程序中,我使用的是 RxAndroidBLE 库。感谢很棒的图书馆。但我有一些问题。我正在尝试使用 连接到 BLE 设备 rxBleDevice.establishConnection(fa
当我未连接到我的设备时,我需要永久扫描ble广告以找到它并知道何时连接到它(特定产品)。此扫描是在前台服务中实现的,以匹配 8.0 的先决条件。 为了节省电量,我想定期扫描(同时遵守 Android
我正在使用 rxandroidble 使用 autoconnect = true 来持续监控来自传感器的数据。该应用会持续扫描它之前连接过的传感器。 即使手机未连接电源,传感器的数据监控和扫描也应持续
我是一名优秀的程序员,十分优秀!