NRF51822之动态广播使用

本教程基于nRF51_SDK_10.0.0_dc26b5e\examples\ble_peripheral\ble_app_uart工程

本教程主要是演示

现在演示通过nus来修改ADV中maufacturer data,在disconnect后我们可以看到广播中maufacturer data更新为我们在nus中配置的

NRF51822之动态广播使用

在工程增加新代码

#define APP_COMPANY_IDENTIFIER       0x0059                                     /**< Company identifier for Nordic Semiconductor ASA. as per www.bluetooth.org. */

#define DEFAULT_ADVDATA                "libra"
#define ADVDATA_BUFFER_SIZE             24

// advertisement data string
static uint8_t advert_str[ADVDATA_BUFFER_SIZE] = DEFAULT_ADVDATA;
;

static volatile bool update_request = false;

// configure advertisements
void adverts_init(void)
{
    uint32_t      err_code;
    ble_advdata_t advdata;
    ble_advdata_t scanrsp;
    ble_advdata_manuf_data_t manuf_data;

    memset(&advdata, , sizeof(advdata));
    memset(&scanrsp, , sizeof(scanrsp));

    manuf_data.company_identifier = APP_COMPANY_IDENTIFIER;
    manuf_data.data.p_data = advert_str;
    manuf_data.data.size = adv_str_len;

    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = false;
    advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
    advdata.p_manuf_specific_data   = &manuf_data;

    err_code = ble_advdata_set(&advdata,NULL);
    APP_ERROR_CHECK(err_code);
}

// start advertising
void adverts_start(void)
{
    uint32_t             err_code;
    ble_gap_adv_params_t adv_params;
    memset(&adv_params, , sizeof(adv_params));    

  //设置广播信道是否开启
    adv_params.channel_mask.ch_37_off = ;
    adv_params.channel_mask.ch_38_off = ;
    adv_params.channel_mask.ch_39_off = ;

    adv_params.type        = BLE_GAP_ADV_TYPE_ADV_IND;
    adv_params.p_peer_addr = NULL;
    adv_params.fp          = BLE_GAP_ADV_FP_ANY;
    adv_params.interval    = APP_ADV_INTERVAL;
    adv_params.timeout     = APP_ADV_TIMEOUT_IN_SECONDS;
    adv_params.p_whitelist = NULL;

    err_code = sd_ble_gap_adv_start(&adv_params);
    APP_ERROR_CHECK(err_code);
}

// check connection state
bool is_connected(void)
{
    return (m_conn_handle != BLE_CONN_HANDLE_INVALID);
}

// update advertised data
void adverts_update(uint8_t * adv_str, uint16_t len)
{
    if (len > ADVDATA_BUFFER_SIZE) len = ADVDATA_BUFFER_SIZE;
    memcpy(advert_str, adv_str, len);
    adv_str_len = len;

    update_request = true;
}

void adverts_update_check(void)
{

    if((update_request)&&(!is_connected()))
    {
        update_request = false;

        sd_ble_gap_adv_stop();

        adverts_init();    

        adverts_start();

    }
}

修改工程原来的代码

static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
    ; i < length; i++)
    {
        while(app_uart_put(p_data[i]) != NRF_SUCCESS);

    }
    while(app_uart_put('\n') != NRF_SUCCESS);
    ////////////////////////////////////////////////////////////////////////
    adverts_update(p_data,length);
    ////////////////////////////////////////////////////////////////////////
}
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
...

        case BLE_GAP_EVT_DISCONNECTED:
            err_code = bsp_indication_set(BSP_INDICATE_IDLE);
            APP_ERROR_CHECK(err_code);
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            //////////////////////////////////////////////////////////////
            adverts_update_check();
            /////////////////////////////////////////////////////////////////
            break;
...
}
/**@brief Application main function.
 */
int main(void)
{
    uint32_t err_code;
    bool erase_bonds;
    uint8_t  start_string[] = START_STRING;

    // Initialize.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);
    uart_init();
    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    gap_params_init();
    services_init();
  //  advertising_init();
    adverts_init();

    conn_params_init();

    printf("%s",start_string);

   // err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
   // APP_ERROR_CHECK(err_code);
   adverts_start();

    // Enter main loop.
    for (;;)
    {
        power_manage();
    }
}
上一篇:动态规划——Frog Jump


下一篇:解决Office 2016客户端如何同SharePoint Server2016安装在一起