printCapturedTestLabel method

Future<void> printCapturedTestLabel(
  1. BleCharacteristic characteristic, {
  2. Duration interWriteDelay = const Duration(milliseconds: 80),
  3. Duration statusPollDelay = const Duration(milliseconds: 250),
  4. Duration responseTimeout = const Duration(seconds: 2),
  5. int maxStatusPolls = 20,
})

Implementation

Future<void> printCapturedTestLabel(
  BleCharacteristic characteristic, {
  Duration interWriteDelay = const Duration(milliseconds: 80),
  Duration statusPollDelay = const Duration(milliseconds: 250),
  Duration responseTimeout = const Duration(seconds: 2),
  int maxStatusPolls = 20,
}) async {
  _ensureConnected('print a captured test label');
  if (_printingCapturedTestLabel) {
    throw StateError('A captured test print is already in progress.');
  }
  if (!characteristic.canNotify ||
      !characteristic.properties.contains(
        BleCharacteristicProperty.writeWithoutResponse,
      )) {
    throw StateError(
      'Captured test printing requires notify and writeWithoutResponse.',
    );
  }

  final writes = capturedTestPrintWrites(sessionId: _newSessionId());
  final largestWrite = writes.fold<int>(
    0,
    (largest, write) => write.length > largest ? write.length : largest,
  );
  final mtu = _mtu;
  if (mtu == null || mtu < largestWrite + 3) {
    throw StateError(
      'Captured test printing requires MTU ${largestWrite + 3} or larger; '
      'negotiated MTU is ${mtu ?? 'unknown'}.',
    );
  }

  _printingCapturedTestLabel = true;
  try {
    await subscribe(characteristic);
    final deviceId = _connectedDevice!;

    const expectedResponses = <int>[
      0x00,
      0x33,
      0x31,
      0x02,
      0x14,
      0xB3,
      0xD3,
      0xB3,
      0xD3,
      0xE4,
    ];
    for (var index = 0; index < writes.length; index++) {
      await _writeAndWaitForCommand(
        deviceId,
        characteristic,
        writes[index],
        expectedResponses[index],
        responseTimeout,
      );
      if (index < writes.length - 1) {
        await Future<void>.delayed(interWriteDelay);
      }
    }

    var completed = false;
    for (var poll = 0; poll < maxStatusPolls; poll++) {
      final status = await _writeAndWaitForCommand(
        deviceId,
        characteristic,
        buildD11hCommand(0xA3, const <int>[0x01]),
        0xB3,
        responseTimeout,
      );
      if (_isCompletedPrintStatus(status)) {
        completed = true;
        break;
      }
      await Future<void>.delayed(statusPollDelay);
    }
    if (!completed) {
      throw TimeoutException(
        'Printer did not report print completion.',
        statusPollDelay * maxStatusPolls,
      );
    }

    await _writeAndWaitForCommand(
      deviceId,
      characteristic,
      buildD11hCommand(0xF3, const <int>[0x01]),
      0xF4,
      responseTimeout,
    );
    await _writeAndWaitForCommand(
      deviceId,
      characteristic,
      buildD11hCommand(0x19, const <int>[0x01, 0x01]),
      0x00,
      responseTimeout,
    );
  } catch (error) {
    _recordError('captured test print failed', error);
    rethrow;
  } finally {
    _printingCapturedTestLabel = false;
  }
}