A simple library/framework to work with Bluetooth Smart (BLE) GATT services and characteristics.
This is a fork of the no longer maintained project at https://github.com/sputnikdev/bluetooth-gatt-parser.
Parsing a standard characteristic (Battery Level, 0x2A19) is a one-liner:
BluetoothGattParserFactory.getDefault().parse("2A19", new byte[] {51}).get("Level").getInteger(null);This prints 51.
- Ships the standard Bluetooth SIG GATT services and characteristics, plus a number of characteristics recovered from the SIG's retired characteristic XML and generated from the current GATT Specification Supplement.
- Parses single- and multi-field characteristics into a user-friendly data format.
- Serializes (writes) single- and multi-field characteristics.
- Validates input against the GATT specification (format types and mandatory fields).
- Supports variable-length array fields (e.g. the RR-Interval list in Heart Rate Measurement).
- Extensible: user-defined services and characteristics via drop-in XML.
- Supports all defined format types, including the
IEEE-11073
SFLOAT/FLOAT(GSSmedfloat16/medfloat32) types.
Add the Maven dependency:
<dependency>
<groupId>org.openhab</groupId>
<artifactId>bluetooth-gatt-parser</artifactId>
<version>X.Y.Z</version>
</dependency>Reading and writing multi-field characteristics:
// A default parser that reads/writes the bundled standard GATT services and characteristics.
BluetoothGattParser parser = BluetoothGattParserFactory.getDefault();
// Read Body Sensor Location (0x2A38) — a single-field characteristic.
GattResponse response = parser.parse("2A38", new byte[] {1}); // 1 == Chest
int sensorLocation = response.get("Body Sensor Location").getInteger(null); // 1 (Chest)
// Read Heart Rate Measurement (0x2A37) — a multi-field characteristic.
response = parser.parse("2A37", new byte[] {20, 74, 13, 3});
int heartRate = response.get("Heart Rate Measurement Value (8 bit resolution)").getInteger(null); // 74
// Write Heart Rate Control Point (0x2A39).
GattRequest request = parser.prepare("2A39");
request.setField("Heart Rate Control Point", 1);
byte[] data = parser.serialize(request);See more in the integration tests: GenericCharacteristicParserIntegrationTest.
The library can add support for custom services/characteristics, or override a bundled one, purely by providing a GATT XML file — no code required. See the bundled Battery Level characteristic for the schema.
BluetoothGattParser parser = BluetoothGattParserFactory.getDefault();
parser.loadExtensionsFromFolder(new File("/path/to/gatt-extensions"));If the generic parser is not enough for a given characteristic, register your own:
BluetoothGattParser parser = BluetoothGattParserFactory.getDefault();
parser.registerParser(CHARACTERISTIC_UUID, myCustomParser);Each characteristic/service is one XML file under src/main/resources/gatt/. A
build-time generator indexes them into gatt_spec_registry.json (the type
attribute of each file must equal its filename).
The Bluetooth SIG retired the machine-readable characteristic XML repository
around 2019. Since then it publishes the GATT Specification Supplement (GSS)
as YAML, where the field structure is present but scaling/unit/presence are
encoded as a rigid mini-grammar inside prose. The characteristics added here were
derived from that GSS: scalars are high fidelity, while cases the prose cannot
express unambiguously carry an inline <!-- REVIEW --> marker rather than a
fabricated value.
Contributions are welcome. Build with Maven:
mvn clean installCut a release to Maven Central:
mvn release:prepare -B
mvn release:perform