1.2.1.1 Initializing the BLE Software

Initializing BLE Stack

Before using any BLE Stack relevant API, BLE component must be initialized. It is strongly recommend that BLE component be initialized during system initialization. BLE component only operates with RTOS, the relevant parameters of RTOS must be assigned to BLE component. Also, device address is necessary for BLE component.

    //BLE Queue definition
    #define QUEUE_LENGTH_BLE (16)
    #define QUEUE_ITEM_SIZE_BLE (sizeof(void *))

    //BLE library Initialization Data
    OSAL_QUEUE_HANDLE_TYPE bleRequestQueueHandle;
    OSAL_API_LIST_TYPE     osalAPIList;
    uint8_t                bdAddr[7];     //address type + address[6]

    // Create BLE Stack Message QUEUE
    OSAL_QUEUE_Create(&bleRequestQueueHandle, QUEUE_LENGTH_BLE, QUEUE_ITEM_SIZE_BLE);

    // Initialize BLE Stack
    if(IB_GetBdAddr(bdAddr) == 0)
    {
        BT_SYS_Init(&bleRequestQueueHandle, &osalAPIList, NULL, bdAddr);
    }
    else
    {
        BT_SYS_Init(&bleRequestQueueHandle, &osalAPIList, NULL, NULL);
    }

BLE Stack provides various APIs for application, and those APIs belong to the specific module within dedicated group. Currently there are four groups inside the BLE Stack. Application shall initialize the dedicated group and modules in the BLE Stack if needs.

  • GAP: Provide the user interface for discovery of BLE devices, broadcast of BLE device, and connection management

  • L2CAP: Provide the user interface for credit based flow control procedure

  • GATT: Provide the user interface for GATT server and client procedure

  • SMP: Provide the user interface for pairing, authentication, and security configuration

Example of initializing modules in GAP group:

    //Initialize BLE GAP main module
    BLE_GAP_Init();
    
    //Initialize BLE GAP advertising module
    BLE_GAP_AdvInit();

Example of initializing modules in L2CAP group:

    //Initialize BLE L2cap main module
    BLE_L2CAP_Init();

    //Initialize BLE L2cap credit based flow control module
    BLE_L2CAP_CbInit();

Example of initializing modules in GATT group:

    //Initialize BLE GATT server module
    GATTS_Init();

    //Initialize BLE GATT client module
    GATTC_Init();

Example of initializing modules in SMP group:

    //Initialize BLE SMP main module
    BLE_SMP_Init();

BLE Stack would generate events to inform application if there is any status changed or activity. Application may need to get the relevant information from BLE Stack and do the corresponding procedure. Therefore, application shall register BLE Stack callback function after BLE Stack initialized.

Example of registering BLE Stack callback event:

    /* Register BLE Stack callback event*/
    STACK_EventRegister(APP_BleStackCb);

Initializing BLE Middleware

BLE component provides two modules in BLE Middleware to assist application. Application shall initialize the dedicated modules if needs.

  • BLE_DM: Handle pairing procedures, connection management and the record of pairing information. Provide the simplified user interface for application.

  • BLE_DD: Handle the service discovery procedures. It shall be included if a GATT client profile is enabled.

Example of initializing modules in BLE Middleware:

    //Initialize BLE device manager module
    BLE_DM_Init();

    //Initialize BLE device discovery module
    BLE_DD_Init();

BLE Middleware module would generate events to inform application if there is any status changed or activity. Application may need to get the relevant information from BLE Middleware and do the corresponding procedure. Therefore, application shall register BLE Middleware callback function after BLE Middleware initialized.

Example of registering BLE middleware callback event:

    /* Register BLE DM module callback event */
    BLE_DM_EventRegister(APP_DmEvtHandler);

    /* Register BLE DD module callback event */
    BLE_DD_EventRegister(APP_DdEvtHandler);

Initializing BLE Profile

BLE component provides some example profiles. Application shall initialize the profiles if needs.

Example of initializing BLE profiles:

    //Initialize BLE transparent server profile
    BLE_TRSPS_Init();

    //Initialize BLE transparent client profile
    BLE_TRSPC_Init();

BLE profiles module would generate events to inform application if there is any status changed or activity. Application may need to get the relevant information from BLE profiles and do the corresponding procedure. Therefore, application shall register BLE profiles callback function after BLE profiles initialized.

Example of registering BLE profiles callback event:

    /* Register BLE transparent server callback event */
    BLE_TRSPS_EventRegistration(APP_TrspsEvtHandler);

    /* Register BLE transparent client callback event */
    BLE_TRSPC_EventRegistration(APP_TrspcEvtHandler);

Initializing BLE Service

BLE component provides some example services. Some of the services are used by the specific profile, and some of them may be used by application directly.

Example of initializing BLE service:

    //Initialize BLE device information service
    BLE_DIS_Add();

    //Initialize BLE battery service
    BLE_BAS_Add();