1@page ble_host ble_host 2 3[更正文档](https://gitee.com/alios-things/ble_host/edit/master/README.md)      [贡献说明](https://help.aliyun.com/document_detail/302301.html) 4 5# 概述 6AliOS Things 3.3提供支持符合蓝牙4.0/4.2/5.0核心协议规范的BLE Host软件协议栈组件,方便用户使用蓝牙BLE功能。<br />ble_host组件功能框图如下图红色部分: 7 8<div align=left display=flex> 9 <img src="https://img.alicdn.com/imgextra/i1/O1CN018Gd9qR2ALhGFNkTT9_!!6000000008187-2-tps-830-730.png#align=left&display=inline&height=461&margin=%5Bobject%20Object%5D&name=image.png&originHeight=922&originWidth=1932&size=259937&status=done&style=none&width=966#align=left&display=inline&height=537&margin=%5Bobject%20Object%5D&originHeight=730&originWidth=830&status=done&style=none&width=611#align=left&display=inline&height=730&margin=%5Bobject%20Object%5D&originHeight=730&originWidth=830&status=done&style=none&width=830" style="max-width:800px;" /> 10</div> 11 12## 功能支持 13ble_host组件主要支持如下功能: 14 15- Generic Access Profile(GAP)角色支持 16 - Peripheral&Central 17 - Observer&Broadcaster 18- Generic Attribute Profile(GATT)连接支持 19 - GATT client 20 - GATT server 21- Security Manager(SM)支持 22 - Legacy Pairing 23 - 多安全等级设定Security Level 1, 2, 3, 4 24 - 安全连接Security Connection 25 - LE Privacy(RPA地址生成) 26- HCI接口支持 27 - 标准HCI接口,支持host-only,host通过HCI硬件接口(以UART为主)和controller对接 28 - 虚拟HCI接口,支持host+controller,适合SoC的硬件平台 29 30 31 32## 版权说明 33> Apache license v2.0 34 35 36 37## 目录结构 38```tree 39|-- ble_profiles #BLE服务 40|-- bt_crypto #BLE安全 41|-- bt_defconfig #BLE配置项 42|-- bt_host #BLE Host核心代码 43|-- bt_preconfig #BLE预配置项 44|-- bt_shell #BLE控制台命令 45|-- include #头文件 46|-- package.yaml #makefile 47|-- README.md #README文档 48|-- SConscript 49|-- script 50``` 51 52 53## 依赖组件 54 55- osal_aos 56 57 58 59# 常用配置 60 61 62## 配置列表 63配置在组件的package.yaml中定义,均已默认打开,用户可根据需求裁剪。 64 65| CONFIG_BT_CENTRAL | BLE Central角色功能开关 | 66| :--- | :--- | 67| CONFIG_BT_PERIPHERAL | BLE 外设角色功能开关 | 68| CONFIG_BT_SMP | BLE SMP功能开关 | 69| CONFIG_BT_GATT_CLIENT | BLE GATT Client功能开关 | 70| CONFIG_BT_OBSERVER | BLE 广播扫描功能开关 | 71 72 73 74# API说明 75## API列表 76| ble_stack_init | BLE协议栈初始化 | 77| :--- | :--- | 78| ble_stack_event_register | 注册BLE事件处理函数 | 79| ble_stack_adv_start | BLE 广播开始 | 80| ble_stack_adv_stop | BLE 广播停止 | 81| ble_stack_scan_start | BLE 扫描开始 | 82| ble_stack_scan_stop | BLE 扫描停止 | 83| ble_stack_gatt_mtu_get | GATT,获取当前连接MTU大小 | 84| ble_stack_gatt_registe_service | GATT server,服务注册 | 85| ble_stack_gatt_notificate | GATT server,属性notificate方式上报 | 86| ble_stack_gatt_indicate | GATT server,属性indicate方式上报 | 87| ble_stack_gatt_mtu_exchange | GATT client,协商MTU大小 | 88| ble_stack_gatt_discovery | GATT client,发现对端GATT服务 | 89| ble_stack_gatt_read | GATT client,读取对端GATT server属性 | 90| ble_stack_gatt_read_multiple | GATT client,读取对端GATT server多个属性 | 91| ble_stack_gatt_write | GATT client,写入对端GATT server属性 | 92| ble_stack_connect | 开始BLE连接 | 93| ble_stack_disconnect | 断开BLE连接 | 94| ble_stack_connect_param_update | BLE连接参数协商 | 95| ble_stack_security | BLE连接安全等级设置 | 96| ble_stack_iocapability_set | BLE SMP IO能力配置 | 97| ble_stack_smp_cancel | BLE SMP配对请求取消 | 98| ble_stack_smp_passkey_entry | BLE SMP passkey输入 | 99| ble_stack_smp_passkey_confirm | BLE SMP passkey确认 | 100| ble_stack_dev_unpair | PIN SMP配对解绑 | 101 102 103 104## API详情 105 106 107### ble_stack_init 108BLE模块初始化。<br />**函数原型** 109``` c 110int ble_stack_init(init_param_t *param) 111``` 112**输入参数** 113 114| args | description | 115| :--- | :--- | 116| param.dev_name | 设备名,string类型,不超过28字符 | 117| param.dev_addr | 设备mac地址,仅设置为random类型时生效 | 118| param.conn_num_max | 最大连接数 | 119 120**返回参数**<br />`0:`成功, `其他值`:失败。<br /> 121 122### ble_stack_event_register 123注册BLE协议栈事件回调函数。<br />注意:入参不能是一个局部变量。<br />**函数原型** 124``` c 125int ble_stack_event_register(ble_event_cb_t *callback) 126``` 127**输入参数** 128 129| args | description | 130| :--- | :--- | 131| callback.callback | 回调函数 | 132| callback.next | 无需填写,由API函数处理 | 133 134**返回参数**<br />`0:`成功, `其他值`:失败。<br /> 135 136### ble_stack_adv_start 137打开BLE广播功能。<br />**函数原型** 138``` c 139int ble_stack_adv_start(adv_param_t *param) 140``` 141**输入参数** 142 143| args | description | 144| :--- | :--- | 145| param.type | 广播类型,定义如下<br /> 0x00 普通广播<br /> 0x01 直连广播<br /> 0x02 可连接可扫描广播<br /> 0x03 不可连接广播,<br /> 0x04 低占空直连广播 | 146| param.ad | ADV广播内容数组 | 147| param.sd | Scan Response广播内容数组 | 148| param.ad_num | ADV广播内容数组大小 | 149| param.sd_num | Scan Response广播内容数组大小 | 150| param.interval_min | 广播发送间隔最小值 | 151| param.interval_max | 广播发送间隔最大值 | 152| param.filter_policy | 广播过滤开关,定义如下<br /> 0x00 接受所有连接与扫描请求<br /> 0x01 接受所有连接请求,接受白名单设备的扫描请求<br /> 0x02 接受白名单的连接请求,接受所有扫描请求<br /> 0x03 仅接受白名单内的扫描与连接请求 | 153| param.channel_map | 广播信道设置,<br /> bit0: 37信道<br /> bit1: 38信道<br /> bit2: 39信道 | 154| param.direct_peer_addr | 直连广播设备地址,仅在广播类型为直连类型时才生效 | 155 156**返回参数**<br />`0:`成功, `其他值`:失败。<br /> 157 158### ble_stack_adv_stop 159关闭BLE广播功能。<br />**函数原型** 160``` c 161int ble_stack_adv_stop() 162``` 163**输入参数** 164 165| args | description | 166| :--- | :--- | 167| 无 | | 168 169**返回参数**<br />`0:`成功, `其他值`:失败。<br /> 170 171### ble_stack_scan_start 172打开BLE扫描功能。<br />**函数原型** 173``` c 174int ble_stack_scan_start(const scan_param_t *param) 175``` 176**输入参数** 177 178| args | description | 179| :--- | :--- | 180| param.type | 扫描类型,定义如下<br /> 0x00 被动扫描<br /> 0x01 主动扫描 | 181| param.filter_dup | 重复设备上报过滤开关,定义如下<br /> 0x00 关闭过滤<br /> 0x01 开启过滤 | 182| param.interval | 扫描间隔,单位为0.625毫秒 | 183| param.window | 扫描窗大小,单位为0.625毫秒 | 184| param.scan_filter | 扫描过滤类型,定义如下<br /> 0x00 所有广播均上报<br /> 0x01 仅白名单设备的广播上报 | 185 186**返回参数**<br />`0:`成功, `其他值`:失败。<br /> 187 188### ble_stack_scan_stop 189关闭BLE扫描功能。<br />异步事件,扫描结果在注册的事件回调函数中返回,返回事件EVENT_GAP_DEV_FIND<br />**函数原型** 190``` c 191int ble_stack_scan_stop() 192``` 193**输入参数** 194 195| args | description | 196| :--- | :--- | 197| 无 | | 198 199**返回值**<br />`0:`成功, `其他值`:失败。<br /> 200 201### ble_stack_gatt_mtu_get 202获取一个连接的当前MTU大小<br />**函数原型** 203``` c 204int ble_stack_gatt_mtu_get(int16_t conn_handle) 205``` 206**输入参数** 207 208| args | description | 209| :--- | :--- | 210| conn_handle | 连接句柄,在连接成功后分配 | 211 212**返回值**<br />23-65535: MTU大小<br /> 213 214### ble_stack_gatt_registe_service 215注册一个GATT服务<br />**函数原型** 216``` c 217int ble_stack_gatt_registe_service(gatt_service *s, gatt_attr_t attrs[], uint16_t attr_num) 218``` 219**输入参数** 220 221| args | description | 222| :--- | :--- | 223| s.bt_gatt_attr | 无需填写,由API赋值 | 224| s.attr_count | 无需填写,由API赋值 | 225| s.node | 无需填写,由API赋值 | 226| attrs | 属性列表 | 227| attr_num | 属性列表大小 | 228 229**返回值**<br />`0:`成功, `其他值`:失败。<br /> 230 231### ble_stack_gatt_notificate 232GATT服务某个属性值上报,此上报方式无需GATT Client回复<br />**函数原型** 233``` c 234int ble_stack_gatt_notificate(int16_t conn_handle, uint16_t char_handle, const uint8_t *data, uint16_t len) 235``` 236**输入参数** 237 238| args | description | 239| :--- | :--- | 240| conn_handle | 连接句柄,在连接成功后分配 | 241| char_handle | 属性句柄,在注册服务时分配 | 242| data | 上报数据指针 | 243| len | 上报数据长度 | 244 245**返回值**<br />`0:`成功, `其他值`:失败。<br /> 246 247### ble_stack_gatt_indicate 248GATT服务某个属性值上报,此上报方式GATT Client回复confirm。<br />**函数原型** 249``` c 250int ble_stack_gatt_indicate(int16_t conn_handle, int16_t char_handle, const uint8_t *data, uint16_t len) 251``` 252**输入参数** 253 254| args | description | 255| :--- | :--- | 256| conn_handle | 连接句柄,在连接成功后分配 | 257| char_handle | 属性句柄,在注册服务时分配 | 258| data | 上报数据指针 | 259| len | 上报数据长度 | 260 261**返回值**<br />`0:`成功, `其他值`:失败。<br /> 262 263### ble_stack_gatt_mtu_exchange 264GATT Client功能,确保配置项CONFIG_BT_GATT_CLIENT配置。<br />GATT协商MTU大小,协商大小由配置项CONFIG_BT_L2CAP_RX_MTU与CONFIG_BT_L2CAP_TX_MTU的最小值以及对端设备的回复决定。<br />异步事件,执行结果在注册的事件回调函数中返回,返回事件EVENT_GATT_MTU_EXCHANGE<br />**函数原型** 265``` c 266int ble_stack_gatt_mtu_exchange(int16_t conn_handle) 267``` 268 269<br />**输入参数** 270 271| args | description | 272| :--- | :--- | 273| conn_handle | 连接句柄,在连接成功后分配 | 274 275 276<br />**返回值**<br />`0:`成功, `其他值`:失败。<br /> 277 278### ble_stack_gatt_discovery 279GATT Client功能,确保配置项CONFIG_BT_GATT_CLIENT配置。<br />GATT服务或者属性项发现。<br />异步事件,执行结果在注册的事件回调函数中返回,返回事件根据入参不同而不同 280 281**函数原型** 282``` c 283int ble_stack_gatt_discovery(int16_t conn_handle, 284 gatt_discovery_type_en type, 285 uuid_t *uuid, 286 uint16_t start_handle, 287 uint16_t end_handle) 288``` 289**输入参数** 290 291| args | description | 292| :--- | :--- | 293| conn_handle | 连接句柄,在连接成功后分配 | 294| type | GATT发现类型,定义如下<br /> 0x00 发现主服务<br /> 0x01 发现包含服务<br /> 0x02 发现属性项<br /> 0x03 发现属性描述项 | 295| uuid | 需要发现的UUID值 | 296| start_handle | 发现起始句柄 | 297| end_handle | 发现结束句柄 | 298 299**返回值**<br />`0:`成功, `其他值`:失败。<br /> 300 301### ble_stack_gatt_read 302GATT Client功能,确保配置项CONFIG_BT_GATT_CLIENT配置。<br />GATT读取某个属性项的属性值<br />异步事件,执行结果在注册的事件回调函数中返回,返回事件EVENT_GATT_CHAR_READ_CB<br />**函数原型** 303``` c 304int ble_stack_gatt_read(int16_t conn_handle, uint16_t attr_handle, uint16_t offset) 305``` 306**输入参数** 307 308| args | description | 309| :--- | :--- | 310| conn_handle | 连接句柄,在连接成功后分配 | 311| attr_handle | 读取的属性项句柄 | 312| offset | 读取起始位置 | 313 314**返回值**<br />`0:`成功, `其他值`:失败。<br /> 315 316### ble_stack_gatt_read_multiple 317GATT Client功能,确保配置项CONFIG_BT_GATT_CLIENT配置。<br />GATT读取多个属性项的属性值<br />异步事件,执行结果在注册的事件回调函数中返回,返回事件EVENT_GATT_CHAR_READ_CB<br />**函数原型** 318``` c 319int ble_stack_gatt_read_multiple(int16_t conn_handle, uint16_t attr_count, uint16_t attr_handle[]) 320``` 321**输入参数** 322 323| args | description | 324| :--- | :--- | 325| conn_handle | 连接句柄,在连接成功后分配 | 326| attr_count | 读取的属性项列表大小 | 327| attr_handle | 读取的属性项句柄列表 | 328 329**返回值**<br />`0:`成功, `其他值`:失败。<br /> 330 331### ble_stack_gatt_write 332GATT Client功能,确保配置项CONFIG_BT_GATT_CLIENT配置。<br />GATT写入某个属性项的属性值<br />异步事件,执行结果在注册的事件回调函数中返回,返回事件EVENT_GATT_CHAR_WRITE_CB<br />**函数原型** 333``` c 334int ble_stack_gatt_write(int16_t conn_handle, uint16_t attr_handle, uint8_t *data, uint16_t len, uint16_t offset, gatt_write_en type) 335``` 336**输入参数** 337 338| args | description | 339| :--- | :--- | 340| conn_handle | 连接句柄,在连接成功后分配 | 341| attr_handle | 写入的属性项句柄 | 342| data | 写入的数据指针 | 343| len | 写入的数据长度 | 344| offset | 写入到属性项的起始位置 | 345| type | 写入类型,定义如下<br /> 0x00 带回复写入<br /> 0x01 不带回复写入<br /> 0x02 带签名写入 | 346 347**返回值**<br />`0:`成功, `其他值`:失败。<br /> 348 349### ble_stack_gatt_read_multiple 350GATT Client功能,确保配置项CONFIG_BT_GATT_CLIENT配置。<br />此函数用于GATT读取多个属性项的属性值。<br />异步事件,执行结果在注册的事件回调函数中返回,返回事件EVENT_GATT_CHAR_READ_CB<br />**函数原型** 351``` c 352int ble_stack_gatt_read_multiple(int16_t conn_handle, uint16_t attr_count, uint16_t attr_handle[]) 353``` 354**输入参数** 355 356| args | description | 357| :--- | :--- | 358| conn_handle | 连接句柄,在连接成功后分配 | 359| attr_count | 读取的属性项列表大小 | 360| attr_handle | 读取的属性项句柄列表 | 361 362**返回值**<br />`0:`成功, `其他值`:失败。<br /> 363 364### ble_stack_connect 365BLE连接功能,确保配置项CONFIG_BT_CONN配置。<br />此函数用于BLE连接某个设备。<br />异步事件,执行结果在注册的事件回调函数中返回,返回事件EVENT_GATT_CONN_CHANGE<br />**函数原型** 366``` c 367int ble_stack_connect(dev_addr_t *peer_addr, conn_param_t *param, uint8_t auto_connect) 368``` 369**输入参数** 370 371| args | description | 372| :--- | :--- | 373| peer_addr.type | 对端设备的地址类型 | 374| peer_addr.val | 对端设备的蓝牙地址 | 375| param.interval_min | 连接间隔最小值参数,单位是0.625毫秒 | 376| param.interval_max | 连接间隔最大值参数,单位是0.625毫秒 | 377| param.lantency | 连接延迟参数,单位是连接间隔,注意此时间长度不能超过timeout参数的一半。 | 378| param.timeout | 连接超时参数,单位10ms | 379| auto_connect | 是否自动连接定义如下<br /> 0x00 不自动连接<br /> 0x01 自动连接 | | 380 381**返回值**<br />`0:`成功, `其他值`:失败。<br /> 382 383### ble_stack_disconnect 384BLE连接功能,确保配置项CONFIG_BT_CONN配置。<br />此函数用于BLE断开某个设备的连接。<br />异步事件,执行结果在注册的事件回调函数中返回,返回事件EVENT_GATT_CONN_CHANGE<br />**函数原型** 385``` c 386int ble_stack_disconnect(int16_t conn_handle) 387``` 388**输入参数** 389 390| args | description | 391| :--- | :--- | 392| conn_handle | 连接句柄,在连接事件中返回 | 393 394**返回值**<br />`0:`成功, `其他值`:失败。<br /> 395 396### ble_stack_connect_param_update 397BLE连接功能,确保配置项CONFIG_BT_CONN配置。<br />此函数用于BLE修改某个连接的连接参数。<br />异步事件,执行结果在注册的事件回调函数中返回,返回事件EVENT_GAP_CONN_PARAM_UPDATE<br />**函数原型** 398``` c 399int ble_stack_connect_param_update(int16_t conn_handle, conn_param_t *param) 400``` 401**输入参数** 402 403| args | description | 404| :--- | :--- | 405| conn_handle | 连接句柄,在连接事件中返回 | 406| param.interval_min | 连接间隔最小值参数,单位是0.625毫秒 | 407| param.interval_max | 连接间隔最大值参数,单位是0.625毫秒 | 408| param.lantency | 连接延迟参数,单位是连接间隔,注意此时间长度不能超过timeout参数的一半。 | 409| param.timeout | 连接超时参数,单位10ms | 410 411**返回值**<br />`0:`成功, `其他值`:失败。<br /> 412 413### ble_stack_security 414BLE连接功能,确保配置项CONFIG_BT_CONN配置。<br />此函数用于BLE修改某个连接的安全参数。<br />异步事件,执行结果在注册的事件回调函数中返回,返回事件EVENT_GAP_CONN_SECURITY_CHANGE<br />**函数原型** 415``` c 416int ble_stack_security(int16_t conn_handle, security_en level) 417``` 418**输入参数** 419 420| args | description | 421| :--- | :--- | 422| conn_handle | 连接句柄,在连接事件中返回 | 423| level | 安全等级,定义如下<br /> 0x00 无安全,无加密无鉴权<br /> 0x01 低安全,等同于0x00<br /> 0x02 中安全,有加密无鉴权<br /> 0x03 高安全,有加密有鉴权 | 424 425**返回值**<br />`0:`成功, `其他值`:失败。<br /> 426 427### ble_stack_iocapability_set 428BLE SMP配对功能,确保配置项CONFIG_BT_SMP配置。<br />此函数用于设置BLE设备设备的IO能力,这个设置会在BLE SMP配对被使用。<br />**函数原型** 429``` c 430int ble_stack_iocapability_set(uint8_t io_cap) 431``` 432**输入参数** 433 434| args | description | 435| :--- | :--- | 436| io_cap | IO能力,定义如下<br /> bit0-bit2:<br /> 0x01 无输入<br /> 0x02 输入正确/错误<br /> 0x03 键盘输入<br /> bit3-bit4:<br /> 0x00 无输出<br /> 0x01 显示器输出 | 437 438**返回值**<br />`0:`成功, `其他值`:失败。<br /> 439 440### ble_stack_smp_cancel 441BLE SMP配对功能,确保配置项CONFIG_BT_SMP配置。<br />此函数用于取消当前BLE的SMP配对请求。<br />**函数原型** 442``` c 443int ble_stack_smp_cancel(int16_t conn_handle) 444``` 445**输入参数** 446 447| args | description | 448| :--- | :--- | 449| conn_handle | 连接句柄,在连接事件中返回 | 450 451**返回值**<br />`0:`成功, `其他值`:失败。<br /> 452 453### ble_stack_smp_passkey_entry 454BLE SMP配对功能,确保配置项CONFIG_BT_SMP配置。<br />此函数用于SMP Passkey模式下输入当前passkey,当接收到EVENT_SMP_PASSKEY_ENTER后调用。<br />**函数原型** 455``` c 456int ble_stack_smp_passkey_entry(int16_t conn_handle, uint32_t passkey) 457``` 458**输入参数** 459 460| args | description | 461| :--- | :--- | 462| conn_handle | 连接句柄,在连接事件中返回 | 463| passskey | 0-999999之间 | 464 465**返回值**<br />`0:`成功, `其他值`:失败。<br /> 466 467### ble_stack_smp_passkey_confirm 468BLE SMP配对功能,确保配置项CONFIG_BT_SMP配置。<br />此函数用于SMP Passkey模式下确认当前的passkey是否正确,当接收到EVENT_SMP_PASSKEY_CONFIRM后调用。<br />**函数原型** 469``` c 470int ble_stack_smp_passkey_confirm(int16_t conn_handle) 471``` 472**输入参数** 473 474| args | description | 475| :--- | :--- | 476| conn_handle | 连接句柄,在连接事件中返回 | 477 478**返回值**<br />`0:`成功, `其他值`:失败。<br /> 479 480### ble_stack_dev_unpair 481BLE SMP配对功能,确保配置项CONFIG_BT_SMP配置。<br />此函数用于解除某台已SMP配对的设备的配对,如果连接存在则断开连接。<br />**函数原型** 482``` c 483int ble_stack_dev_unpair(dev_addr_t *peer_addr) 484``` 485**输入参数** 486 487| args | description | 488| :--- | :--- | 489| peer_addr.type | 地址类型 | 490| peer_addr.val | 设备地址 | 491 492**返回值**<br />`0:`成功, `其他值`:失败。<br /> 493 494# 使用示例 495本用例测试过程中使用到的工具有: 496 497- 串口工具 498- NRF Connect(手机) 499 500组件使用示例相关的代码下载、编译和固件烧录均依赖AliOS Things配套的开发工具,所以首先需要参考[《AliOS Things集成开发环境使用说明之搭建开发环境》](https://help.aliyun.com/document_detail/302378.html),下载安装。 501待开发环境搭建完成后,可以按照以下步骤进行示例的测试。 502 503## 步骤1 创建或打开工程 504 505**打开已有工程** 506 507如果用于测试的案例工程已存在,可参考[《AliOS Things集成开发环境使用说明之打开工程》](https://help.aliyun.com/document_detail/302381.html)打开已有工程。 508 509**创建新的工程** 510 511组件的示例代码可以通过编译链接到AliOS Things的任意案例(solution)来运行,这里选择helloworld_demo案例。helloworld_demo案例相关的源代码下载可参考[《AliOS Things集成开发环境使用说明之创建工程》](https://help.aliyun.com/document_detail/302379.html)。 512 513## 步骤2 添加组件 514 515案例下载完成后,需要在helloworld_demo组件的package.yaml中添加对组件的依赖: 516 517```yaml 518depends: 519 - ble_host: master 520``` 521 522## 步骤3 下载组件 523 524在已安装了 的开发环境工具栏中,选择Terminal -> New Terminal启动终端,并且默认工作路径为当前工程的workspace,此时在终端命令行中输入: 525 526```shell 527 528aos install ble_host 529 530``` 531 532上述命令执行成功后,组件源码则被下载到了./components/ble_host路径中。 533 534## 步骤4 添加示例 535 536修改solutions/helloworld_demo/helloworld_demo.c,如下: 537 538**头文件修改** 539```c 540#include <aos/ble.h> 541#include <atomic.h> 542#include <bluetooth/bluetooth.h> 543#include <bluetooth/gatt.h> 544#include <bluetooth/uuid.h> 545``` 546 547**代码修改** 548 549```c 550#define EXAMPLE_BLE_DEV_NAME "HaaS BLE" 551#define DEVICE_ADDR {0xE8,0x3B,0xE3,0x88,0xB4,0xC8} 552 553int application_start(int argc, char *argv[]) 554{ 555 int count = 0; 556 int ret; 557 dev_addr_t addr = {DEV_ADDR_LE_RANDOM, DEVICE_ADDR}; 558 init_param_t init = { 559 .dev_name = EXAMPLE_BLE_DEV_NAME, 560 .dev_addr = &addr, //&addr, 561 .conn_num_max = 1, 562 }; 563 564 printf("nano entry here!\r\n"); 565 566#if 1 567 /* bt stack init */ 568 ret = ble_stack_init(&init); 569 if (ret) { 570 printf("error: ble_stack_init!, ret = %x\r\n", ret); 571 return -1; 572 } 573#endif 574 575 while(1) { 576 printf("hello world! count %d \r\n", count++); 577 aos_msleep(1000); 578 }; 579} 580``` 581 582 583## 步骤5 编译固件 584 585在示例代码已经添加至组件的配置文件,并且helloworld_demo已添加了对该组件的依赖后,就可以编译helloworld_demo案例来生成固件了,具体编译方法可参考[《AliOS Things集成开发环境使用说明之编译固件》](https://help.aliyun.com/document_detail/302384.html)。 586 587## 步骤6 烧录固件 588 589helloworld_demo案例的固件生成后,可参考[《AliOS Things集成开发环境使用说明之烧录固件》](https://help.aliyun.com/document_detail/302383.html)来烧录固件。 590 591## 步骤7 打开串口 592 593固件烧录完成后,可以通过串口查看示例的运行结果,打开串口的具体方法可参考[《AliOS Things集成开发环境使用说明之查看日志》](https://help.aliyun.com/document_detail/302382.html)。 594 595当串口终端打开成功后,可在串口中输入help来查看已添加的测试命令。 596 597## 步骤8 测试示例 598 599打开串口工具,在串口工具中输入ble,可看到ble的cli命令集。<br /> 600 601<div align=left display=flex> 602 <img src="https://img.alicdn.com/imgextra/i1/O1CN01Vm24En1Hnoi4vJ9lC_!!6000000000803-2-tps-1932-922.png#align=left&display=inline&height=461&margin=%5Bobject%20Object%5D&name=image.png&originHeight=922&originWidth=1932&size=259937&status=done&style=none&width=966#align=left&display=inline&height=293&margin=%5Bobject%20Object%5D&originHeight=922&originWidth=1932&status=done&style=none&width=614#align=left&display=inline&height=922&margin=%5Bobject%20Object%5D&originHeight=922&originWidth=1932&status=done&style=none&width=1932" style="max-width:800px;" /> 603</div> 604 605**BLE广播与连接测试** 606 607在串口工具中输入ble adv start 020106,可以在串口日志中看到如下打印 608```sh 609adv_type:0;adv_interval_min:160 (*0.625)ms;adv_interval_max:240 (*0.625)ms 610Advertising started 611``` 612 613<br />此时在手机端打开NRF Connect工具并搜索广播包,可以看到设备。 614 615<div align=left display=flex> 616 <img src="https://img.alicdn.com/imgextra/i2/O1CN01CZZkJl1ThCNOYmP1f_!!6000000002413-2-tps-700-1268.png#align=left&display=inline&height=520&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1268&originWidth=700&size=482965&status=done&style=none&width=287#align=left&display=inline&height=828&margin=%5Bobject%20Object%5D&originHeight=1268&originWidth=700&status=done&style=none&width=457#align=left&display=inline&height=949&margin=%5Bobject%20Object%5D&originHeight=1268&originWidth=700&status=done&style=none&width=524" style="max-width:800px;" /> 617</div> 618 619<br />点击连接,可以看到Generic Access(GAP)和Generic Attribute(GATT)2个服务,点击GAP服务的Device Name的读取按键(下图红色圈中的箭头),可以读取到数值“HaaS BLE”<br /> 620 621<div align=left display=flex> 622 <img src="https://img.alicdn.com/imgextra/i3/O1CN01jK0smX1FJvdIip6ZO_!!6000000000467-2-tps-696-726.png#align=left&display=inline&height=363&margin=%5Bobject%20Object%5D&name=image.png&originHeight=726&originWidth=696&size=250340&status=done&style=none&width=348#align=left&display=inline&height=469&margin=%5Bobject%20Object%5D&originHeight=726&originWidth=696&status=done&style=none&width=450#align=left&display=inline&height=548&margin=%5Bobject%20Object%5D&originHeight=726&originWidth=696&status=done&style=none&width=525" style="max-width:800px;" /> 623</div> 624 625# 常见问题 626 627Q:此协议栈是否支持蓝牙音乐播放 628A:不支持经典蓝牙功能,仅支持BLE功能 629 630Q:常见的GATT Service是否支持 631A:支持,包括BAS,DIS,HIDS等等,详情请参考ble_host/ble_profiles文件下的实现。 632 633