- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
首先,我使用的是RxAndroidBLE库来管理我的 BLE 连接。
我有两个 SensorTag devices我想同时读取两者的温度。例如,我想每 500 毫秒从两个设备读取一次温度,并在两个 TextView 中将其显示给用户。
我的应用目前成功连接到两个 BLE 设备,如下所示:
@OnClick(R.id.connectButton1)
public void connectFirstSensorTag(Button b) {
if (!isDeviceConnected(sensorTag1)) {
connectionObservable1 = sensorTag1.establishConnection(getApplicationContext(), false).compose(new ConnectionSharingAdapter());
}
connectionObservable1.subscribe(new Subscriber<RxBleConnection>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
updateStatus(statusTextView1, "SensorTag not found");
}
@Override
public void onNext(RxBleConnection rxBleConnection) {
updateStatus(statusTextView1, "Connected");
enableSensorTagTemperatureSensor(connectionObservable1);
}
});
}
@OnClick(R.id.connectButton2)
public void connectSecondSensorTag(Button b) {
if (!isDeviceConnected(sensorTag2)) {
connectionObservable2 = sensorTag2.establishConnection(getApplicationContext(), false).compose(new ConnectionSharingAdapter());
}
connectionObservable2.subscribe(new Subscriber<RxBleConnection>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
updateStatus(statusTextView2, "SensorTag not found");
}
@Override
public void onNext(RxBleConnection rxBleConnection) {
updateStatus(statusTextView2, "Connected");
enableSensorTagTemperatureSensor(connectionObservable2);
}
});
}
现在我正在寻找每 500 毫秒同时从两者读取温度的最佳方法。
现在,我正在做这样的事情:
connectionObservable1
.flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(uuidFromShortCode("AA01")))
.subscribe(bytes -> {
// first temperature was successfully read here
connectionObservable2
.flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(uuidFromShortCode("AA01")))
.subscribe(bytes -> {
// second temperature was successfully read here
}, error -> {
updateStatus(error.toString());
});
}, error -> {
updateStatus(error.toString());
});
并且此代码块位于每 500 毫秒调用一次的可运行对象中。
我觉得这是一种极其低效的方法。有人可以告诉我是否有更好的方法吗?
最佳答案
首先,您无法在 BLE 上进行真正的并行读取或任何其他操作,因为您只有一个 radio ,并且操作需要按顺序进行。你能做的最好的事情就是尽快一个接一个地解雇他们。使用 RxAndroidBle,您将获得为您管理的序列化。
在我看来,做你想做的事的方式是这样的:
RxBleDevice sensorTag0 = // your first SensorTag
RxBleDevice sensorTag1 = // your second SensorTag
UUID characteristicUuid = uuidFromShortCode("AA01");
Subscription flowSubscription = Observable.combineLatest(
sensorTag0.establishConnection(this, false), // establishing connections
sensorTag1.establishConnection(this, false),
Pair::new // merging them together
)
.flatMap(connections -> Observable
.interval(500, TimeUnit.MILLISECONDS) // every 500 ms
.flatMap(aLong -> Observable.combineLatest(
connections.first.readCharacteristic(characteristicUuid), // performing reads
connections.second.readCharacteristic(characteristicUuid),
Pair::new // and merging the results
)))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
readings -> {
updateUISensorTag0(readings.first); // updating the UI
updateUISensorTag1(readings.second);
},
throwable -> updateStatus(throwable.toString()) // or showing the error
);
希望对你有帮助。
最好的问候。
关于android - 如何同时有效地读取两个 BLE 设备的温度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940943/
我正在开发一个使用 BLE Android 到 iOS 的聊天应用程序,现在我正在使用下面两个库作为引用 https://github.com/izumin5210/Bletia https://gi
我正在从 HCI 套接字接收 EVT_LE_ADVERTISING_REPORT。我想区分 BLE 信标和普通 BLE 设备(解析设备名称等)我有点困惑是否所有 BLE 设备都会发出信标或者它们是不同
我正在从 HCI 套接字接收 EVT_LE_ADVERTISING_REPORT。我想区分 BLE 信标和普通 BLE 设备(解析设备名称等)我有点困惑是否所有 BLE 设备都会发出信标或者它们是不同
我在我的 iOS 应用程序项目中使用核心蓝牙框架。我有以下关于蓝牙低功耗的问题 - iOS 中单个中央设备是否可以连接多个外围设备? 多个中央设备可以与单个外围设备连接吗? 单个 iOS 设备可以同时
我正在创建一个 iOS 和一个 Android 应用程序,它们从蓝牙传感器读取一些数据并将它们保存在数据库中。 即使应用程序终止,我也想提供保存传感器数据的能力。 仅供引用。当应用程序在后台时,我已经
我正在使用核心蓝牙框架并扫描一些设备,例如 micromax A250、micromax A116、samsung grand neo、HTC 610 和 ipod 5s,然后我无法扫描 samsun
这个任务的目的是通过BLE设备连接iPhone的BLE,访问iphone的ANCS。请注意,iPhone 中没有安装应用程序来打开 iPhone 的 BLE。那么,如果我们从 iPhone 设置中启用
我在为我的 BLE 设备开发安卓软件时遇到了问题。我的软件可以找到我的设备和 GATT 服务,但在我的服务中找不到任何特征。 我检查了 android-sdk-4.4.2 源码,找到了一些代码。 ht
有什么方法可以从 BleExplr、LightBlue 等通用 BLE 扫描应用程序中隐藏 BLE 设备? 最佳答案 您可以配置外围设备以使用服务请求。在这种模式下,中央为它们提供服务和外设扫描 -
我有 3 个组件。 Activity1 有连接和断开 BLE 连接的按钮 Activity2 需要从 BLE 设备获取数据。 Service 所有连接逻辑(如 getRemoteDevice()、co
我有一个 BLE 设备,它在通过相当标准的用户界面(点击 UITableView 中显示的设备条目)选择后连接到 iOS 设备。 连接非常简单 - 一些内部处理,然后调用 CBCentralManag
我正在尝试使用 Meteor 和这个 Cordova 插件 - https://github.com/don/cordova-plugin-ble-central - 使用 meteor add co
我坚持在 Android Lollipop 智能手机和 BLE 设备(带有 BLE 模块的 TI 实验板)之间实现连接。我使用以下调用进行连接: device.connectGatt(context,
我正在使用 react-native-ble-plx 在我的应用程序中实现蓝牙 蓝牙扫描工作正常,但它在 android 中包含重复项,而在 iOS 中工作正常,因为 allowDuplicates
我正在尝试在我的 Windows 笔记本电脑上设置一个基于 Nodejs 的演示,并使用额外的 BLE 适配器将我的笔记本电脑连接到另一个 BLE 设备 (Anki Overdrive)。我在互联网上
我正在研究基于 Android 的 BLE 接近感应功能,需要一些信息。目前我看到没有适用于 android 的 BLE 信标制造商。到目前为止,我为 iPhone 找到了 2 个。1) http:/
我正在使用 cordova 和 BLE 插件开发一个应用程序。我想通过 BLE 根据硬编码的已知 device.name 自动连接到 ESP32,而无需用户按下连接按钮。 我的想法是: 在设备准备就绪
我正在尝试实现从/向蓝牙设备接收和发送消息的模块。 我一直在寻找可以通过蓝牙搜索、连接和发送消息的 NuGet 包,但找不到适用于 Linux 的任何东西。 我正在使用 .NET Core 2.1 和
我正在尝试寻找一种方法来了解如何在 iOS 中获取 BLE mac??? 这将适用于所有 BLE,不会存在制造商依赖性。我们正在寻找扫描时间的解决方案。我可以在扫描时间内区分 BLE 吗? 如果获取M
我已阅读技术规范并试图了解为什么 BLE 4.2 比 BLE 4.1 更快? 我们能否发送大于 20 字节的数据包或者连接间隔是否更快? 我想了解是什么让 BLE 4.2 更快。 最佳答案 与早期相比
我是一名优秀的程序员,十分优秀!