关键函数:sd_ble_opt_set
/**@brief GAP Option IDs.
* 唯一标识GAP选项的id。
*/
enum BLE_GAP_OPTS
{
BLE_GAP_OPT_CH_MAP = BLE_GAP_OPT_BASE, /**< Channel Map. @ref ble_gap_opt_ch_map_t */
BLE_GAP_OPT_LOCAL_CONN_LATENCY, /**< Local connection latency. @ref ble_gap_opt_local_conn_latency_t */
BLE_GAP_OPT_PASSKEY, /**< 设置配对密码. @ref ble_gap_opt_passkey_t */
BLE_GAP_OPT_PRIVACY, /**< Custom privacy. @ref ble_gap_opt_privacy_t */
BLE_GAP_OPT_SCAN_REQ_REPORT, /**< Scan request report. @ref ble_gap_opt_scan_req_report_t */
BLE_GAP_OPT_COMPAT_MODE /**< Compatibility mode. @ref ble_gap_opt_compat_mode_t */
};
/**@brief Set a BLE option.
*
* @details 这个调用允许应用程序设置一个选项的值.
*
* @mscs
* @mmsc{@ref BLE_GAP_PERIPH_BONDING_STATIC_PK_MSC}
* @mmsc{@ref BLE_COMMON_CONF_BW}
* @endmscs
*
* @param[in] opt_id Option ID, see @ref BLE_COMMON_OPTS and @ref BLE_GAP_OPTS.
* @param[in] p_opt Pointer to a ble_opt_t structure containing the option value.
*
* @retval ::NRF_SUCCESS Option set successfully.
* @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
* @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
* @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, check parameter limits and constraints.
* @retval ::NRF_ERROR_INVALID_STATE [此时无法设置参数]
* @retval ::NRF_ERROR_BUSY [BLE堆栈繁忙或前一个过程未完成]
*/
SVCALL(SD_BLE_OPT_SET, uint32_t, sd_ble_opt_set(uint32_t opt_id, ble_opt_t const *p_opt));
例程基础:
**nRF5_SDK_12.3.0_d7731ad\examples\ble_peripheral\ble_app_hids_mouse\pca10028\s130\arm5_no_packs
所需内容:
-
所需文件(对等管理器,安全协议)
-
配对参数如下:
#define SEC_PARAM_BOND 1 /**< Perform bonding. */
#define SEC_PARAM_MITM 1 /**< Man In The Middle protection not required. */
#define SEC_PARAM_LESC 0 /**< LE Secure Connections not enabled. */
#define SEC_PARAM_KEYPRESS 0 /**< Keypress notifications not enabled. */
#define SEC_PARAM_IO_CAPABILITIES BLE_GAP_AUTH_KEY_TYPE_PASSKEY /**< 密码配对 */
#define SEC_PARAM_OOB 0 /**< Out Of Band data not available. */
#define SEC_PARAM_MIN_KEY_SIZE 7 /**< Minimum encryption key size. */
#define SEC_PARAM_MAX_KEY_SIZE 16 /**< Maximum encryption key size. */
- 设置配对密码(在广播前设置advertising_start)
//下面是添加设置配对密码
uint8_t passcode[]="123456";
ble_opt_t static_option;
static_option.gap_opt.passkey.p_passkey = passcode;
err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &static_option);
APP_ERROR_CHECK(err_code);
- 动态密码则每次随机生成(RNG自带),调用[sd_ble_opt_set]更新