java - RxAndroidBle2 配对时有时会丢失 ble 传感器的通知

标签 java android bluetooth-lowenergy rxandroidble bluetooth-gatt

我创建了自己的蓝牙服务来管理 BLE 传感器。当我尝试配对新传感器时遇到麻烦。它连接成功,但当 rxandroidble 调用 setCharacteristicNotification() 并在不到一秒后返回 true 时,它返回 enable: false

这是日志猫:

D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a37-0000-1000-8000-00805f9b34fb enable: true
D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a37-0000-1000-8000-00805f9b34fb enable: false

因此传感器已连接,但我无法从该传感器获取任何数据。

这是我用来启动服务的代码:

startService(new Intent(this, BluetoothTracker.class));
((User) getApplicationContext()).setServiceConnection(new ServiceConnection() {
     @Override
     public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
         ((User) getApplicationContext()).setBluetoothTracker(((BluetoothTracker.MyLocalBinder) iBinder).getService());
     }

     @Override
     public void onServiceDisconnected(ComponentName componentName) {

     }
});
bindService(new Intent(this, BluetoothTracker.class), ((User) getApplicationContext()).getServiceConnection(), BIND_AUTO_CREATE);

当我启动应用程序时,这总是有效,只有当我配对新传感器时,我才会遇到此问题。

这是 setupNotification 函数,似乎有问题,但我无法弄清楚发生了什么

private static void setupNotify(RxBleConnection connection, UUID uuid, IGattCharacteristicHandler handler) {
    connection.getCharacteristic(uuid).subscribe(characteristic -> {
        Observable<byte[]> notification = connection.setupNotification(characteristic, NotificationSetupMode.QUICK_SETUP).flatMap(notificationObservable -> notificationObservable);
        notification.subscribe(handler::handle, e -> Log.e(TAG, "BleException:" + e.getMessage()));
    }, throwable -> Log.e(TAG, "setupNotify::ERROR " + throwable.getMessage()));
}

最佳答案

您的连接可能会一直持续到您的可观察对象完成为止。 您可以实现 onCompete 回调并查看它是如何发生的。

rxandroidble 自动处理连接终止。 要了解更多详细信息,请查看他们的 samples

关于java - RxAndroidBle2 配对时有时会丢失 ble 传感器的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57787889/

相关文章:

java - java 二维数组

java - 如何在 Clojure REPL 中从 Maven 项目加载 Java 类?

android - 回滚到 ADT 20.0.3 和 SDK 之前的版本

android - IntentService 和 Activity 生命周期

android - 如何使用 AltBeacon 获得更快的测距响应?

bluetooth-lowenergy - 蓝牙 4.0 扫描响应

java - 纬度/经度查找器不工作

java - Spring 测试配置忽略 Maven 过滤器

javascript - 使用 javascript/jquery 检测移动浏览器的最佳方法?

java - BLE 入门 -> NullPointerException