src.bluetooth_sig.core.encoder

Characteristic encoding, value creation, and data validation.

Provides encode_characteristic, create_value, validate_characteristic_data, and type introspection for characteristic value types.

Attributes

Name

Description

T

logger

Classes

Name

Description

CharacteristicEncoder

Handles characteristic encoding, value creation, and data validation.

Module Contents

class src.bluetooth_sig.core.encoder.CharacteristicEncoder(parser: src.bluetooth_sig.core.parser.CharacteristicParser)

Handles characteristic encoding, value creation, and data validation.

Takes a CharacteristicParser reference for validate_characteristic_data, which needs to attempt a parse to check data format validity.

create_value(uuid: str, **kwargs: Any) Any

Create a properly typed value instance for a characteristic.

Parameters:
  • uuid – The characteristic UUID

  • **kwargs – Field values for the characteristic’s type

Returns:

Properly typed value instance

Raises:
  • ValueError – If UUID is invalid or characteristic not found

  • TypeError – If kwargs don’t match the characteristic’s expected fields

encode_characteristic(char: type[src.bluetooth_sig.gatt.characteristics.base.BaseCharacteristic[T]], value: T, validate: bool = ...) bytes
encode_characteristic(char: str, value: Any, validate: bool = ...) bytes

Encode a value for writing to a characteristic.

Parameters:
  • char – Characteristic class (type-safe) or UUID string (not type-safe).

  • value – The value to encode. Type is checked when using characteristic class.

  • validate – If True, validates the value before encoding (default: True)

Returns:

Encoded bytes ready to write to the characteristic

Raises:
validate_characteristic_data(uuid: str, data: bytes) src.bluetooth_sig.types.ValidationResult

Validate characteristic data format against SIG specifications.

Parameters:
  • uuid – The characteristic UUID

  • data – Raw data bytes to validate

Returns:

ValidationResult with validation details

src.bluetooth_sig.core.encoder.T
src.bluetooth_sig.core.encoder.logger