lehuijie 发表于 2021-7-17 18:18

BLE使用(二)连接之前

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">BLE在连接之前,外围设备需要进行不间断广播,<span style="background:white"><span style="color:black">中心设备去扫描发现广播然后连接。</span></span></span></span></p>

<p style="text-align:justify"> &nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">但是广播数据怎样配置呢?这就是得从BLE的GAP<strong><span style="background:white"><span style="font-family:等线"><span style="color:#111111">(</span></span></span></strong><strong><span lang="EN-US" style="background:white"><span style="font-family:&quot;Arial&quot;,sans-serif"><span style="color:#111111">Generic Access Profile</span></span></span></strong><strong><span style="background:white"><span style="font-family:等线"><span style="color:#111111">)说起。</span></span></span></strong></span></span>Generic Access Service是每个BLE设备都有的服务,可以理解为强制服务。<span style="font-size:10.5pt"><span style="font-family:等线">GAP规定了广播结构与连接过程。</span></span></p>

<p style="text-align:justify">&nbsp;</p>

<p style="text-align:justify"> &nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GAP结构</span></span></p>

<p style="text-align:justify">安全模式则分为两种,模式1分4种情况:</p>

<p>第一种是无安全,不认证也不加密,俗称裸奔。第二、三种均进行加密,第三种做认证,第二种不认证。</p>

<p>第二种安全等级将使用Just Works配对方式,而第三种安全等级则根据IO Capability选择合适的配对方式。</p>

<p>第四种安全等级是BLE v4.2新增的选项。BLE配对时候传输Passkey有被窃听风险,而这种等级使用特殊算法避免传输Passkey被窃听。</p>

<p>模式2分两种</p>

<p>签名证书和无签名证书。</p>

<p>&nbsp;</p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:等线">在官方的sdk中,GAP由gapm_set_dev_config_cmd结构体进行配置。</span></span></p>

<pre>
<code>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 (@see enum gapm_att_and_ext_cfg_flag)
        //uint16_tatt_cfg;
    uint16_tatt_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_tmax_nb_lecb;

    /// --------------- LE Audio Mode Supported -----------------------
    ///
    /// LE Audio Mode Configuration (@see gapm_audio_cfg_flag)
    uint16_taudio_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;
};
</code></pre>

<p>从上面的结构体看,在广播连接之前还需要定义广播设备地址,设备名字,甚至图标等等。</p>

<p>设备地址有<strong>Public Device Address,和Random Device Address,其中Public需要收费。</strong></p>

<p>&nbsp;</p>
页: [1]
查看完整版本: BLE使用(二)连接之前