我正在编写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
}
答案 0 :(得分:0)
使用connectedDevices
的未来:
var _flutterBlue = FlutterBlue.instance;
_flutterBlue.connectedDevices.then((value) {
//Do your processing
});