【问题标题】:Flutter How can I check if my device is connected to another device with Bluetooth or notFlutter 如何检查我的设备是否通过蓝牙连接到另一台设备
【发布时间】:2020-04-19 23:37:22
【问题描述】:

我正在编写一个 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


              }

【问题讨论】:

    标签: android flutter dart bluetooth


    【解决方案1】:

    使用connectedDevices 未来:

    var _flutterBlue = FlutterBlue.instance;
    _flutterBlue.connectedDevices.then((value) {
      //Do your processing
    });
    

    【讨论】:

    • 它总是返回 True
    • 它返回一个设备列表,它不能返回一个布尔值。什么返回 true?
    • 一个列表???该设备一次只能连接到一个设备
    猜你喜欢
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 2013-02-05
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多