简体   繁体   English

flutter_reactive_ble - 如何在订阅前检查设备是否已连接

[英]flutter_reactive_ble - how to check if device is connected before subscribing

I'm using this plugin to try to develop an app that uses a Bluetooth module.我正在使用这个插件来尝试开发一个使用蓝牙模块的应用程序。

GitHub https://github.com/PhilipsHue/flutter_reactive_ble/ GitHub https://github.com/PhilipsHue/flutter_reactive_ble/

I'm trying to modify what the example included in the source code, so that onTap of the list of devices, would connect and subscribeToCharacteristic .我正在尝试修改源代码中包含的示例,以便设备列表的 onTap 将连接和 subscribeToCharacteristic

but, it seems that the connectToDevice() method is declared as a stream,但是,似乎 connectToDevice() 方法被声明为 stream,

and putting "await" before the method call won't really fit my criteria (on a device connected, then subscribeToCharacteristic)并在方法调用之前放置“等待”并不真正符合我的标准(在连接的设备上,然后 subscribeToCharacteristic)

how do I achieve this?我该如何做到这一点?

what I currently have in device_list.dart我目前在 device_list.dart

(device) => ListTile(
    title: Text(device.name),
    subtitle: Text("${device.id}\nRSSI: ${device.rssi}"),
    leading: const BluetoothIcon(),
    onTap: () async {
        widget.stopScan();
        await widget.deviceConn.connect(device.id);
        //call to subscribeToCharacteristic(characteristic);
    });

await only works in case the method returns a future. await仅在方法返回未来的情况下才有效。 You should listen to the stream and when the stream emits a the connected state you execute the subscribe call.您应该收听 stream 并且当 stream 发出connected的 state 时,您执行订阅调用。 For example:例如:

_ble.connectToDevice(id: deviceId).listen(
      (update) {
        if(update.connectionState == DeviceConnectionState.connected){
       // execute subscribe
        }
      },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 flutter_reactive_ble 配对蓝牙设备 - Pair bluetooth device using flutter_reactive_ble 如何从 flutter_reactive_ble 示例中获取 deviceServices - How to get deviceServices from flutter_reactive_ble example 在后台接收数据(flutter_reactive_ble) - Receive data in background (flutter_reactive_ble) 如何在flutter/dart中正确使用flutter_reactive_ble writetocharacteristic和subscribetocharacteristic - how to properly use flutter_reactive_ble writetocharacteristic and subscribetocharacteristic in flutter/dart 无法通过蓝牙与 flutter_reactive_ble 连接 - Trouble to connect via bluetooth with flutter_reactive_ble flutter_reactive_ble 无法连接发布版本(Flutter 版本 3.3.4) - flutter_reactive_ble not able to connect with release build (Flutter version 3.3.4) Flutter 如何检查是否有设备通过蓝牙连接到我的设备 - Flutter how to check if there is a device connected to my device via bluetooth 如何使用 Flutter Reactive BLE 将 UUID 发送到蓝牙设备并保存对字符串的响应 - How to use Flutter Reactive BLE to send a UUID to a Bluetooth device and save response to string Flutter 连接设备(医生检查崩溃) - Flutter Connected device (the doctor check crashed) 如何在 Flutter 中与 BLE 设备进行绑定 - How to perform bonding with BLE device in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM