【发布时间】:2020-01-10 16:06:22
【问题描述】:
我正在尝试从 arduino uno 接收和发送数据。我试过研究颤振蓝色插件和颤振蓝牙串行插件,颤振串行插件 似乎不完整,flutter blue 缺少示例或文档,官方 github 示例太复杂了,与我想做的事情无关。我想要一种使用 HC-05 模块从 arduino 发送或检索数据的非常简单的方法。
【问题讨论】:
我正在尝试从 arduino uno 接收和发送数据。我试过研究颤振蓝色插件和颤振蓝牙串行插件,颤振串行插件 似乎不完整,flutter blue 缺少示例或文档,官方 github 示例太复杂了,与我想做的事情无关。我想要一种使用 HC-05 模块从 arduino 发送或检索数据的非常简单的方法。
【问题讨论】:
如果您使用的是 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_bluetooth_serial”包中的示例应用程序。它们甚至包括从 HC-05 模块读取数据!如果您想要更简单的东西,请尝试从示例中提取基本代码并将其复制到另一个应用程序中。
【讨论】: