Arduino Nano 33 BLE 不接收通过 flutter 应用程序发送的数据
Arduino Nano 33 BLE doesn't receive data sent via flutter app
import 'package:control_pad/control_pad.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
final flutterReactiveBle = FlutterReactiveBle();
late QualifiedCharacteristic c;
Future<void> main() async {
runApp(const MaterialApp(
title: 'Control Pad Example',
home: FirstRoute(),
));
}
class FirstRoute extends StatelessWidget {
const FirstRoute({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('car'),
actions: [
IconButton(
constraints: BoxConstraints.expand(width: 80),
onPressed: () async {
print("pressed");
var serviceUuid =
Uuid.parse("0000180a-0000-1000-8000-00805f9b34fb");
var characteristicUuid =
Uuid.parse("00002a57-0000-1000-8000-00805f9b34fb");
var foundDeviceId = 'FB:DF:BF:CD:83:26';
flutterReactiveBle.connectToDevice(id: foundDeviceId,
connectionTimeout: const Duration(seconds: 5),
).listen((state) async {
if (state.connectionState == DeviceConnectionState.connected) {
print("connected");
await flutterReactiveBle.requestConnectionPriority(deviceId: foundDeviceId, priority: ConnectionPriority.highPerformance);
c = QualifiedCharacteristic(
serviceId: serviceUuid,
characteristicId: characteristicUuid,
deviceId: foundDeviceId);
}
});
},
icon: const Text("BLE", textAlign: TextAlign.center),
),
],
),
body: JoystickView(
onDirectionChanged: (double x, double y) async {
print('$x, $y');
flutterReactiveBle.writeCharacteristicWithoutResponse(c, value: [0x01]);
},
),
);
}
}
我从 Arduino 收到了连接确认,但是当尝试使用 writeCharacteristicWithoutResponse 时,Arduino 似乎没有收到任何东西。使用像 LightBlue 这样的应用程序,我可以轻松地编写十六进制输入,而 Arduino 会接收它们。我对蓝牙没有太多先验知识,想了解我在这里缺少的是什么。 Arduino 代码似乎运行良好,所以问题出在 flutter 应用程序方面。
我无法让代码与我正在使用的包一起工作。改成Flutter Blue并使用这段代码后,我在flutter应用程序中写入和接收数据成功。
async {
flutterBlue.startScan(timeout: Duration(seconds: 4));
var subscription =
flutterBlue.scanResults.listen((results) async {
for (int k = 0; k < results.length; k++) {
ScanResult r = results[k];
if (r.device.id.toString() == <YOUR_DEVICE_ID>) {
print("found device");
device = r.device;
await device.connect();
List<BluetoothService> services =
await device.discoverServices();
for (int j = 0; j < services.length; j++) {
var service = services[j];
if (service.uuid.toString() ==
<YOUR_SERVICE_UUID>) {
print("found serial");
var characteristics = service.characteristics;
for (int i = 0; i < characteristics.length; i++) {
BluetoothCharacteristic c = characteristics[i];
if (c.uuid.toString() ==
<YOUR_CHARACTERISTIC_UUID>) {
print("found chars");
chars = c;
}
}
}
}
}
}
});
}
这是一个可用但有问题的特征查找器。在此之后,您可以使用简单的 await chars.write([<hex-value>]);
发送数据。
如果没有打开响应(它们是自动的),这是行不通的。如果有人能解释一下,那就太好了。
import 'package:control_pad/control_pad.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
final flutterReactiveBle = FlutterReactiveBle();
late QualifiedCharacteristic c;
Future<void> main() async {
runApp(const MaterialApp(
title: 'Control Pad Example',
home: FirstRoute(),
));
}
class FirstRoute extends StatelessWidget {
const FirstRoute({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('car'),
actions: [
IconButton(
constraints: BoxConstraints.expand(width: 80),
onPressed: () async {
print("pressed");
var serviceUuid =
Uuid.parse("0000180a-0000-1000-8000-00805f9b34fb");
var characteristicUuid =
Uuid.parse("00002a57-0000-1000-8000-00805f9b34fb");
var foundDeviceId = 'FB:DF:BF:CD:83:26';
flutterReactiveBle.connectToDevice(id: foundDeviceId,
connectionTimeout: const Duration(seconds: 5),
).listen((state) async {
if (state.connectionState == DeviceConnectionState.connected) {
print("connected");
await flutterReactiveBle.requestConnectionPriority(deviceId: foundDeviceId, priority: ConnectionPriority.highPerformance);
c = QualifiedCharacteristic(
serviceId: serviceUuid,
characteristicId: characteristicUuid,
deviceId: foundDeviceId);
}
});
},
icon: const Text("BLE", textAlign: TextAlign.center),
),
],
),
body: JoystickView(
onDirectionChanged: (double x, double y) async {
print('$x, $y');
flutterReactiveBle.writeCharacteristicWithoutResponse(c, value: [0x01]);
},
),
);
}
}
我从 Arduino 收到了连接确认,但是当尝试使用 writeCharacteristicWithoutResponse 时,Arduino 似乎没有收到任何东西。使用像 LightBlue 这样的应用程序,我可以轻松地编写十六进制输入,而 Arduino 会接收它们。我对蓝牙没有太多先验知识,想了解我在这里缺少的是什么。 Arduino 代码似乎运行良好,所以问题出在 flutter 应用程序方面。
我无法让代码与我正在使用的包一起工作。改成Flutter Blue并使用这段代码后,我在flutter应用程序中写入和接收数据成功。
async {
flutterBlue.startScan(timeout: Duration(seconds: 4));
var subscription =
flutterBlue.scanResults.listen((results) async {
for (int k = 0; k < results.length; k++) {
ScanResult r = results[k];
if (r.device.id.toString() == <YOUR_DEVICE_ID>) {
print("found device");
device = r.device;
await device.connect();
List<BluetoothService> services =
await device.discoverServices();
for (int j = 0; j < services.length; j++) {
var service = services[j];
if (service.uuid.toString() ==
<YOUR_SERVICE_UUID>) {
print("found serial");
var characteristics = service.characteristics;
for (int i = 0; i < characteristics.length; i++) {
BluetoothCharacteristic c = characteristics[i];
if (c.uuid.toString() ==
<YOUR_CHARACTERISTIC_UUID>) {
print("found chars");
chars = c;
}
}
}
}
}
}
});
}
这是一个可用但有问题的特征查找器。在此之后,您可以使用简单的 await chars.write([<hex-value>]);
发送数据。
如果没有打开响应(它们是自动的),这是行不通的。如果有人能解释一下,那就太好了。