![](/img/trans.png)
[英]Flutter how to check if there is a device connected to my device via bluetooth
[英]Flutter How can I check if my device is connected to another device with Bluetooth or not
我正在编写一个 Flutter 应用程序,它检查我的设备是否连接到另一个设备、+-蓝牙设备或全部没有。
我在 Flutter 文档中找到的内容无法帮助我,因为我首先检查蓝牙是否打开。 我向用户询问它,因为它显示在代码中,但我无法检查设备是否已连接。 我用wifi做到了,但用蓝牙我不能。
@override
Widget build(BuildContext context) {
return MaterialApp(
color: Colors.lightBlue,
home: StreamBuilder<BluetoothState>(
stream: FlutterBlue.instance.state,
initialData: BluetoothState.unknown,
builder: (c, snapshot) {
final state = snapshot.data;
if (state == BluetoothState.off)
// ignore: missing_return
return Bluetoth();
else return Find();
}),
);
}
// open bluetooth if closed
class BluetothState extends State<Bluetoth>
{
void initState(){
super.initState();
aa();
}
Future aa() async {
// async lambda seems to not working
print('ccc');
await FlutterBluetoothSerial.instance.requestEnable();
}
class _FindState extends State<Find> {
//check bluetooth connectivity
}
使用connectedDevices
未来:
var _flutterBlue = FlutterBlue.instance;
_flutterBlue.connectedDevices.then((value) {
//Do your processing
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.