【问题标题】:How do I send/receive data using bluetooth in flutter?如何在颤振中使用蓝牙发送/接收数据?
【发布时间】:2020-01-10 16:06:22
【问题描述】:

我正在尝试从 arduino uno 接收和发送数据。我试过研究颤振蓝色插件和颤振蓝牙串行插件,颤振串行插件 似乎不完整,flutter blue 缺少示例或文档,官方 github 示例太复杂了,与我想做的事情无关。我想要一种使用 HC-05 模块从 arduino 发送或检索数据的非常简单的方法。

【问题讨论】:

    标签: android flutter arduino


    【解决方案1】:

    如果您使用的是 HC-05 模块(无低功耗蓝牙)。使用“flutter_bluetooth_serial”包。这不是一个很好的包,但它应该可以工作。

    这个例子可能行不通。

    扫描设备:

    //Here the scan results will be saved
    List<BluetoothDiscoveryResult> results = List<BluetoothDiscoveryResult>();
    
    void startDiscovery() {
      streamSubscription = FlutterBluetoothSerial.instance.startDiscovery().listen((r) {
        results.add(r);
      });
    
      streamSubscription.onDone(() {
        //Do something when the discovery process ends
      });
    }
    

    连接到设备:

    从结果列表中选择设备时使用此功能。

    BluetoothConnection connection;
    
    connect(String address) async {
      try {
        connection = await BluetoothConnection.toAddress(address);
        print('Connected to the device');
    
        connection.input.listen((Uint8List data) {
          //Data entry point
          print(ascii.decode(data));
        })
    
      } catch (exception) {
        print('Cannot connect, exception occured');
      }
    }
    

    发送数据:

    Future send(Uint8List data) async {
        connection.output.add(data);
        await _connection.output.allSent;
    }
    

    【讨论】:

    • 不支持新版本的Flutter,建议使用这个包flutter_reactive_ble:^3.1.1
    • @Hamed Flutter_reactive_ble 不支持蓝牙
    【解决方案2】:

    立即试用“flutter_bluetooth_serial”包中的示例应用程序。它们甚至包括从 HC-05 模块读取数据!如果您想要更简单的东西,请尝试从示例中提取基本代码并将其复制到另一个应用程序中。

    【讨论】:

      猜你喜欢
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      • 2012-11-07
      相关资源
      最近更新 更多