Giter VIP home page Giter VIP logo

bluetooth-gatt-parser's Introduction

Maven Central Build Status Coverage Status Codacy Badge Join the chat at https://gitter.im/sputnikdev/bluetooth-gatt-parser

bluetooth-gatt-parser

A simple library/framework to work with Bluetooth Smart (BLE) GATT services and characteristics.

Have a look at an example of parsing a standard characteristic (Battery Level 0x2A19) value:

BluetoothGattParserFactory.getDefault().parse("2A19", new byte[] {51}).get("Level").getInteger(null);

This would print 51.

Features:

  1. Supports 99% of the existing/standard GATT services and characteristics specifications.
  2. Parse/read single and multi field characteristics into a user-friendly data format.
  3. Writing single and multi field characteristics.
  4. Validating input data whether it conforms to GATT specifications (format types and mandatory fields).
  5. Extensibility. User defined services and characteristics.
  6. Support for all defined format types.

Start using the library by including a maven dependency in your project:

<dependency>
  <groupId>org.sputnikdev</groupId>
  <artifactId>bluetooth-gatt-parser</artifactId>
  <version>X.Y.Z</version>
</dependency>

A more complex example of parsing multi-field characteristics (Heart Rate service):

// Getting a default implementation which is capable of reading/writing the standard GATT services and characteristics
BluetoothGattParser parser = BluetoothGattParserFactory.getDefault();

// Reading Body Sensor Location (0x2A38) characteristic (sigle field)
byte[] data = new byte[] {1}; // 1 == Chest
GattResponse response = parser.parse("2A38", data);
String sensorLocation = response.get("Body Sensor Location").getInteger(null); // prints 1 (Chest)

// Reading Heart Rate Measurement (0x2A37) characteristic (multi field)
byte[] data = new byte[] {20, 74, 13, 3};
GattResponse response = parser.parse("2A37", data);
String heartRateValue = response.get("Heart Rate Measurement Value (uint8)").getInteger(null); // prints 74
String rrIntervalValue = response.get("RR-Interval").getInteger(null); // prints 781

// Writing Heart Rate Control Point (0x2A39) characteristic
GattRequest request = parser.prepare("2A39");
request.setField("Heart Rate Control Point", 1); // control value to be sent to a bluetooth device
byte[] data = parser.serialize(request);

See more examples in the integration tests: GenericCharacteristicParserIntegrationTest


Extending the library with user defined services and characteristics

The gatt-parser library is designed to be able to add support for some new custom services/characteristics or to override an existing ("approved") service and characteristic. This can be done by just providing a new GATT XML file which specifies your service and characteristic (have a look at the standard definition for the Battery Level characteristic). The library will read your custom files and build internal rules/conditions for parsing and serialization of your custom characteristics. This means you don't have to write any code to parse/serialize simple or complex custom characteristics.

Loading XML GATT specification files (GATT-like specifications) from a folder:

BluetoothGattParser parser = BluetoothGattParserFactory.getDefault();
File extensionsFolderFile = new File(..);
gattParser.loadExtensionsFromFolder(extensions);

A custom parser can be added for a characteristic if you are not satisfied with the default one

See the default one for a hint and a reference: GenericCharacteristicParser

BluetoothGattParser parser = BluetoothGattParserFactory.getDefault();
CharacteristicParser customParser = new ...; // your own implementation
parser.registerParser(CHARACTERISTIC_UUID, customParser);

Contribution

You are welcome to contribute to the project, the project environment is designed to make it easy by using:

  • Travis CI to release artifacts directly to the Maven Central repository.
  • Code style rules to support clarity and supportability. The results can be seen in the Codacy.
  • Code coverage reports in the Coveralls to maintain sustainability. 100% of code coverage with unittests is the target.

The build process is streamlined by using standard maven tools.

To build the project with maven:

mvn clean install

To cut a new release and upload it to the Maven Central Repository:

