GR5x series BLE knowledge and application topics (6) – How to enable various broadcasts

Article directory

  • 1. Start traditional radio
    • 1.1 Set broadcast parameters
    • 1.2 Set broadcast data (optional)
    • 1.3 Set scan response data (optional)
    • 1.4 Turn on broadcast
    • 1.5 Call the callback function app_gap_adv_start_cb after the broadcast is successfully started.
  • 2. Enable extended broadcast
    • 2.1 Set extended broadcast parameters.
    • 2.2 Set broadcast data (optional)
    • 2.3 Set scan response data (optional)
    • 2.4 Turn on broadcast
  • 3. Enable periodic broadcasting
    • 3.1 Set broadcast parameters.
    • 3.2 Set broadcast data
    • 3.3 Set periodic broadcast data (optional)
    • 3.4 Turn on broadcast
    • 3.5 Call the callback function app_gap_adv_start_cb after the broadcast is successfully started
  • Preface

    Through the “GR5x Series BLE Knowledge and Application Topic”, we systematically introduce some knowledge concepts involved in BLE, and how to use the SDK of Goodix GR5xx series chips for BLE application development.
    The introduction and selection reference of the GR5xx series Bluetooth chips can be obtained from the following web page.

    • Goodix Bluetooth chip product introduction
    • Goodix Bluetooth chip selection list

    This article is based on the Goodix GR55xx Bluetooth chip SDK project [${SDK}\projects\ble\ble_basic_example] as an example to illustrate how to enable several typical broadcasts.

1. Start traditional broadcasting

1.1 Set broadcast parameters

 s_gap_adv_param.adv_intv_max = APP_ADV_MAX_INTERVAL;
s_gap_adv_param.adv_intv_min = APP_ADV_MIN_INTERVAL;
s_gap_adv_param.adv_mode = GAP_ADV_TYPE_ADV_IND;
s_gap_adv_param.chnl_map = GAP_ADV_CHANNEL_37_38_39;
s_gap_adv_param.disc_mode = GAP_DISC_MODE_NON_DISCOVERABLE;
s_gap_adv_param.filter_pol = GAP_ADV_ALLOW_SCAN_ANY_CON_ANY;
error_code = ble_gap_adv_param_set(0, BLE_GAP_OWN_ADDR_STATIC, & amp;s_gap_adv_param);
APP_ERROR_CHECK(error_code);

illustrate:

  • The discoverability mode of directed broadcast can only be set to GAP_DISC_MODE_NON_DISCOVERABLE (not discoverable).
  • If the discovery mode is GAP_DISC_MODE_BROADCASTER, the broadcast mode can only be set to GAP_ADV_TYPE_ADV_NONCONN_IND (cannot be connected or scanned).
  • The peer_addr parameter is only used when directed broadcast or controller privacy is enabled (that is, the second parameter of the ble_gap_privacy_params_set function, BLE_GAP_PRIV_CFG_PRIV_EN_BIT, is set).
  • Code path:
SDK_Folder\projects\ble\ble_basic_example\ble_app_gap_legacy_adv\Src\user\user_app.c

1.2 Set broadcast data (optional)

When adv_mode is GAP_ADV_TYPE_ADV_HIGH_DIRECT_IND or GAP_ADV_TYPE_ADV_LOW_DIRECT_IND, there is no need to set broadcast data. In other cases, it is required.

1.3 Set scan response data (optional)

When adv_mode is GAP_ADV_TYPE_ADV_IND or GAP_ADV_TYPE_ADV_SCAN_IND, the scan response data needs to be set. In other cases, there is no need to set it.
static const uint8_t s_adv_data_set[] =
{<!-- -->
        0x03,
        BLE_GAP_AD_TYPE_COMPLETE_LIST_16_BIT_UUID,
        0x01, 0x00,
};
static const uint8_t s_adv_rsp_data_set[] =
{<!-- -->
        0x0b,
        BLE_GAP_AD_TYPE_COMPLETE_NAME,
        'L', 'e', 'g', 'a', 'c', 'y', '_', 'A', ' D', 'V',
};
error_code = ble_gap_adv_data_set(0, BLE_GAP_ADV_DATA_TYPE_DATA,s_adv_data_set, sizeof(s_adv_data_set));
APP_ERROR_CHECK(error_code);
error_code = ble_gap_adv_data_set(0, BLE_GAP_ADV_DATA_TYPE_SCAN_RSP, s_adv_rsp_data_set, sizeof(s_adv_rsp_data_set));
APP_ERROR_CHECK(error_code);

