Declaring a service

There are two types of Services:

  • Primary Services

  • Secondary Services - these are only to be included by other Primary or Secondary Services

The Service declaration attribute has one of these UUIDs, as defined by the Bluetooth SIG:

  • 0x2800 a.k.a. <<Primary Service>> - for a Primary Service declaration

  • 0x2801 a.k.a. <<Secondary Service>> - for a Secondary Service declaration

The Service declaration attribute permissions are read-only and no authentication required. The Service declaration attribute value contains the Service UUID. The Service Range starts from the Service declaration and ends at the next service declaration. All the Characteristics declared within the Service Range are considered to belong to that Service. For a more comprehensive list of SIG defied UUID values, check ble_sig_defines.h.

Service declaration macros

The following macros are to be used for declaring a Service:

  • PRIMARY_SERVICE (name, uuid16)

    • Most often used.

    • The name parameter is common to all macros; it is a universal, user-friendly identifier for the generated attribute.

    • The uuid16 is a 2-byte SIG-defined UUID, written in 0xZZZZ format.

  • PRIMARY_SERVICE_UUID32 (name, uuid32)

    • This macro is used for a 4-byte, SIG-defined UUID, written in 0xZZZZZZZZ format.

  • PRIMARY_SERVICE_UUID128 (name, uuid128)

    • The uuid128 is the friendly name given to the custom UUID in the gatt_uuid128.h file.

  • SECONDARY _SERVICE (name, uuid16)

  • SECONDARY_SERVICE_UUID32 (name, uuid32)

  • SECONDARY _SERVICE_UUID128 (name, uuid128)

    • All three are similar to Primary Service declarations.

Parent topic:Declaring a service

Include declaration macros

Secondary Services are meant to be included by other Services, usually by Primary Services. Primary Services may also be included by other Primary Services. The inclusion is done using the Include declaration macro:

  • INCLUDE (service_name)

    • The service_name parameter is the friendly name used to declare the Secondary Service.

    • This macro is used only for Secondary Services with a SIG-defined, 2-byte, Service UUID.

  • INCLUDE_CUSTOM (service_name)

    • This macro is used for Secondary Services that have either a 4-byte UUID or a 16-byte UUID.

The effect of the service inclusion is that the including Service is considered to contain all the Characteristics of the included Service.

Parent topic:Declaring a service

Parent topic:Creating static GATT database