我正在使用flutter_reactive_ble包从我的BLE设备接收数据.

我有以下方法:

_broadcastCharacteristSubscription = _flutterReactiveBle
          .subscribeToCharacteristic(_broadCastCharacteristic!)
          .listen((event) {
        Uint8List data = Uint8List.fromList(event); 
        _handleBytesReceived(data);
      });

这些数据被字节填充并压缩成块.

我已经完成了字节填充,以重新构建如下所示的压缩内容:

I/flutter (13164): Current payload before decompression : xUO�� ��>�Ew�gL

现在我需要解压缩,所以我try 使用ZLibDecoder,因为我的客户告诉我他们使用Zlib进行压缩:

enter image description here

因此,我实现了以下几点:

  void _handleBytesReceived(Uint8List data) {
    String decoded = _decoder.convert(data);
    print("Ascii string compressed data : $decoded");
    if (currentPayload.isEmpty) {
      currentPayload = decoded;
      print("Current payload is now : $currentPayload");
    }
    else {
      for (var element in decoded.characters) {
        if (element != "~") {
          currentPayload += element;
        }
        else {
          currentPayload = currentPayload.replaceAll("~", "");
          print("Current payload before decompression : $currentPayload");
          currentPayload = _unzipPayload(currentPayload);
          print("Current payload after : $currentPayload");
          currentPayload = "";
          break;
        }
      }
    }
  }

  String _unzipPayload(String encoded) {
    return String.fromCharCodes(ZLibCodec(level: 6).decode(encoded.codeUnits));
  }

我收到以下错误:

I/flutter (13164): Error from outside Flutter : FormatException: Filter error, bad data
  I/flutter (13164):  #0      _FilterImpl.processed (dart:io-patch/filter_patch.dart:13:71)
  I/flutter (13164): #1      _FilterSink.addSlice (dart:io/data_transformer.dart:505:29)
  I/flutter (13164): #2      _FilterSink.add (dart:io/data_transformer.dart:489:5)
  I/flutter (13164): #3      ZLibDecoder.convert (dart:io/data_transformer.dart:363:9)
  I/flutter (13164): #4      Codec.decode (dart:convert/codec.dart:30:34)
  I/flutter (13164): #5      ReactiveBleAPI._unzipPayload (package:myappname/api/reactive_ble_api.dart:213:53)
  I/flutter (13164): #6      ReactiveBleAPI._handleBytesReceived (package:myappname/api/reactive_ble_api.dart:203:28)
  I/flutter (13164): #7      ReactiveBleAPI._connectToDevice.<anonymous closure> (package:myappname/api/reactive_ble_api.dart:171:9)
  I/flutter (13164): #8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
  I/flutter (13164): #9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
  I/flutter (13164): #10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
  I/flutter (13164): #11     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:776:19)
  I/flutter (13164): #12     _StreamController._add (dart:async/stream_controller.dart:650:7)
  I/flutter (13164): #13     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
  I/flutter (13164): #14     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
  I/flutter (13164): #15     _DelayedData.perform (dart:async/stream_impl.dart:515:14)
  I/flutter (13164): #16     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
  I/flutter (13164): #17     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
  I/flutter (13164): #18     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
  I/flutter (13164): #19     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

解压似乎失败了,我非常确定数据没有损坏,除非我在上面的转换中搞错了.

例如,我是不是对currentPayload.codeUnits做错了什么?或者在其中的任何部分?

