writeHex method

Future<void> writeHex(
  1. BleCharacteristic characteristic,
  2. String hex, {
  3. required BleWriteMode mode,
})

Implementation

Future<void> writeHex(
  BleCharacteristic characteristic,
  String hex, {
  required BleWriteMode mode,
}) async {
  _ensureConnected('write');
  final deviceId = _connectedDevice!;
  final supportsMode = switch (mode) {
    BleWriteMode.withResponse => characteristic.properties.contains(
      BleCharacteristicProperty.write,
    ),
    BleWriteMode.withoutResponse => characteristic.properties.contains(
      BleCharacteristicProperty.writeWithoutResponse,
    ),
  };
  if (!supportsMode) {
    throw StateError('Characteristic does not support ${mode.name}.');
  }

  final bytes = parseHexBytes(hex);
  if (bytes.isEmpty) {
    throw ArgumentError.value(hex, 'hex', 'Write bytes must not be empty.');
  }

  try {
    await _transport.write(deviceId, characteristic, bytes, mode: mode);
    _record(
      ProbeEventKind.write,
      '${characteristic.characteristicUuid} ${_formatPayload(bytes)}',
    );
  } catch (error) {
    _recordError('write failed', error);
    rethrow;
  }
}