illustrate:

  • adv data and adv rsp data must be combined according to the format specified by Bluetooth Core Spec, that is (length, type, data). The length indicated by length is the total length of the type field and data field.
  • Code path:
SDK_Folder\projects\ble\ble_basic_example\ble_app_gap_legacy_adv\Src\user\user_app.c

1.4 Turn on broadcast

When calling the ble_gap_adv_start function to start broadcasting, the user needs to pass in the parameter adv_idx to specify the broadcast instance index. Supports the establishment of up to 5 traditional broadcasts at the same time, so the value range of adv_idx is [0, 1, 2, 3, 4].

s_gap_adv_time_param.duration = 0;
s_gap_adv_time_param.max_adv_evt = 0;
error_code = ble_gap_adv_start(0, & amp;s_gap_adv_time_param);
APP_ERROR_CHECK(error_code);

illustrate:

  • Code path: SDK_Folder\projects\ble\ble_basic_example\ble_app_gap_legacy_adv\Src\user\user_app.c

1.5 Call the callback function app_gap_adv_start_cb after the broadcast is successfully started

illustrate:

  • If the user wants to update the broadcast parameters, he can call the ble_gap_adv_data_set and ble_gap_adv_param_set interfaces to reconfigure the broadcast data and broadcast parameters after the broadcast stops, and then call ble_gap_adv_start to start the broadcast.
  • If the scan_req_ind_en value of the broadcast parameter is true, the app_gap_scan_req_ind_cb callback function will be called when a scan request sent by the peer device is received.

2. Enable extended broadcast

2.1 Set extended broadcast parameters.

s_gap_adv_param.type = GAP_ADV_TYPE_EXTENDED;
s_gap_adv_param.disc_mode = GAP_DISC_MODE_GEN_DISCOVERABLE;
/* The advertisement shall not be both connectable and scannable, and
High duty cycle directed advertising cannot be used */
s_gap_adv_param.prop = GAP_ADV_PROP_CONNECTABLE_BIT;
s_gap_adv_param.max_tx_pwr = 0;
s_gap_adv_param.filter_pol = GAP_ADV_ALLOW_SCAN_ANY_CON_ANY;
memset( & amp;s_gap_adv_param.peer_addr, 0, sizeof(gap_bdaddr_t));
s_gap_adv_param.prim_cfg.adv_intv_min = APP_ADV_MIN_INTERVAL;
s_gap_adv_param.prim_cfg.adv_intv_max = APP_ADV_MAX_INTERVAL;
s_gap_adv_param.prim_cfg.chnl_map = GAP_ADV_CHANNEL_37_38_39;
s_gap_adv_param.prim_cfg.phy = GAP_PHY_1MBPS_VALUE;
s_gap_adv_param.second_cfg.max_skip = 0;
s_gap_adv_param.second_cfg.phy = GAP_PHY_1MBPS_VALUE;
s_gap_adv_param.second_cfg.adv_sid = 0x00;
s_gap_adv_param.period_cfg.adv_intv_min = 0;
s_gap_adv_param.period_cfg.adv_intv_max = 0;
error_code = ble_gap_ext_adv_param_set(0, BLE_GAP_OWN_ADDR_STATIC, &s_gap_adv_param);
APP_ERROR_CHECK(error_code);