EDIT:以下是没有try 从BLE中解码初始字节的新代码(按照注释中的建议删除String decoded = _decoder.convert(data):

void _resetPayload() {
    print("Current payload before compression : $currentPayload");
    List<int> finalList = [];
    for (var element in currentPayload) {
      if (element != 126) {
        finalList.add(element);
      }
    }
    String output = _unzipPayload(Uint8List.fromList(finalList));
    print("Current payload after compression : $output");
    currentPayload = Uint8List(0);
  }

  void _handleBytesReceived(Uint8List data) {
    print("Raw data : $data");
    //String decoded = _decoder.convert(data);
    // print("Decoded ascii data : $decoded");
    if (currentPayload.isEmpty) {
      currentPayload = data;
    }
    print("Current payload is : $currentPayload");
    for (var element in data.toList()) {
      if (currentPayload.contains(126)) {
        if (element == 126) {
          _resetPayload();
        }
        else {
          currentPayload.add(element);
        }
      }
    }
  }

  String _unzipPayload(Uint8List encoded) {
    try {
      List<int> decoded = _codec.decode(encoded);
      print("Decoded : $decoded");
      String result =  String.fromCharCodes(decoded);
      print("Result : $result");
      return result;
    } catch (error) {
      print("Error unzipping $error");
    }
    return "";
  }

下面是我通过上面的代码获得的日志(log),例如:

I/flutter (25368): Raw data : [126, 120, 1, 85, 79, 209, 10, 131, 48, 12, 252, 151, 60, 23, 113, 67, 157, 245, 103, 36]
I/flutter (25368): Current payload is : [126, 120, 1, 85, 79, 209, 10, 131, 48, 12, 252, 151, 60, 23, 113, 67, 157, 245, 103, 36]
I/flutter (25368): Current payload before compression : [120, 1, 85, 79, 209, 10, 131, 48, 12, 252, 151, 60, 23, 113, 67, 157, 245, 103, 36]
I/flutter (25368): Decoded : []
I/flutter (25368): Result : 
I/flutter (25368): Current payload after compression : 
D/BluetoothGatt(25368): onConnectionUpdated() - Device=C4:DE:E2:90:A6:DE interval=36 latency=0 timeout=500 status=0
I/flutter (25368): Raw data : [126, 120, 1, 85, 140, 65, 10, 133, 48, 16, 67, 239, 146, 117, 23, 174, 123, 21, 145, 207]
I/flutter (25368): Current payload is : [126, 120, 1, 85, 140, 65, 10, 133, 48, 16, 67, 239, 146, 117, 23, 174, 123, 21, 145, 207]
I/flutter (25368): Current payload before compression : [120, 1, 85, 140, 65, 10, 133, 48, 16, 67, 239, 146, 117, 23, 174, 123, 21, 145, 207]
I/flutter (25368): Decoded : []
I/flutter (25368): Result : 
I/flutter (25368): Current payload after compression : 

最终编辑/回答:有效负载为空,因为我没有请求更高的MTU,没有传输足够的字节.我已经根据我的BLE设备将其设置为180.

推荐答案

您似乎正在将字节流视为字符.这不是一个字符串(这就是为什么您会看到�;这是解码器告诉您它不是有效的UTf-8).您需要直接对Uint8List进行操作.你不应该打String decoded = _decoder.convert(data).

Flutter相关问答推荐

Flutter—如何在Riverpod中使用全类状态?

为什么Flutter InAppWebView中的DatePicker看起来很旧?

Android NFC未收到空的NFC标签

IsScrollable为True时如何删除Flutter 选项卡栏左侧填充

如何使用Dio/Ffltter(前端)和amp;Go(后端)向API发送正确的请求

设置Flutter 中的ListView.Builder()的最小高度

如何将assets资源 实体类型转换为文件类型?

使小部件处于有状态状态会导致hasSize异常

Flutter守护进程无法启动

如何在 Dart/Flutter 中克隆 Future

如何在flutter中更改showModalBottomSheet小部件中CheckboxListTile小部件的值?

SliverAppbar 呈蓝色

我无法找到修复此错误的位置,指出无法使用静态访问来访问实例成员

hooks_riverpod 有用途吗

「Flutter」错误:没有名为'kind'的命名参数

Listview 如何转到下一行/新行?

在 Flame Forge2d 中射击子弹

如何验证非表单字段?

Flutter 中的无效日期格式 2022 年 11 月 14 日

TextSpan 避免符号字符串组上的换行符