gpt4 book ai didi

flutter - 如何在flutter中使用flutter blue接收数据

转载 作者:行者123 更新时间:2023-12-02 02:31:55 31 4
gpt4 key购买 nike

我实际上很困惑如何使用 flutter_blue 包接收数据,我可以发送数据但无法接收,这是我一直在使用的示例:

  startScan() {
setState(() {
connectionText = "Start Scanning";
});

scanSubScription = flutterBlue.scan().listen((scanResult) {
if (scanResult.device.name == TARGET_DEVICE_NAME) {
print('DEVICE found');
stopScan();
setState(() {
connectionText = "Found Target Device";
});

targetDevice = scanResult.device;
connectToDevice();
}
}, onDone: () => stopScan());
}

stopScan() {
scanSubScription?.cancel();
scanSubScription = null;
}

connectToDevice() async {
if (targetDevice == null) return;

setState(() {
connectionText = "Device Connecting";
});

await targetDevice.connect();
print('DEVICE CONNECTED');
setState(() {
connectionText = "Device Connected";
});

discoverServices();
}

disconnectFromDevice() {
if (targetDevice == null) {}

targetDevice.disconnect();

setState(() {
connectionText = "Device Disconnected";
});
}

discoverServices() async {
if (targetDevice == null) return;

List<BluetoothService> services = await targetDevice.discoverServices();
services.forEach((service) {
// do something with service
if (service.uuid.toString() == SERVICE_UUID) {
service.characteristics.forEach((characteristic) {
if (characteristic.uuid.toString() == CHARACTERISTIC_UUID) {
targetCharacteristic = characteristic;
// writeData("A 300 300 300");
setState(() {
connectionText = "All Ready with ${targetDevice.name}";
});
}
});
}
});
}

writeDataAndWaitForRespond() async {
writeData("A 300 300 300");
List<BluetoothService> services = await targetDevice.discoverServices();
print("////////////////We're here, listening to Hive...");
// isDeviceTurnedOn = true;
services.forEach((service) async {
Future.delayed(const Duration(milliseconds: 500), () async {
print("Entered the loop...");
var characteristics = service.characteristics;
for (BluetoothCharacteristic c in characteristics) {
List<int> value = await c.read();
String stringValue = new String.fromCharCodes(value);
print("The recieved Characteristic Value is $stringValue and $value");
print("Entered the second loop...");
var descriptors = c.descriptors;
print("The descriptors value is equal to: $descriptors");
for (BluetoothDescriptor d in descriptors) {
List<int> value = await d.read();
print("Entered the third loop...");
String stringValue = new String.fromCharCodes(value);
print("The recieved Value is $stringValue and $value");
}
}
});
});
}

writeData(String data) {
if (targetCharacteristic == null) return;

List<int> bytes = utf8.encode(data);
targetCharacteristic.write(bytes);
}

我完全糊涂了,我如何使用 flutter_blue 从我的 ESP32 设备接收数据,ESP32 工作正常并且我已经使用“BLE 扫描”对其进行了测试,据我所知 writeDataAndWaitForRespond 函数应该执行工作但它没有,它甚至不会进入以下循环:

          for (BluetoothDescriptor d in descriptors) {
List<int> value = await d.read();
print("Entered the third loop...");
String stringValue = new String.fromCharCodes(value);
print("The recieved Value is $stringValue and $value");
}

请帮助,在此先感谢。

最佳答案

因为您正在使用

将数据写入 targetCharacteristic
  writeData(String data) {
if (targetCharacteristic == null) return;

List<int> bytes = utf8.encode(data);
targetCharacteristic.write(bytes);
}

您使用相同的方式读取数据

  Future<List<int>> readData() async {
if (targetCharacteristic == null) return;
return await targetCharacteristic.read();
}

关于flutter - 如何在flutter中使用flutter blue接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64918849/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com