illustrate:

  • The GAP_ADV_PROP_DIRECTED_BIT in the prop parameter can be set in two cases. One case is: the disc_mode parameter is set to GAP_DISC_MODE_NON_DISCOVERABLE; the other case is: the disc_mode parameter is set to GAP_DISC_MODE_BROADCASTER, and the parameter GAP_ADV_PROP_ANONYMOUS_BIT is set.
  • Extended broadcast does not support High Duty directed broadcast, so the GAP_ADV_PROP_HDC_BIT of the prop parameter cannot be set in extended broadcast.
  • Extended broadcasts do not support broadcasts that can be scanned and connected, so GAP_ADV_PROP_CONNECTABLE_BIT and GAP_ADV_PROP_SCANNABLE_BIT in the prop parameters cannot be set at the same time.
  • If GAP_ADV_PROP_ANONYMOUS_BIT is set in the prop parameter, the disc_mode parameter can only be set to: GAP_DISC_MODE_NON_DISCOVERABLE or GAP_DISC_MODE_BROADCASTER.
  • If GAP_ADV_PROP_ANONYMOUS_BIT is set in the prop parameter, neither GAP_ADV_PROP_CONNECTABLE_BIT nor GAP_ADV_PROP_SCANNABLE_BIT in the prop parameter can be set.
  • The peer_addr parameter is only used when directed broadcast or controller privacy is enabled (that is, the second parameter of the ble_gap_privacy_params_set function, BLE_GAP_PRIV_CFG_PRIV_EN_BIT, is set).
  • Code path: SDK_Folder\projects\ble\ble_basic_example\ble_app_gap_extended_adv\Src\user\user_app.c

2.2 Set broadcast data (optional)

If GAP_ADV_PROP_SCANNABLE_BIT is set in the prop parameter, no setting is required.

2.3 Set scan response data (optional)

It needs to be set if GAP_ADV_PROP_SCANNABLE_BIT is set in the prop parameter.

static const uint8_t s_adv_data_set[] =
{<!-- -->
        0x03,
        BLE_GAP_AD_TYPE_COMPLETE_LIST_16_BIT_UUID,
        0x01, 0x00,
};
static const uint8_t s_adv_rsp_data_set[] =
{<!-- -->
        0x0d,
        BLE_GAP_AD_TYPE_COMPLETE_NAME,
        'E', 'x', 't', 'e', 'n', 'd', 'e', 'd', ' _', 'A', 'D', 'V',
};
error_code = ble_gap_adv_data_set(0, BLE_GAP_ADV_DATA_TYPE_DATA,
s_adv_data_set,sizeof(s_adv_data_set));
APP_ERROR_CHECK(error_code);
error_code = ble_gap_adv_data_set(0, BLE_GAP_ADV_DATA_TYPE_SCAN_RSP,
s_adv_rsp_data_set,sizeof(s_adv_rsp_data_set));
APP_ERROR_CHECK(error_code);

illustrate:

  • adv data and adv rsp data must be combined according to the format specified by Bluetooth Core Spec, that is (length, type, data). The length of length is the total length of the type field and data field.
  • Code path: SDK_Folder\projects\ble\ble_basic_example\ble_app_gap_extended_adv\Src\user\user_app.c

2.4 Turn on broadcast

When calling the ble_gap_adv_start function to start broadcasting, the user needs to pass in the parameter adv_idx to specify the broadcast instance index. Supports the establishment of up to 5 extended broadcasts at the same time, so the value range of adv_idx is [0, 1, 2, 3, 4].

s_gap_adv_time_param.duration = 0;
s_gap_adv_time_param.max_adv_evt = 0;
error_code = ble_gap_adv_start(0, & amp;s_gap_adv_time_param);
APP_ERROR_CHECK(error_code);

illustrate:

  • Code path: SDK_Folder\projects\ble\ble_basic_example\ble_app_gap_extended_adv\Src\user\user_app.c
  • After the broadcast is successfully started, the callback function app_gap_adv_start_cb is called.
  • If GAP_ADV_PROP_SCAN_REQ_NTF_EN_BIT in the prop of the broadcast parameter is set, then the app_gap_scan_req_ind_cb callback function will be called when a scan request sent from the peer device is received.

3. Enable periodic broadcast

3.1 Set broadcast parameters.

