1.目的
设置广播的类型
2.分析
在项目中经常使用到各种不同的广播类型,比如只做广播而不能被连接,且不定性广播的时候可以设置为BLE_GAP_ADV_TYPE_ADV_NONCONN_IND,
假如只指定MAC地址的设备才能连上设备,这个时候就可以用定向广播
3.平台:
协议栈版本:SDK8.0.0
编译软件:keil 5.14
硬件平台:nrf51822最小系统
例子:SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4
4.步骤
广播类型有如下选择。
BLE_GAP_ADV_TYPE_ADV_IND :可以被任何主机连接,不定向广播
BLE_GAP_ADV_TYPE_ADV_DIRECT_IND:可以连接,定向广播,(只能被指定的设备连接)
BLE_GAP_ADV_TYPE_ADV_SCAN_IND:可以被扫面,不定向
BLE_GAP_ADV_TYPE_ADV_NONCONN_IND:不可以连接,不定向
/**@defgroup BLE_GAP_ADV_TYPES GAP Advertising types * @{ */ #define BLE_GAP_ADV_TYPE_ADV_IND 0x00 /**< Connectable undirected. */ #define BLE_GAP_ADV_TYPE_ADV_DIRECT_IND 0x01 /**< Connectable directed. */ #define BLE_GAP_ADV_TYPE_ADV_SCAN_IND 0x02 /**< Scannable undirected. */ #define BLE_GAP_ADV_TYPE_ADV_NONCONN_IND 0x03 /**< Non connectable undirected. */ /**@} */
uint32_t ble_advertising_start(ble_adv_mode_t advertising_mode) { uint32_t err_code; ble_gap_adv_params_t adv_params; m_adv_mode_current = advertising_mode; uint32_t count = 0; // Verify if there are any pending flash operations. If so, delay starting advertising until // the flash operations are complete. err_code = pstorage_access_status_get(&count); if (err_code == NRF_ERROR_INVALID_STATE) { // Pstorage is not initialized, i.e. not in use. count = 0; } else if (err_code != NRF_SUCCESS) { return err_code; } if (count != 0) { m_advertising_start_pending = true; return NRF_SUCCESS; } // Fetch the peer address. ble_advertising_peer_address_clear(); if ( ((m_adv_modes_config.ble_adv_directed_enabled) && m_adv_mode_current == BLE_ADV_MODE_DIRECTED) ||((m_adv_modes_config.ble_adv_directed_slow_enabled) && m_adv_mode_current == BLE_ADV_MODE_DIRECTED_SLOW)) { if (m_evt_handler != NULL) { m_peer_addr_reply_expected = true; m_evt_handler(BLE_ADV_EVT_PEER_ADDR_REQUEST); } else { m_peer_addr_reply_expected = false; } } // If a mode is disabled, continue to the next mode. I.e fast instead of direct, slow instead of fast, idle instead of slow. if ( (m_adv_mode_current == BLE_ADV_MODE_DIRECTED) &&(!m_adv_modes_config.ble_adv_directed_enabled || !peer_address_exists(m_peer_address.addr))) { m_adv_mode_current = BLE_ADV_MODE_DIRECTED_SLOW; } if ( (m_adv_mode_current == BLE_ADV_MODE_DIRECTED_SLOW) &&(!m_adv_modes_config.ble_adv_directed_slow_enabled || !peer_address_exists(m_peer_address.addr))) { m_adv_mode_current = BLE_ADV_MODE_FAST; } if (!m_adv_modes_config.ble_adv_fast_enabled && m_adv_mode_current == BLE_ADV_MODE_FAST) { m_adv_mode_current = BLE_ADV_MODE_SLOW; } if (!m_adv_modes_config.ble_adv_slow_enabled && m_adv_mode_current == BLE_ADV_MODE_SLOW) { m_adv_mode_current = BLE_ADV_MODE_IDLE; m_adv_evt = BLE_ADV_EVT_IDLE; } // Fetch the whitelist. if ( (m_evt_handler != NULL) && (m_adv_mode_current == BLE_ADV_MODE_FAST || m_adv_mode_current == BLE_ADV_MODE_SLOW) && (m_adv_modes_config.ble_adv_whitelist_enabled) && (!m_whitelist_temporarily_disabled)) { m_whitelist_reply_expected = true; m_evt_handler(BLE_ADV_EVT_WHITELIST_REQUEST); } else { m_whitelist_reply_expected = false; } // Initialize advertising parameters with default values. memset(&adv_params, 0, sizeof(adv_params)); 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.p_whitelist = NULL; // Set advertising parameters and events according to selected advertising mode. switch (m_adv_mode_current) { case BLE_ADV_MODE_DIRECTED: LOG("[ADV]: Starting direct advertisement.\r\n"); adv_params.p_peer_addr = &m_peer_address; // Directed advertising. adv_params.type = BLE_GAP_ADV_TYPE_ADV_DIRECT_IND; adv_params.timeout = 0; adv_params.interval = 0; m_adv_evt = BLE_ADV_EVT_DIRECTED; break; case BLE_ADV_MODE_DIRECTED_SLOW: LOG("[ADV]: Starting direct advertisement.\r\n"); adv_params.p_peer_addr = &m_peer_address; // Directed advertising. adv_params.type = BLE_GAP_ADV_TYPE_ADV_DIRECT_IND; adv_params.timeout = m_adv_modes_config.ble_adv_directed_slow_timeout; adv_params.interval = m_adv_modes_config.ble_adv_directed_slow_interval; m_adv_evt = BLE_ADV_EVT_DIRECTED_SLOW; break; case BLE_ADV_MODE_FAST: adv_params.timeout = m_adv_modes_config.ble_adv_fast_timeout; adv_params.interval = m_adv_modes_config.ble_adv_fast_interval; if ( whitelist_has_entries(&m_whitelist) && m_adv_modes_config.ble_adv_whitelist_enabled && !m_whitelist_temporarily_disabled) { adv_params.fp = BLE_GAP_ADV_FP_FILTER_CONNREQ; adv_params.p_whitelist = &m_whitelist; m_advdata.flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED; err_code = ble_advdata_set(&m_advdata, NULL); if(err_code != NRF_SUCCESS) { return err_code; } m_adv_evt = BLE_ADV_EVT_FAST_WHITELIST; LOG("[ADV]: Starting fast advertisement with whitelist.\r\n"); } else { m_adv_evt = BLE_ADV_EVT_FAST; LOG("[ADV]: Starting fast advertisement.\r\n"); } break; case BLE_ADV_MODE_SLOW: adv_params.interval = m_adv_modes_config.ble_adv_slow_interval; adv_params.timeout = m_adv_modes_config.ble_adv_slow_timeout; if ( whitelist_has_entries(&m_whitelist) && m_adv_modes_config.ble_adv_whitelist_enabled && !m_whitelist_temporarily_disabled) { adv_params.fp = BLE_GAP_ADV_FP_FILTER_CONNREQ; adv_params.p_whitelist = &m_whitelist; m_advdata.flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED; err_code = ble_advdata_set(&m_advdata, NULL); if(err_code != NRF_SUCCESS) { return err_code; } m_adv_evt = BLE_ADV_EVT_SLOW_WHITELIST; LOG("[ADV]: Starting slow advertisement with whitelist.\r\n"); } else { m_adv_evt = BLE_ADV_EVT_SLOW; LOG("[ADV]: Starting slow advertisement.\r\n"); } break; default: break; } if (m_adv_mode_current != BLE_ADV_MODE_IDLE) { err_code = sd_ble_gap_adv_start(&adv_params); if(err_code != NRF_SUCCESS) { return err_code; } } if (m_evt_handler != NULL) { m_evt_handler(m_adv_evt); } return NRF_SUCCESS; }
上面代带可以看到ble_advertising_start();可以设置广播的模式。
BLE_ADV_MODE_DIRECTED和BLE_ADV_MODE_DIRECTED_SLOW的时候,需要设置
adv_params.p_peer_addr = &m_peer_address; // Directed advertising.这个参数
参数的定义如下:
/**@brief Bluetooth Low Energy address. */
typedef struct
{
uint8_t addr_type; /**< See @ref BLE_GAP_ADDR_TYPES. */
uint8_t addr[BLE_GAP_ADDR_LEN]; /**< 48-bit address, LSB format. */
} ble_gap_addr_t;
static ble_gap_addr_t m_peer_address; /**< Address of the most recently connected peer, used for direct advertising. */
可以看到这个是设置地址的类型和6位的地址码。
这里设置能连上自己的设备的蓝牙地址,就可以定向广播了。蓝牙值选择设置mac地址的设备才能连上自己。