fetchProgressStream method

  1. @override
Stream<FetchProgress> fetchProgressStream(
  1. BluetoothDevice device
)
override

Stream of fetch progress events for the given device.

Subscribe to this stream before calling fetchMeasurement to receive progress updates and completion/failure notifications.

The stream emits:

Implementation

@override
Stream<FetchProgress> fetchProgressStream(BluetoothDevice device) {
  final deviceId = device.remoteId.str;

  // Transform the stream to process raw data on completion
  return _fetchRegistry.getFetchProgressStream(device).map((progress) {
    return progress.when(
      idle: () => progress,
      inProgress: (_) => progress,
      completed: (rawData, totalBytes, _) {
        // Process the raw data: extract measurement data and validate CRC
        // Pass config for decompression if data was stored compressed
        final expectedCrc = _expectedCrcByDevice[deviceId] ?? 0;
        final config = _measurementConfigByDevice[deviceId];
        final result = MeasurementDataProcessor.processRawData(
          rawData,
          expectedCrc,
          config: config,
        );
        return FetchProgress.completed(
          data: result.data,
          totalBytes: result.data.length,
          crcValid: result.crcValid,
        );
      },
      failed: (_, __, ___) => progress,
    );
  });
}