s_gap_adv_param.type = GAP_ADV_TYPE_PERIODIC;
s_gap_adv_param.disc_mode = GAP_DISC_MODE_GEN_DISCOVERABLE;
/* Connectable, anonymous, scannable, high duty circle bit must be set to 0 */
s_gap_adv_param.prop = 0;
s_gap_adv_param.max_tx_pwr = 0;
s_gap_adv_param.filter_pol = GAP_ADV_ALLOW_SCAN_ANY_CON_ANY;
memset( & amp;s_gap_adv_param.peer_addr, 0, sizeof(gap_bdaddr_t));
s_gap_adv_param.prim_cfg.adv_intv_min = APP_PRIMARY_ADV_MIN_INTERVAL;
s_gap_adv_param.prim_cfg.adv_intv_max = APP_PRIMARY_ADV_MAX_INTERVAL;
s_gap_adv_param.prim_cfg.chnl_map = GAP_ADV_CHANNEL_37_38_39;
s_gap_adv_param.prim_cfg.phy = GAP_PHY_1MBPS_VALUE;
s_gap_adv_param.second_cfg.max_skip = 0;
s_gap_adv_param.second_cfg.phy = GAP_PHY_1MBPS_VALUE;
s_gap_adv_param.second_cfg.adv_sid = 0x00;
s_gap_adv_param.period_cfg.adv_intv_min = APP_PERIODIC_ADV_MIN_INTERVAL;
s_gap_adv_param.period_cfg.adv_intv_max = APP_PERIODIC_ADV_MAX_INTERVAL;
error_code = ble_gap_ext_adv_param_set(0, BLE_GAP_OWN_ADDR_STATIC, &s_gap_adv_param);
APP_ERROR_CHECK(error_code);

illustrate:

  • During the process of configuring periodic broadcast parameters, the GAP_ADV_PROP_CONNECTABLE_BIT, GAP_ADV_PROP_SCANNABLE_BIT, GAP_ADV_PROP_ANONYMOUS_BIT, and GAP_ADV_PROP_HDC_BIT of the prop parameters cannot be set.
  • The peer_addr parameter is only used when directed broadcast or controller privacy is enabled (that is, the second parameter BLE_GAP_PRIV_CFG_PRIV_EN_BIT of the ble_gap_privacy_params_set function is set).
  • Code path: SDK_Folder\projects\ble\ble_basic_example\ble_app_gap_periodic_adv\Src\user\user_app.c

3.2 Set broadcast data

3.3 Set periodic broadcast data (optional)

static const uint8_t s_adv_data_set[] =
{<!-- -->
        0x03,
        BLE_GAP_AD_TYPE_COMPLETE_LIST_16_BIT_UUID,
        0x01, 0x00,
        0x0d,
        BLE_GAP_AD_TYPE_COMPLETE_NAME,
        'P', 'e', 'r', 'i', 'o', 'd', 'i', 'c', ' _', 'A', 'D', 'V',
};
static const uint8_t s_periodic_adv_data[] =
{<!-- -->
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06
};
error_code = ble_gap_adv_data_set(0, BLE_GAP_ADV_DATA_TYPE_DATA,s_adv_data_set, sizeof(s_adv_data_set));
APP_ERROR_CHECK(error_code);
error_code = ble_gap_adv_data_set(0, BLE_GAP_ADV_DATA_TYPE_PER_DATA,s_periodic_adv_data,sizeof(s_periodic_adv_data));
APP_ERROR_CHECK(error_code);

illustrate:

  • adv data and periodic_adv_data must be combined according to the format specified by Bluetooth Core Spec, that is (length, type, data). The length indicated by length is the total length of the type field and data field.
  • Code path: SDK_Folder\projects\ble\ble_basic_example\ble_app_gap_periodic_adv\Src\user\user_app.c

3.4 Turn on broadcast

When calling the ble_gap_adv_start function to start broadcasting, the user needs to pass in the parameter adv_idx to specify the broadcast instance index. Supports the establishment of up to 5 periodic broadcasts at the same time, so the value range of adv_idx is [0, 1, 2, 3, 4].

s_gap_adv_time_param.duration = 0;
s_gap_adv_time_param.max_adv_evt = 0;
error_code = ble_gap_adv_start(0, & amp;s_gap_adv_time_param);
APP_ERROR_CHECK(error_code);

illustrate:

  • Code path: SDK_Folder\projects\ble\ble_basic_example\ble_app_gap_periodic_adv\Src\user\user_app.c

3.5 Call the callback function app_gap_adv_start_cb after the broadcast is successfully started

In addition, it should be noted that the system can enable up to five broadcasts at the same time, including traditional broadcasts, extended broadcasts and periodic broadcasts.