将数据写入 BLE 设备并读取其响应抖动?

Write data to BLE device and read its response flutter?

我在向 BLE 设备写入命令和接收数据时遇到问题。我正在将 ASCII 编码的字符串写入特征。当需要解码数据时,问题就出现了。收到的数据与 iOS 中收到的一样,但当我尝试解码时,它只是变成空白。我尝试通过UTF8解码器和ASCII解码器解码,但没有得到结果。

这就是我发现设备的方式。

@override
  void initState() {
    // TODO: implement initState
    widget.flutterBlue.scanResults.listen((List<ScanResult> results) {
      for (ScanResult result in results) {
        _addDeviceTolist(result.device);
      }
    });
    widget.flutterBlue.startScan();
  }

连接设备:-

void _connectDevice() async {
    widget.flutterBlue.stopScan();
    print(_deviceToConnect);
    try {
      await _deviceToConnect.connect();
    } catch (e) {
      if (e.code != 'already_connected') {
        throw e;
      }
    } finally {
      _services = await _deviceToConnect.discoverServices();
    }
    _discoverDeviceServices();
    setState(() {
      _connectedDevice = _deviceToConnect;
    });
  }

发现服务:-

在注释 TAG-1 和 TAG-2 行时打印值。我尝试了 ASCII 和 UTF8 解码。

在浏览各种问题时,我发现要从所有特征中读取值。

void _discoverDeviceServices() async {
    for (BluetoothService service in _services){
      for (BluetoothCharacteristic characteristic in service.characteristics){
        var value = await characteristic.read();
        print(value);
        // print(AsciiDecoder().convert(value)); /*** TAG-1***/
        // print(utf8.decode(value)); /*** TAG-2***/
        if (characteristic.properties.write){
          if (characteristic.properties.notify){
            _rx_Write_Characteristic = characteristic;
            _sendCommandToDevice();
          }
        }
      }
    }
  }

正在向设备写入命令:-

void _sendCommandToDevice() async {
    final command = "AT Z\r";
    final convertedCommand = AsciiEncoder().convert(command);
    await _rx_Write_Characteristic.write(convertedCommand);
    _connectedDevice.disconnect();
  }

请帮我指出我在读取写入 BLE 设备的数据的响应时出错的地方,以及如何准确地写入和读取值?

谢谢!

可能是String.fromCharCode你要找的

当我用你在 https://dartpad.dev/

上的值测试它时,它似乎给出了正确的答案

13\rescape characters表示马车return