mvn release:prepare -B
mvn release:perform

Travis CI process will take care of everything, you will find a new artifact in the Maven Central repository when the release process finishes successfully.

bluetooth-gatt-parser's People

Contributors

gitter-badger avatar meccup avatar vkolotov avatar xrucka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bluetooth-gatt-parser's Issues

Soma blinds - could not read the gatt files

Hi,

edit: I removed the weird copy/paste stuff above :)

Could someone help with the gatt files? The binding is picking up the Soma Blinds but does not seem to be using the gatt files and showing the rest of the characteristics

I’ve got it in a folder which has access by openhab user. Folder is alongside the items and things folders.

org.bluetooth.characteristic.blind_position.xml

<?xml version="1.0" encoding="utf-8"?><!--Copyright 2011 Bluetooth SIG, Inc. All rights reserved.-->
<Characteristic xsi:noNamespaceSchemaLocation="http://schemas.bluetooth.org/Documents/characteristic.xsd"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Blind Position"
                type="org.bluetooth.characteristic.blind_position" uuid="1525" last-modified="2011-12-05"
                approved="No">
    <InformativeText>
        <Abstract>
			Position of blinds
        </Abstract>
    </InformativeText>
    <Value>
        <Field name="Position">
            <Requirement>Mandatory</Requirement>
            <Format>uint8</Format>
            <Unit>org.bluetooth.unit.percentage</Unit>
            <Minimum>0</Minimum>
            <Maximum>100</Maximum>
            <Enumerations>
                <Reserved start="101" end="255"/>
            </Enumerations>
        </Field>
    </Value>
</Characteristic>

org.bluetooth.service.blind_position.xml

<Service xsi:noNamespaceSchemaLocation="http://schemas.bluetooth.org/Documents/service.xsd"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Soma Blinds"
         type="org.bluetooth.service.blind_position" uuid="1861" last-modified="2018-09-06">
<InformativeText>
    <Abstract>
        Soma Blinds control
    </Abstract>
    <Summary>
        Controller for Soma Blinds
    </Summary>
</InformativeText>
<Dependencies>
    <Dependency>This service has no dependencies on other GATT-based services.</Dependency>
</Dependencies>
<GATTRequirements>
    <Requirement subProcedure="Write Characteristic Value">Mandatory</Requirement>
    <Requirement subProcedure="Notification">Mandatory</Requirement>
    <Requirement subProcedure="Read Characteristic Descriptors">Mandatory</Requirement>
    <Requirement subProcedure="Write Characteristic Descriptors">Mandatory</Requirement>
</GATTRequirements>
<Transports>
    <Classic>false</Classic>
    <LowEnergy>true</LowEnergy>
</Transports>
<ErrorCodes>
</ErrorCodes>
<Characteristics>
    <Characteristic name="Blind Position"
                    type="org.bluetooth.characteristic.blind_position">
        <InformativeText>Blind control
        </InformativeText>
        <Requirement>Mandatory</Requirement>
        <Properties>
            <Read>Excluded</Read>
            <Write>Excluded</Write>
            <WriteWithoutResponse>Excluded</WriteWithoutResponse>
            <SignedWrite>Excluded</SignedWrite>
            <ReliableWrite>Excluded</ReliableWrite>
            <Notify>Excluded</Notify>
            <Indicate>Mandatory</Indicate>
            <WritableAuxiliaries>Excluded</WritableAuxiliaries>
            <Broadcast>Excluded</Broadcast>
        </Properties>
    </Characteristic>
</Characteristics>
</Service>

The blinds have 4 characteristics - I’ve just tried to get the one working thus far.
from a python file I currently use control.py -

battery

BATTERY_SERVICE_UUID = "0000180f-0000-1000-8000-00805f9b34fb".lower()
BATTERY_CHARACTERISTIC_UUID = "00002a19-0000-1000-8000-00805f9b34fb".lower()

