[英]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.