Discover all primary services

The following API can be used to discover all the Primary Services in a Server’s database:

bleResult_t **GattClient\_DiscoverAllPrimaryServices**
(
    deviceId_t         deviceId,
    gattService_t *    aOutPrimaryServices,
    uint8_t            maxServiceCount,
    uint8_t *          pOutDiscoveredCount
);

The aOutPrimaryServices parameter must point to an allocated array of services. The size of the array must be equal to the value of the maxServiceCount parameter, which is passed to make sure the GATT module does not attempt to write past the end of the array if more Services are discovered than expected.

The pOutDiscoveredCount parameter must point to a static variable because the GATT module uses it to write the number of Services discovered at the end of the procedure. This number is less than or equal to the maxServiceCount.

If there is equality, it is possible that the Server contains more than maxServiceCount Services, but they could not be discovered as a result of the array size limitation. It is the application developer’s responsibility to allocate a large enough number according to the expected contents of the Server’s database.

In the following example, the application expects to find no more than 10 Services on the Server.

**\#define** mcMaxPrimaryServices_c 10
**static **gattService_t primaryServices[mcMaxPrimaryServices_c];
uint8_t mcPrimaryServices;
bleResult_t result = **GattClient\_DiscoverAllPrimaryServices**
(
    deviceId,
    primaryServices,
    mcMaxPrimaryServices_c,
    &mcPrimaryServices
);
**if** (gBleSuccess_c != result)
{
    /* Treat error */
}

The operation triggers the Client Procedure Callback when complete. The application may read the number of discovered services and each service’s handle range and UUID.

**void ****gattClientProcedureCallback**
(
    deviceId_t             deviceId,
    gattProcedureType_t    procedureType,
    gattProcedureResult_t  procedureResult,
    bleResult_t            error
)
{
    **switch** (procedureType)
    {
        /* ... */
        **case ***gGattProcDiscoverAllPrimaryServices\_c*:
            **if** (*gGattProcSuccess\_c* == procedureResult)
            {
                /* Read number of discovered services */
                PRINT( mcPrimaryServices );
                /* Read each service's handle range and UUID */
                **for** (**int** j = 0; j < mcPrimaryServices; j++)
                {
                    PRINT( primaryServices[j]. startHandle );
                    PRINT( primaryServices[j]. endHandle );
                    PRINT( primaryServices[j]. uuidType );
                    PRINT( primaryServices[j]. uuid );
                }
            }
            **else**
            {
                /* Handle error */
                PRINT( error );
            }
            **break**;
        /* ... */
    }
}

Parent topic:Service and characteristic discovery