android - 通过 HM-10 和 Flutter-blue 实现 Arduino 和 Flutter 之间的通信
问题描述
我正在做一个物联网项目,需要与 arduino 通信以收集一些温度信息并将其从串行通信通过 HM-10 传递到移动应用程序。
该应用程序将请求数据,然后 arduino 将通过 HM-10 传输数据。
我能够通过特性连接到 HM-10 并使用 Flutter-blue 依赖项向 arduino 发送命令。但是即使传输数据也无法从arduino板上收集数据。即使我通过flutter_blue读取了数据没有显示的特征。
我想知道,因为我们可以通过蓝牙写入数据,所以我们也可以获取和读取数据。 (因为 features.read() 不起作用)
如果有人能回答这个问题,那将是一个巨大的帮助。
以下是用于与 HM-10 通信的代码。
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:rxdart/rxdart.dart';
class BluetoothService with ChangeNotifier {
final FlutterBlue flutterBlue = FlutterBlue.instance;
late String connectionText;
Future<bool> get bluetoothState => enabled();
Future<bool> enabled() async {
return await flutterBlue.isOn;
}
late StreamSubscription scanSubscription;
String targetDeviceName = 'BT05';
late BluetoothCharacteristic? targetCharacteristic;
late BluetoothDevice _targetDevice;
BluetoothDevice get targetDevice => _targetDevice;
bool isDone = true;
Stream streamScreen =
ValueConnectableStream(FlutterBlue.instance.state).autoConnect();
startScan() {
connectionText = "Start Scanning";
print(connectionText);
scanSubscription =
flutterBlue.scan(timeout: Duration(seconds: 4)).listen((scanResult) {
print(scanResult);
if (scanResult.device.name == targetDeviceName) {
stopScan();
connectionText = "Found Target Device";
print(connectionText);
_targetDevice = scanResult.device;
notifyListeners();
connectToDevice();
}
}, onDone: () {
isDone = false;
print('is done $isDone');
stopScan();
notifyListeners();
});
notifyListeners();
}
stopScan() {
scanSubscription.cancel();
flutterBlue.stopScan();
print("stopped");
}
connectToDevice() async {
connectionText = "Device Connecting";
print(connectionText);
await _targetDevice.connect();
isDone = true;
connectionText = "Device Connected";
print(connectionText);
discoverServices();
}
String uuid = "0000ffe0-0000-1000-8000-00805f9b34fb";
String writeUuid = "0000ffe1-0000-1000-8000-00805f9b34fb";
discoverServices() async {
var services = await _targetDevice.discoverServices();
services.forEach(
(service) {
if (service.uuid.toString() == uuid) {
service.characteristics.forEach((characteristic) {
if (characteristic.uuid.toString() == writeUuid) {
targetCharacteristic = characteristic;
print("All Ready with ${targetDevice.name}");
}
});
}
},
);
}
readData() async {
// _targetDevice.discoverServices().then((services) async {
// List<int> decode = await services[2].characteristics[0].read();
// print(decode);
// String msg = utf8.decode(decode);
// print(msg);
// });
targetCharacteristic!.value.listen(null).onData((data) {
String msg1 = utf8.decode(data);
print("here is the $msg1");
});
List<int> decode = await targetCharacteristic!.read();
String msg = utf8.decode(decode);
print(msg);
}
disconnectFromDevice({required BluetoothDevice device}) async {
await device.disconnect();
connectionText = "Device Disconnected";
print(connectionText);
notifyListeners();
}
writeData(String data) async {
if (targetCharacteristic == null) return;
List<int> bytes = utf8.encode(data);
print("bytes recieved");
await targetCharacteristic!.write(bytes, withoutResponse: false);
}
}
以下是arduino代码。
char inputdata = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the baud rate for bluetooth pins
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
inputdata = Serial.read(); //Read the incoming data & store into data
// If a measurement is required, measure data and send it back
if ( inputdata == 't'){
int t = 32;
int m =7200;
String data = (String(t)+','+String(m));
Serial.print(data);
}else if(inputdata == 'g') {
String temp = "32";
Serial.print(temp);
}
}
}
解决方案
推荐阅读
- javascript - 如何在带有 react-router-dom 和查询字符串的 url 上使用参数并使用它?
- apache-spark - Parquet 压缩性能分组与平面数据
- vb.net - 是否可以在 vb.net 中的两个数据网格上循环
- php - 在产品页面 opencart 2 中显示所有折扣代码
- node.js - Sequelize - 有条件地按关联排序
- redux-form - 如果特定输入具有值,则隐藏输入
- r - R函数使用前一行数据循环遍历新行
- javascript - 找到双 jquery.js 包含并删除它
- javascript - 根据当前页面或 URL 设置菜单活动
- java - 卡夫卡民意调查突然卡住了