BLE在连接之前,外围设备需要进行不间断广播,中心设备去扫描发现广播然后连接。
但是广播数据怎样配置呢?这就是得从BLE的GAP(Generic Access Profile)说起。Generic Access Service是每个BLE设备都有的服务,可以理解为强制服务。GAP规定了广播结构与连接过程。
GAP结构
安全模式则分为两种,模式1分4种情况:
第一种是无安全,不认证也不加密,俗称裸奔。第二、三种均进行加密,第三种做认证,第二种不认证。
第二种安全等级将使用Just Works配对方式,而第三种安全等级则根据IO Capability选择合适的配对方式。
第四种安全等级是BLE v4.2新增的选项。BLE配对时候传输Passkey有被窃听风险,而这种等级使用特殊算法避免传输Passkey被窃听。
模式2分两种
签名证书和无签名证书。
在官方的sdk中,GAP由gapm_set_dev_config_cmd结构体进行配置。
struct gapm_set_dev_config_cmd
{
/// GAPM requested operation:
/// - GAPM_SET_DEV_CONFIG: Set device configuration
uint8_t operation;
/// Device Role: Central, Peripheral, Observer, Broadcaster or All roles.
uint8_t role;
/// -------------- Privacy Config -----------------------
/// Duration before regenerate device address when privacy is enabled. - in seconds
uint16_t renew_dur;
/// Provided own static private random address (addr_type = GAPM_CFG_ADDR_PRIVATE)
bd_addr_t addr;
/// Device IRK used for resolvable random BD address generation (LSB first)
struct gap_sec_key irk;
/// Device Address Type
/// - GAPM_CFG_ADDR_PUBLIC: Device Address is a Public Static address
/// - GAPM_CFG_ADDR_PRIVATE: Device Address is a Private Static address
/// - GAPM_CFG_ADDR_HOST_PRIVACY: Device Address generated using Host Privacy feature
/// - GAPM_CFG_ADDR_CTNL_PRIVACY: Device Address generated using Controller Privacy feature
uint8_t addr_type;
/// -------------- Security Config -----------------------
/// Pairing mode authorized (see enum gapm_pairing_mode)
uint8_t pairing_mode;
/// -------------- ATT Database Config -----------------------
/// GAP service start handle
uint16_t gap_start_hdl;
/// GATT service start handle
uint16_t gatt_start_hdl;
//CEVA-284
/// Attribute database and gap extended configuration ([url=home.php?mod=space&uid=418085]@see[/url] enum gapm_att_and_ext_cfg_flag)
//uint16_t att_cfg;
uint16_t att_and_ext_cfg;
/// -------------- LE Data Length Extension -----------------------
///Suggested value for the Controller's maximum transmitted number of payload octets to be used
uint16_t sugg_max_tx_octets;
///Suggested value for the Controller's maximum packet transmission time to be used
uint16_t sugg_max_tx_time;
/// --------------- L2CAP Configuration ---------------------------
/// Maximal MTU acceptable for device
uint16_t max_mtu;
/// Maximal MPS Packet size acceptable for device
uint16_t max_mps;
/// Maximum number of LE Credit based connection that can be established
uint8_t max_nb_lecb;
/// --------------- LE Audio Mode Supported -----------------------
///
/// LE Audio Mode Configuration (@see gapm_audio_cfg_flag)
uint16_t audio_cfg;
/// ------------------ LE PHY Management -------------------------
/// Preferred LE PHY rate for data transmission (@see enum gap_rate)
uint8_t tx_pref_rates;
/// Preferred LE PHY rate for data reception (@see enum gap_rate)
uint8_t rx_pref_rates;
};
从上面的结构体看,在广播连接之前还需要定义广播设备地址,设备名字,甚至图标等等。
设备地址有Public Device Address,和Random Device Address,其中Public需要收费。