motor control

MOTOR_SERVICE_UUID = "00001861-B87F-490C-92CB-11BA5EA5167C".lower()
MOTOR_STATE_CHARACTERISTIC_UUID = "00001525-B87F-490C-92CB-11BA5EA5167C".lower()
MOTOR_CONTROL_CHARACTERISTIC_UUID = "00001530-B87F-490C-92CB-11BA5EA5167C".lower()
MOTOR_TARGET_CHARACTERISTIC_UUID = "00001526-B87F-490C-92CB-11BA5EA5167C".lower()
MOTOR_MOVE_UP = 69
MOTOR_MOVE_DOWN = 96

Original py file is here https://github.com/paolotremadio/SOMA-Smart-Shades-HTTP-API

Thanks

Incorrect handling Characteristic flag field (GatParser assumes that flags are 1 bit long only)

Bit tag can be longer that 1 bit. GattParser should support reading the such flags, for example:

<Bit index="20" size="2" name="Distribute System Support">
    <Enumerations>
        <Enumeration key="0" value="Unspecified (legacy sensor)"/>
        <Enumeration key="1" value="Not for use in a distributed system"/>
        <Enumeration key="2" value="Can be used in a distributed system"/>
        <Enumeration key="3" value="RFU"/>
    </Enumerations>
</Bit>

No interface method stream()

On on older Android device running Android 6, I'm seeing the following crash:

java.lang.NoSuchMethodError: No interface method stream()Ljava/util/stream/Stream; in class Ljava/util/Set; or its super classes (declaration of 'java.util.Set' appears in /system/framework/core-libart.jar)
        at org.sputnikdev.bluetooth.gattparser.spec.BluetoothGattSpecificationReader.loadExtensionsFromCatalogResources(BluetoothGattSpecificationReader.java:263)
        at org.sputnikdev.bluetooth.gattparser.spec.BluetoothGattSpecificationReader.<init>(BluetoothGattSpecificationReader.java:92)
        at org.sputnikdev.bluetooth.gattparser.BluetoothGattParserFactory.getSpecificationReader(BluetoothGattParserFactory.java:59)
        at org.sputnikdev.bluetooth.gattparser.BluetoothGattParserFactory.getDefault(BluetoothGattParserFactory.java:74)

Add support for custom parsers to handle invalid characteristic specifications

Some bluetooth GATT specifications (XML files) are not valid to use them in automated process to parse characteristic values, these are the following characteristics:

  • Bond Management Control Point
  • CGM Measurement
  • CGM Session Run Time
  • CGM Session Start Time
  • CGM Specific Ops Control Point
  • CGM Status
  • Cycling Power Control Point
  • Cycling Power Measurement
  • Cycling Power Vector
  • LN Control Point
  • Location and Speed Characteristic
  • Navigation
  • Position Quality
  • SC Control Point
  • TDS Control Point

Please note that some of the characteristics above are not used for read operations at all (write operations only).

Gatt parser should be flexible enough to allow users to parse these specifications anyway. There might be some options:

  1. Override XML specification file with a correct definition of characteristic.
  2. Allow users to plug in a custom parser (java code).

Characteristic is not valid for read: 2AD2

I'm trying to load the Indoor Bike data characteristic (which is part of Fitness Machine service), using the loadExtensionsFromFolder() method.

I have the following code:

BluetoothGattParser bluetoothGattParser = BluetoothGattParserFactory.getDefault();
bluetoothGattParser.loadExtensionsFromFolder("/gatt_custom");

gatt_custom is a folder off of root that contains two sub-folders, service and characteristic. Within characteristic, I have this xml file for Indoor Bike data characteristic: https://share.getcloudapp.com/z8uXZAYX

Any idea what I'm wrong? I keep getting:
org.sputnikdev.bluetooth.gattparser.CharacteristicFormatException: Characteristic is not valid for read: 2AD2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.