Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

Using Notifications in BLE Peripheral Mode

lock attach
Attachments are accessible only for community members.

Using Notifications in BLE Peripheral Mode

JaeyoungY_71
Employee
Employee
First comment on KBA 25 solutions authored 10 solutions authored

In order to use notifications in BLE peripheral mode, you will need to define the CCCD in the GATT database and send the notifications or indications according to the value the client sets it. I attached a zip file that has addition of code using a CCCD for notifications in the ble_proximity_reporter. Please look at the following for a description of changes made (in red).

 

** This was verified on a BCM943341WCD1 board with the iPhone LightBlue app.

 

1. First you will need to define the characteristic descriptor in wiced_bt_gatt_db.c, and also add properties to the characteristic that you want to receive notifications.

 

wiced_bt_gatt_db.c

<Modify>

CHARACTERISTIC_UUID16 (HDLC_TX_POWER_LEVEL,

                      HDLC_TX_POWER_LEVEL_VALUE,

                      GATT_UUID_TX_POWER_LEVEL,

                      LEGATTDB_CHAR_PROP_READ | LEGATTDB_CHAR_PROP_NOTIFY | LEGATTDB_CHAR_PROP_INDICATE,

                      LEGATTDB_PERM_READABLE),

 

 

<Add>

CHAR_DESCRIPTOR_UUID16_WRITABLE (HDLC_TX_POWER_LEVEL_DESCRIPTOR,

                      GATT_UUID_CHAR_CLIENT_CONFIG,

                      LEGATTDB_PERM_READABLE | LEGATTDB_PERM_WRITE_REQ ),

 

 

 

2. In wiced_bt_gatt_db.h, add a handle for HDLC_TX_POWER_LEVEL_DESCRIPTOR,

 

wiced_bt_gatt_db.h

    // ***** Primary service 'TX Power'

    HDLS_TX_POWER,

    HDLC_TX_POWER_LEVEL,

    HDLC_TX_POWER_LEVEL_VALUE,

    HDLC_TX_POWER_LEVEL_DESCRIPTOR,

 

 

 

3. In ble_proximity_reporter.c, add a global descriptor value and add a clause to function ble_proximity_gatt_write_request() to enable the client writing to the descriptor.

 

ble_proximity_reporter.c

static int8_t  proximity_tx_power_level_descriptor;

 

 

case HDLC_TX_POWER_LEVEL_DESCRIPTOR:

      proximity_tx_power_level_descriptor = attribute_value;

      WPRINT_APP_INFO( ("POWER_LEVEL_DESCRIPTOR written as 0x%x\n", attribute_value));

      break;

 

 

4. Once the descriptor is defined, we are going to use the function wiced_bt_gatt_send_notification() to send notifications every 2 seconds. We shall set the value to decrease from 20 to 1. The following code should be added in application_start() afterwiced_bt_stack_init() in ble_proximity_reporter.c.

 

ble_proximity_reporter.c

void application_start( void )

{

    /* Initialize WICED platform */

    wiced_init( );

 

    /* Initialize Bluetooth controller and host stack */

    wiced_bt_stack_init( ble_proximity_management_callback, &wiced_bt_cfg_settings, wiced_bt_cfg_buf_pools );

 

    wiced_rtos_delay_milliseconds(2000);

    for (uint8_t i = 20; i > 0; i-- )

    {

        // If notification is set

        if ( proximity_tx_power_level_descriptor == 0x1 )

        {

            proximity_tx_power_level = i;

            wiced_bt_gatt_send_notification (proximity_connection_handle, HDLC_TX_POWER_LEVEL_VALUE, sizeof(proximity_tx_power_level), &proximity_tx_power_level );

        }

        wiced_rtos_delay_milliseconds(2000);

    }

}

 

 

**For indications, use the function wiced_bt_gatt_send_indication() when the proximity_tx_power_level_descriptor == 0x2

 

 

5. For the first parameter in the function, proximity_connection_handle, this should be defined as a global variable and maintained inble_proximity_gatt_cback(). Whenever a new connection is established it should store the connection handle, and when connection is lost it should reset it to 0. ( Add red code in ble_proximity_reporter.c )

 

ble_proximity_reporter.c

static uint16_t proximity_connection_handle = 0;

 

/* GATT event handler */

static wiced_bt_gatt_status_t ble_proximity_gatt_cback( wiced_bt_gatt_evt_t event, wiced_bt_gatt_event_data_t *p_event_data )

{

    wiced_bt_gatt_status_t status = WICED_BT_GATT_SUCCESS;

    uint8_t *bda;

 

    switch ( event )

    {

        case GATT_CONNECTION_STATUS_EVT:

            /* GATT connection status change */

            bda = p_event_data->connection_status.bd_addr;

            WPRINT_BT_APP_INFO( ("GATT connection to [%02X:%02X:%02X:%02X:%02X:%02X] %s.\n", bda[0], bda[1], bda[2], bda[3], bda[4], bda[5], (p_event_data->connection_status.connected ? "established" : "released")) );

 

            if ( p_event_data->connection_status.connected )

            {

                /* Connection established. Get current TX power  (required for setting TX power attribute in GATT database) */

                wiced_bt_dev_read_tx_power( p_event_data->connection_status.bd_addr, p_event_data->connection_status.transport, (wiced_bt_dev_cmpl_cback_t *) ble_proximity_tx_power_callback );

 

                /* Store information  */

                proximity_connection_handle = p_event_data->connection_status.conn_id;

 

                /* Disable connectability. */

                wiced_bt_start_advertisements( BTM_BLE_ADVERT_OFF, 0, NULL );

            }

            else

            {

                /* Connection released. Re-enable BLE connectability. */

                wiced_bt_start_advertisements( BTM_BLE_ADVERT_UNDIRECTED_HIGH, 0, NULL );

                /* test code for directed adv */

                /* wiced_bt_start_advertisements (BTM_BLE_ADVERT_DIRECTED_HIGH, 0, p_event_data->connection_status.bd_addr); */

 

                proximity_connection_handle = 0;

 

                WPRINT_BT_APP_INFO( ("Waiting for proximity monitor to connect...\n") );

            }

            break;

 

            ...

 

 

 

6. That's it! Now compile the code and download it to your WICED board. When you connect to "WICED Proximity" and select "Tx Power Level" from the LightBlue app of your iPhone (below) or the WICED Smart Explorer app in your Android device.

 

You should see the notifications arriving once you set "Listen to notifications". This app only counts down from 20 to 1 every 2 seconds after it resets, so if you don't see it counting down on your client try resetting the WICED board and connecting to it faster.

 

  IMG_0841.jpg

 

I hope this helped in getting you started with notifications!

 

Thanks,

Jaeyoung

Attachments
1411 Views