如何在颤动中使用蓝牙发送/接收数据? [英] How do I send/receive data using bluetooth in flutter?

查看:169
本文介绍了如何在颤动中使用蓝牙发送/接收数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从arduino uno接收和发送数据.我试着研究了Flutter Blue插件和Flutter蓝牙串行插件,Flutter串行插件似乎是不完整的,并且飘动的蓝色缺少示例或文档,而github官方示例太复杂了,与我要执行的操作无关.我想要一种使用HC-05模块从arduino发送或检索数据的非常简单的方法.

I'm trying to receive and send data from and to an arduino uno. I've tried looking into flutter blue plugin and flutter bluetooth serial plugin ,flutter serial plugin seems to be incomplete and flutter blue lacks examples or documentation, and the official github example is way too complicated and is irrelevant to what i want to do. I want a very simple method of sending or retrieving data from an arduino using a HC-05 module.

推荐答案

如果正在使用HC-05模块(无低功耗蓝牙).使用"flutter_bluetooth_serial"包.这不是一个很好的程序包,但是应该可以.

If you are working with a HC-05 module (No Low Energy Bluetooth). Use ´flutter_bluetooth_serial´ package. It is not a great package, but it should work.

此示例可能不起作用.

扫描设备:

//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
  });
}

连接到设备:

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

Use this function when you select a device from the result list.

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;
}

这篇关于如何在颤动中使用蓝牙发送/接收数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