BT_bluedroid enable启动

1,364 阅读21分钟

基于Android P版本分析

BT 启动

我们知道,BT的启动,对应的上层应用的enable方法。

而在enable的过程中,首先会判断mBluetooth变量是否为空,而该变量的类型为AdapterServiceBinder,首次开启的时候为空,所以我们首先先分析一下AdapterService的启动,分析在AdapterService的启动过程中,执行了加载了哪些信息、执行了哪些逻辑;

bluedroid 加载 / 初始化

BT_init_bluedroid流程分析.png

我们主要关注AdapterService启动的时候,bluedroid做了哪些操作;

基本上可以分为5件事儿:

  • classInitNative:

    • 建立JNI和Java层之间的映射调用关系,使用JniCallbacks作为中间件;
    • hal_util_load_bt_library:加载so库;
  • AdapterService -- onCreate -- initNative

    • init:创建stack_mananger线程,用于管理BT 协议栈的运行;
    • set_os_callouts:设置bluedroid用于报警和唤醒锁的操作系统调出功能
    • get_profile_interface:创建用于RFCOMM通信的interface

其中stack_manager的init_stack函数也比较重要,其中涉及到了几个点:

  • 基本的信号量操作;

  • module相关函数(对其他模块的一些初始化检查操作);

  • btif_init_bluetooth

    • 主机和蓝牙芯片相关入口初始化

      • 检查并初始化INTEROP_MODULE模块;
      • 初始化、获取HCI接口hci_layer_get_interface,并返回hci变量;
      • hci->set_data_cb监听post_to_message_loop消息;
    • 新创建一个JNI工作队列,用于处理JNI的回调信息;

bluedroid Gatt 初始化

AdapterService onCreate方法执行完成之后,bluedroid也就加载和初始化成功。

而在enable逻辑中,会对mBluetooth变量进行多次判空,当AdapterService启动成功之后,mBluetooth便指向了AdapterService,当前不为空,则紧接着执行enable逻辑;

BT_gatt_start_bluedroid流程分析.png

而在真正enable之前,又初始化了GattService,现在低功耗蓝牙(BLE)连接都是建立在GATT协议之上,GATT是一个在蓝牙连接之上的发送和接收很短的数据段的通用规范,这个很短的数据段被称为属性(Attribute)。GattService用于定义和管理输入/输出数据交互。

因为在后续的enable过程中,会涉及到GattService的判断,只有当class == GattService.class的时候,才会执行到enableNative函数中;

bluedroid enable使能

上述的过程,基本上还是用于建立bluedroid和Java之间的回调机制;enable实际的流程是在AdapterService和GattService启动之后执行的。

enable_bluedroid流程图.png

BT_enable_bluedroid流程分析.png

在这个过程中,主要对应的方法就是enableNative;

在这个方法中,主要实现了如下几个:

  • 初始化BTE

  • 创建BTU_TASK及启动BTU_TASK;

    • btu_init_core:初始化核心栈控制块

      • btm_init

        • btm_inq_db_init
        • btm_acl_init
        • btm_sec_init
        • btm_sco_init
        • btm_dev_init
      • l2c_init

      • sdp_init

      • gatt_init

      • SMP_init

      • btm_ble_init

    • BTE_initStack:初始化任何可选堆栈组件

      • RFCOMM_init
      • BNEP_init
      • PAN_init
      • A2DP_init
      • AVRC_init
      • GAP_init
      • HID_Host_init
    • bta_sys_init:注册BTA系统消息handler、注册BTM通知监听

      • bta_sys_register
      • BTM_RegisterForDeviceStatusNotify
      • bta_ar_init
  • 初始化HCI、串口相关。启动HCI工作主线程:bt_hc_challback,芯片上电、RF参数初始化;

BT 协议数据包分析

我们在分析BT enable的过程中,在分析到bta_sys_init函数执行完成之后,会在btu_message_loop_run中向上层发送btif_init_ok,用于向JNI线程发送初始化完成的消息,其中该函数中,还执行了BTA_EnableBluetooth,这个过程中,其实是用于start_up Controller,而Controller对应的就是蓝牙芯片的软件层面,或者说是Host向Controller访问的接口;

而在这之前的过程中,都是Host模块的初始化和启动分析;

Controller Start Up

我们分析一下,在controller module的start_up过程中,发送了哪些HCI Command;

BT_BTA_EnableBluetooth_bluedroid流程分析.png

在这个过程中,最核心的逻辑就是在启动Controller_Module,在该module的start_up过程中,Host会向Controller module发送需要HCI Command,用于获取蓝牙芯片必要的一些信息;

Reset
Frame 1: 4 bytes on wire (32 bits), 4 bytes captured (32 bits)
Bluetooth
    [Source: host]
    [Destination: controller]
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Reset
    Command Opcode: Reset (0x0c03)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0000 0011 = Opcode Command Field: Reset (0x003)
    Parameter Total Length: 0
    [Response in frame: 2]
    [Command-Response Delta: 17.899ms]
  • Source:host,蓝牙协议栈
  • Destination:controller,蓝牙芯片
  • Direction:Sent,发送
  • HCI Packet Type:HCI Command,Host和Controller之间的通信一般常见的有4种:HCI Command、HCI Event、HCI ACL、HCI SCO;
  • Command Opcode:Reset
Frame 2: 7 bytes on wire (56 bits), 7 bytes captured (56 bits)
Bluetooth
    [Source: controller]
    [Destination: host]
Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 4
    Number of Allowed Command Packets: 1
    Command Opcode: Reset (0x0c03)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0000 0011 = Opcode Command Field: Reset (0x003)
    Status: Success (0x00)
    [Command in frame: 1]
    [Command-Response Delta: 17.899ms]
  • HCI Packet Type:HCI Event,HCI Command和HCI Event是对应的关系;
  • Event Code:Command Complete;
  • Status:Success;
  • Command Opcode:Reset,Event对应的Opcode;

一般情况下,Source为Host的情况下,对应的HCI Packet Type为HCI Command,而Source为Controller的情况下,对应的HCI Packet Type为HCI Event;

Read Local Version Information
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Read Local Version Information
    Command Opcode: Read Local Version Information (0x1001)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 0001 = Opcode Command Field: Read Local Version Information (0x001)
    Parameter Total Length: 0
    [Response in frame: 8]
    [Command-Response Delta: 12.938ms]

这个Command用于获取本地Controller的版本信息;

Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 12
    Number of Allowed Command Packets: 1
    Command Opcode: Read Local Version Information (0x1001)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 0001 = Opcode Command Field: Read Local Version Information (0x001)
    Status: Success (0x00)
    HCI Version: 5.1 (0x0a)
    HCI Revision: 0
    LMP Version: 5.1 (0x0a)
    Manufacturer Name: Qualcomm (0x001d)
    LMP Subversion: 4025
    [Command in frame: 7]
    [Command-Response Delta: 12.938ms]
  • Statue:0x00表示success,0x01-0xFF error code;
  • HCI Version:定义HCI层的版本信息,5.1;
  • HCI Revision:具体根据实现来定;
  • LMP Version:定义LMP或者PAL层的版本信息,5.1;
  • Manufacturer Name:Qualcomm,指定生产商;
  • LMP Subversion:具体根据实现来定,4025;
Read BD ADDR
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Read BD ADDR
    Command Opcode: Read BD ADDR (0x1009)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 1001 = Opcode Command Field: Read BD ADDR (0x009)
    Parameter Total Length: 0
    [Response in frame: 12]
    [Command-Response Delta: 1.227ms]

如果是BR/EDR Controller,这个command用来获取Bluetooth Controlleraddress,如果是LE Controller,这个command用来获取Public Device Address,如果Controller没有Public Device Address,将返回0x000000000000;如果是BR/EDR/LE Controller,Public Device Address和BD_ADDR是同一个值;

Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 10
    Number of Allowed Command Packets: 1
    Command Opcode: Read BD ADDR (0x1009)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 1001 = Opcode Command Field: Read BD ADDR (0x009)
    Status: Success (0x00)
    BD_ADDR: BarrotTe_50:67:20 (04:7f:0e:50:67:20)
    [Command in frame: 11]
    [Command-Response Delta: 1.227ms]
  • Command Opcode:Read BD ADDR
  • Status:Success,0x00表示success;0x01-0xFF error code
  • BD_ADDR:BarrotTe_50:67:20 (04:7f:0e:50:67:20),Controller的地址
Read Local Supported Commands
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Read Local Supported Commands
    Command Opcode: Read Local Supported Commands (0x1002)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 0010 = Opcode Command Field: Read Local Supported Commands (0x002)
    Parameter Total Length: 0
    [Response in frame: 14]
    [Command-Response Delta: 5.28ms]

这个Command用来获取本地BR/EDR Controller支持的LMP features;

Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 68
    Number of Allowed Command Packets: 1
    Command Opcode: Read Local Supported Commands (0x1002)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 0010 = Opcode Command Field: Read Local Supported Commands (0x002)
    Status: Success (0x00)
    Local Supported Commands: ffffff03ceffefffffffff1ff20fe8fe3ff783ff1c00040061ffffff7fbee0fffff0ffff…
    [Command in frame: 13]
    [Command-Response Delta: 5.28ms]
  • Statue:Success
  • Local Supported Commands:每个bit代表一个LMP features
Read Local Extended Features
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Read Local Extended Features
    Command Opcode: Read Local Extended Features (0x1004)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 0100 = Opcode Command Field: Read Local Extended Features (0x004)
    Parameter Total Length: 1
    Page Number: 0
    [Response in frame: 16]
    [Command-Response Delta: 20.434ms]

这个Command用来获取具体Page number里面的extended LMP features;

  • Page number:用来设置获取某一页的LMP features / extended LMP feature;Page number = 0时,获取的就是Read Local Supported Features command返回的LMP features;
Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 14
    Number of Allowed Command Packets: 1
    Command Opcode: Read Local Extended Features (0x1004)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 0100 = Opcode Command Field: Read Local Extended Features (0x004)
    Status: Success (0x00)
    Page Number: 0
    Max. Page Number: 2
    LMP Features
        .... ...1 = 3-slot packets: True
        .... ..1. = 5-slot packets: True
        .... .1.. = Encryption: True
        .... 1... = Slot Offset: True
        ...1 .... = Timing Accuracy: True
        ..1. .... = Role Switch: True
        .1.. .... = Hold Mode: True
        1... .... = Sniff Mode: True
        .... ...0 = Park Mode: False
        .... ..1. = Power Control Requests: True
        .... .1.. = Channel Quality Driven Data Rate: True
        .... 1... = SCO Link: True
        ...1 .... = HV2 packets: True
        ..1. .... = HV3 packets: True
        .0.. .... = u-law Log Synchronous Data: False
        0... .... = A-law Log Synchronous Data: False
        .... ...1 = CVSD Synchronous Data: True
        .... ..1. = Paging Parameter Negotiation: True
        .... .1.. = Power Control: True
        .... 1... = Transparent Synchronous Data: True
        .000 .... = Flow Control Lag: 0 (0 bytes)
        1... .... = Broadband Encryption: True
        .... ...0 = Reserved: False
        .... ..1. = EDR ACL 2 Mbps Mode: True
        .... .1.. = EDR ACL 3 Mbps Mode: True
        .... 1... = Enhanced Inquiry Scan: True
        ...1 .... = Interlaced Inquiry Scan: True
        ..1. .... = Interlaced Page Scan: True
        .1.. .... = RSSI with Inquiry Results: True
        1... .... = EV3 Packets: True
        .... ...0 = EV4 Packets: False
        .... ..0. = EV5 Packets: False
        .... .0.. = Reserved: False
        .... 1... = AFH Capable Slave: True
        ...1 .... = AFH Classification Slave: True
        ..0. .... = BR/EDR Not Supported: False
        .1.. .... = LE Supported Controller: True
        1... .... = 3-slot EDR ACL packets: True
        .... ...1 = 5-slot EDR ACL packets: True
        .... ..1. = Sniff Subrating: True
        .... .1.. = Pause Encryption: True
        .... 1... = AFH Capable Master: True
        ...1 .... = AFH Classification Master: True
        ..1. .... = EDR eSCO 2 Mbps Mode: True
        .0.. .... = EDR eSCO 3 Mbps Mode: False
        0... .... = 3-slot EDR eSCO Packets: False
        .... ...1 = Extended Inquiry Response: True
        .... ..1. = Simultaneous LE and BR/EDR to Same Device Capable Controller: True
        .... .0.. = Reserved: False
        .... 1... = Secure Simple Pairing: True
        ...1 .... = Encapsulated PDU: True
        ..0. .... = Erroneous Data Reporting: False
        .1.. .... = Non-flushable Packet Boundary Flag: True
        0... .... = Reserved: False
        .... ...1 = Link Supervision Timeout Changed Event: True
        .... ..1. = Inquiry TX Power Level: True
        .... .1.. = Enhanced Power Control: True
        .000 0... = Reserved: False
        1... .... = Extended Features: True
    [Command in frame: 15]
    [Command-Response Delta: 20.434ms]
  • Status:success
  • Page number:表示这是返回的第Page number页的LMP feature / extended LMP features;
  • Maximum Page Number:最多有多少页;
  • Extended LMP Features:每个bit代表一个LMP feature / extended LMP features,0代表不支持,1代表支持;
LMP features 具体的bit位定义
NoSupported featureByteBitvalue
03 slot packets00true
15 slot packets01true
2Encryption02true
3Slot offset03true
4Timing accuracy04true
5Role switch05true
6Hold mode06true
7Sniff mode07true
8Park state10false
9Power control requests11true
10Channel quality driven data rate(CQDDR)12true
11SCO link13true
12HV2 packets14true
13HV3 packets15true
14μ-law log synchronous data16false
15A-law log synchronous data17false
16CVSD synchronous data20true
17Paging parameter negotiation21true
18Power control22true
19Transparent synchronous data23true
20Flow control lag(least significant bit)24false
21Flow control lag(middle bit)25false
22Flow control lag(most significant bit)26false
23Broadcast Encryption27true
24Reserved30false
25Enhanced Data Rate ACL 2 Mb/s mode31true
26Enhanced Data Rate ACL 3 Mb/s mode32true
27Enhanced inquiry scan33true
28Interlaced inquiry scan34true
29Interlaced page scan35true
30RSSI with inquiry results36true
31Extended SCO link(EV3 packets)37true
32EV4 packets40false
33EV5 packets41false
34Reserved42false
35AFH capable slave43true
36AFH classification slave44true
37BR/EDR Not Supported45false
38LE Supported(Controller)46true
393-slot Enhanced Data Rate ACL packets47true
405-slot Enhanced Data Rate ACL packets50true
41Sniff subrating51true
42Pause encryption52true
43AFH capable master53true
44AFH classification master54true
45Enhanced Data Rate eSCO 2 Mb/s mode55true
46Enhanced Data Rate eSCO 3 Mb/s mode56false
473-slot Enhanced Data Rate eSCO packets57false
48Extended Inquiry Response60true
49Simultaneous LE and BR/EDR to Same Device Capable(Controller)61true
50Reserved62false
51Secure Simple Pairing63true
52Encapsulated PDU64true
53Erroneous Data REporting65false
54Non-flushable Packet Boundary Flag66true
55Reserved67false
56Link Supervision Timeout Changed Event70true
57Inquiry TX Power Level71true
58Enhanced Power Control72true
59Reserved73false
60Reserved74false
61Reserved75false
62Reserved76false
63Extended features77true
Write Simple Pairing Mode
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Write Simple Pairing Mode
    Command Opcode: Write Simple Pairing Mode (0x0c56)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0101 0110 = Opcode Command Field: Write Simple Pairing Mode (0x056)
    Parameter Total Length: 1
    Simple Pairing Mode: enabled (1)
    [Response in frame: 18]
    [Command-Response Delta: 2.935ms]

这个命令用来配置本地Controller是否支持Simple Pairing Mode(写入简单配对模式),如果不设置,默认是不支持的,如果支持,当收到IO capability request时应该作出回应,如果不支持,当收到IO capability request时应该拒绝;

  • Simple_Pairing_Mode:0x00表示不支持(默认),0x01表示支持;

这个命令应该在page scan之前执行,最好在HCI Write Scan Enable前面;

Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 4
    Number of Allowed Command Packets: 1
    Command Opcode: Write Simple Pairing Mode (0x0c56)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0101 0110 = Opcode Command Field: Write Simple Pairing Mode (0x056)
    Status: Success (0x00)
    [Command in frame: 17]
    [Command-Response Delta: 2.935ms]
  • Status:0x00表示成功,其他表示失败;
Write LE Host Supported
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Write LE Host Supported
    Command Opcode: Write LE Host Supported (0x0c6d)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0110 1101 = Opcode Command Field: Write LE Host Supported (0x06d)
    Parameter Total Length: 2
    LE Supported Host: true (0x01)
    Simultaneous LE Host: true (0x01)
    [Response in frame: 20]
    [Command-Response Delta: 3.749ms]

这个命令用于支持BLE command;

  • LE Supported Host:true
  • Simultaneous LE Host:true
Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 4
    Number of Allowed Command Packets: 1
    Command Opcode: Write LE Host Supported (0x0c6d)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0110 1101 = Opcode Command Field: Write LE Host Supported (0x06d)
    Status: Unsupported Feature or Parameter Value (0x11)
    [Command in frame: 19]
    [Command-Response Delta: 3.749ms]
  • Status:0x00表示成功,其他表示失败;
Read Local Extended Features
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Read Local Extended Features
    Command Opcode: Read Local Extended Features (0x1004)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 0100 = Opcode Command Field: Read Local Extended Features (0x004)
    Parameter Total Length: 1
    Page Number: 1
    [Response in frame: 22]
    [Command-Response Delta: 0.989ms]

这个Command用来获取具体Page number里面的extended LMP features;

Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 14
    Number of Allowed Command Packets: 1
    Command Opcode: Read Local Extended Features (0x1004)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 0100 = Opcode Command Field: Read Local Extended Features (0x004)
    Status: Success (0x00)
    Page Number: 1
    Max. Page Number: 2
    LMP Features
        .... ...1 = Secure Simple Pairing Host: True
        .... ..1. = LE Supported Host: True
        .... .0.. = Simultaneous LE and BR/EDR to Same Device Capable Host: False
        .... 0... = Secure Connections Host: False
        0000 .... = Reserved: 0x0
        Reserved: 00000000000000
    [Command in frame: 21]
    [Command-Response Delta: 0.989ms]
  • Status:Success
  • Page Number = 1:用来设置获取某一页的LMP features/extended LMP features;Page number为0时,获取的就是Read Local Supported Features command返回的LMP features;
  • Maximum Page Number = 2:最多有多少页
  • LMP Features:支持的LMP features;
Write Secure Connections Host Support
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Write Secure Connections Host Support
    Command Opcode: Write Secure Connections Host Support (0x0c7a)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0111 1010 = Opcode Command Field: Write Secure Connections Host Support (0x07a)
    Parameter Total Length: 1
    Secure Connection Host Support: Enable (0x01)
    [Response in frame: 26]
    [Command-Response Delta: 1.76ms]

这个命令用于设置安全连接主机;

  • Secure Connection Host Support:Enable
Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 4
    Number of Allowed Command Packets: 1
    Command Opcode: Write Secure Connections Host Support (0x0c7a)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0111 1010 = Opcode Command Field: Write Secure Connections Host Support (0x07a)
    Status: Success (0x00)
    [Command in frame: 25]
    [Command-Response Delta: 1.76ms]
  • Status:Success
LE Set Event Mask
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - LE Set Event Mask
    Command Opcode: LE Set Event Mask (0x2001)
        0010 00.. .... .... = Opcode Group Field: LE Controller Commands (0x08)
        .... ..00 0000 0001 = Opcode Command Field: LE Set Event Mask (0x001)
    Parameter Total Length: 8
    LE Event Mask: 0x0000000000021e7f, LE Extended Advertising Set Terminated, LE Extended Advertising Report, LE PHY Update Complete, LE Direct Advertising Report, LE Enhanced Connection Complete, LE Data Length Change, LE Remote Connection P
        0000 0000 0000 0000 0000 0000 0000 0... .... .... .... .... .... .... .... .... = Reserved: 0x00000000
        .... .... .... .... .... .... .... .0.. .... .... .... .... .... .... .... .... = LE Subrate Changed: False
        .... .... .... .... .... .... .... ..0. .... .... .... .... .... .... .... .... = LE BIGInfo Advertising Report: False
        .... .... .... .... .... .... .... ...0 .... .... .... .... .... .... .... .... = LE Transmit Power Reporting: False
        .... .... .... .... .... .... .... .... 0... .... .... .... .... .... .... .... = LE Path Loss Threshold: False
        .... .... .... .... .... .... .... .... .0.. .... .... .... .... .... .... .... = LE Request Peer SCA Complete: False
        .... .... .... .... .... .... .... .... ..0. .... .... .... .... .... .... .... = LE BIG Sync Lost: False
        .... .... .... .... .... .... .... .... ...0 .... .... .... .... .... .... .... = LE BIG Sync Established: False
        .... .... .... .... .... .... .... .... .... 0... .... .... .... .... .... .... = LE Terminate BIG Complete: False
        .... .... .... .... .... .... .... .... .... .0.. .... .... .... .... .... .... = LE Create BIG Complete: False
        .... .... .... .... .... .... .... .... .... ..0. .... .... .... .... .... .... = LE CIS Request: False
        .... .... .... .... .... .... .... .... .... ...0 .... .... .... .... .... .... = LE CIS Established: False
        .... .... .... .... .... .... .... .... .... .... 0... .... .... .... .... .... = LE Periodic Advertising Sync Transfer Received: False
        .... .... .... .... .... .... .... .... .... .... .0.. .... .... .... .... .... = LE CTE Request Failed: False
        .... .... .... .... .... .... .... .... .... .... ..0. .... .... .... .... .... = LE Connection IQ Report: False
        .... .... .... .... .... .... .... .... .... .... ...0 .... .... .... .... .... = LE Connectionless IQ Report: False
        .... .... .... .... .... .... .... .... .... .... .... 0... .... .... .... .... = LE Channel Selection Algorithm: False
        .... .... .... .... .... .... .... .... .... .... .... .0.. .... .... .... .... = LE Scan Request Received: False
        .... .... .... .... .... .... .... .... .... .... .... ..1. .... .... .... .... = LE Extended Advertising Set Terminated: True
        .... .... .... .... .... .... .... .... .... .... .... ...0 .... .... .... .... = LE Extended Scan Timeout: False
        .... .... .... .... .... .... .... .... .... .... .... .... 0... .... .... .... = LE Periodic Advertising Sync Lost: False
        .... .... .... .... .... .... .... .... .... .... .... .... .0.. .... .... .... = LE Periodic Advertising Report: False
        .... .... .... .... .... .... .... .... .... .... .... .... ..0. .... .... .... = LE Periodic Advertising Sync Established: False
        .... .... .... .... .... .... .... .... .... .... .... .... ...1 .... .... .... = LE Extended Advertising Report: True
        .... .... .... .... .... .... .... .... .... .... .... .... .... 1... .... .... = LE PHY Update Complete: True
        .... .... .... .... .... .... .... .... .... .... .... .... .... .1.. .... .... = LE Direct Advertising Report: True
        .... .... .... .... .... .... .... .... .... .... .... .... .... ..1. .... .... = LE Enhanced Connection Complete: True
        .... .... .... .... .... .... .... .... .... .... .... .... .... ...0 .... .... = LE Generate DHKey Complete: False
        .... .... .... .... .... .... .... .... .... .... .... .... .... .... 0... .... = LE Read Local P-256 Public Key Complete: False
        .... .... .... .... .... .... .... .... .... .... .... .... .... .... .1.. .... = LE Data Length Change: True
        .... .... .... .... .... .... .... .... .... .... .... .... .... .... ..1. .... = LE Remote Connection Parameter Request: True
        .... .... .... .... .... .... .... .... .... .... .... .... .... .... ...1 .... = LE Long Term Key Request: True
        .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... 1... = LE Read Remote Features Complete: True
        .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... .1.. = LE Connection Update Complete: True
        .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... ..1. = LE Advertising Report: True
        .... .... .... .... .... .... .... .... .... .... .... .... .... .... .... ...1 = LE Connection Complete: True
    [Response in frame: 46]
    [Command-Response Delta: 0.725ms]

该命令用来启动或屏蔽相关的低功耗蓝牙事件;

上面的Event Mask不一一描述了;

Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 4
    Number of Allowed Command Packets: 1
    Command Opcode: LE Set Event Mask (0x2001)
        0010 00.. .... .... = Opcode Group Field: LE Controller Commands (0x08)
        .... ..00 0000 0001 = Opcode Command Field: LE Set Event Mask (0x001)
    Status: Success (0x00)
    [Command in frame: 45]
    [Command-Response Delta: 0.725ms]
  • Status:Success;
Set Event Mask
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Set Event Mask
    Command Opcode: Set Event Mask (0x0c01)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0000 0001 = Opcode Command Field: Set Event Mask (0x001)
    Parameter Total Length: 8
    .... ...1 = Inquiry Complete: true (0x1)
    .... ..1. = Inquiry Result: true (0x1)
    .... .1.. = Connect Complete: true (0x1)
    .... 1... = Connect Request: true (0x1)
    ...1 .... = Disconnect Complete: true (0x1)
    ..1. .... = Auth Complete: true (0x1)
    .1.. .... = Remote Name Req Complete: true (0x1)
    1... .... = Encrypt Change: true (0x1)
    .... ...1 = Change Connection Link Key Complete: true (0x1)
    .... ..1. = Master Link Key Complete: true (0x1)
    .... .1.. = Read Remote Supported Features: true (0x1)
    .... 1... = Read Remote Ver Info Complete: true (0x1)
    ...1 .... = QoS Setup Complete: true (0x1)
    1... .... = Hardware Error: true (0x1)
    .... ...1 = Flush Occurred: true (0x1)
    .... ..1. = Role Change: true (0x1)
    .... 1... = Mode Change: true (0x1)
    ...1 .... = Return Link Keys: true (0x1)
    ..1. .... = PIN Code Request: true (0x1)
    .1.. .... = Link Key Request: true (0x1)
    1... .... = Link Key Notification: true (0x1)
    .... ...1 = Loopback Command: true (0x1)
    .... ..1. = Data Buffer Overflow: true (0x1)
    .... .1.. = Max Slots Change: true (0x1)
    .... 1... = Read Clock Offset Complete: true (0x1)
    ...1 .... = Connection Packet Type Changed: true (0x1)
    ..1. .... = QoS Violation: true (0x1)
    .1.. .... = Page Scan Mode Change: true (0x1)
    1... .... = Page Scan Repetition Mode Change: true (0x1)
    .... ...1 = Flow Specification Complete: true (0x1)
    .... ..1. = Inquiry Result With RSSI: true (0x1)
    .... .1.. = Read Remote Ext. Features Complete: true (0x1)
    .... 1... = Synchronous Connection Complete: true (0x1)
    ...1 .... = Synchronous Connection Changed: true (0x1)
    ..1. .... = Sniff Subrate: true (0x1)
    .1.. .... = Extended Inquiry Result: true (0x1)
    1... .... = Encryption Key Refresh Complete: true (0x1)
    .... ...1 = IO Capability Request: true (0x1)
    .... ..1. = IO Capability Response: true (0x1)
    .... .1.. = User Confirmation Request: true (0x1)
    .... 1... = User Passkey Request: true (0x1)
    ...1 .... = Remote OOB Data Request: true (0x1)
    ..1. .... = Simple Pairing Complete: true (0x1)
    1... .... = Link Supervision Timeout Changed: true (0x1)
    .... ...1 = Enhanced Flush Complete: true (0x1)
    .... .1.. = User Passkey Notification: true (0x1)
    .... 1... = Keypress Notification: true (0x1)
    [Response in frame: 48]
    [Command-Response Delta: 0.754ms]

该命令用于控制HCI为Host生成哪些事件;

Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 4
    Number of Allowed Command Packets: 1
    Command Opcode: Set Event Mask (0x0c01)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0000 0001 = Opcode Command Field: Set Event Mask (0x001)
    Status: Success (0x00)
    [Command in frame: 47]
    [Command-Response Delta: 0.754ms]
  • Status:Success;
Read Local Supported Codecs
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Read Local Supported Codecs
    Command Opcode: Read Local Supported Codecs (0x100b)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 1011 = Opcode Command Field: Read Local Supported Codecs (0x00b)
    Parameter Total Length: 0
    [Response in frame: 50]
    [Command-Response Delta: 3.624ms]

该命令用于读取本地芯片支持的编码格式;

Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 12
    Number of Allowed Command Packets: 1
    Command Opcode: Read Local Supported Codecs (0x100b)
        0001 00.. .... .... = Opcode Group Field: Informational Parameters (0x04)
        .... ..00 0000 1011 = Opcode Command Field: Read Local Supported Codecs (0x00b)
    Status: Success (0x00)
    Number of Supported Codecs: 6
    Codecs
        Codec: u-Law log (0x00)
        Codec: A-law log (0x01)
        Codec: CVSD (0x02)
        Codec: Transparent (0x03)
        Codec: Linear PCM (0x04)
        Codec: mSBC (0x05)
    Number of Supported Vendor Codecs: 0
    Vendor Codecs
    [Command in frame: 49]
    [Command-Response Delta: 3.624ms]
  • u-Law log:u-Law算法
  • A-law log:A-Law(A律)算法,PCM音频压缩,非均匀量化;
  • CVSD:该编码方式的数据传输使用 SCO 或 eSCO 链路。链路的选取是根据本端 Controller是否支持 Enhanced Setup Synchronous Connection指令来决定的,如果支持该命令则创建语音音频链路时使用 eSCO,否则创建 SCO 链路;
  • Transparent:透明度;
  • Linear PCM:LPCM(线性脉冲编码调制)是一种非压缩音频数字化技术,是一种未压缩的原音重现,是非压缩的数字化技术,码率较大;
  • mSBC:该编码方式的数据传输只能使用 eSCO 链路;

Controller Start Up Complete

BT_Controller_start_up_complete流程分析.png

这个过程就是controller start_up成功之后的reset_complete的逻辑以及对应的响应HCI Command;

Write Inquiry Mode
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Write Inquiry Mode
    Command Opcode: Write Inquiry Mode (0x0c45)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0100 0101 = Opcode Command Field: Write Inquiry Mode (0x045)
    Parameter Total Length: 1
    Inquiry Mode: Results With RSSI or Extended Results (2)
    [Response in frame: 56]
    [Command-Response Delta: 1.051ms]

该命令用于把搜索模式写入到芯片中;

  • Inquiry Mode:Results With RSSI or Extended Results (2) -- 查询结果带有RSSI的格式或者额外的查询结果格式,Inquiry Result with RSSI format or Extended Inquiry Result format。Extended Inquiry Result format:除了带有RSSI,可能带有设备名字之类的信息。

Response的Status为Success;

Write Page Scan Type
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Write Page Scan Type
    Command Opcode: Write Page Scan Type (0x0c47)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0100 0111 = Opcode Command Field: Write Page Scan Type (0x047)
    Parameter Total Length: 1
    Scan Type: Interlaced Scan (1)
    [Response in frame: 58]
    [Command-Response Delta: 1.702ms]

该命令用于配置本地BR/EDR Controller的Page Scan Type;

  • Scan Type = Interlaced Scan (1):0x00表示标准Scan (默认使用这种模式),0x01表示隔行Scan;

Response的Status为Success;

Write Inquiry Scan Type
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Write Inquiry Scan Type
    Command Opcode: Write Inquiry Scan Type (0x0c43)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0100 0011 = Opcode Command Field: Write Inquiry Scan Type (0x043)
    Parameter Total Length: 1
    Scan Type: Interlaced Scan (1)
    [Response in frame: 60]
    [Command-Response Delta: 1.911ms]

该命令用于写查询扫描类型;

  • Scan Type = Interlaced Scan (1):0x00表示标准Scan (默认使用这种模式),0x01表示隔行Scan;

Response的Status为Success;

Write Class of Device
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Write Class of Device
    Command Opcode: Write Class of Device (0x0c24)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0010 0100 = Opcode Command Field: Write Class of Device (0x024)
    Parameter Total Length: 3
    Class of Device: 0x260408 (Audio/Video:Hands-free Device - services: Networking Rendering Audio)
        0000 10.. = Minor Device Class: Hands-free Device (0x02)
        .... ..00 = Format Type: 0x0
        0... .... .... .... = Major Service Classes: Information: False
        .0.. .... .... .... = Major Service Classes: Telephony: False
        ..1. .... .... .... = Major Service Classes: Audio: True
        ...0 .... .... .... = Major Service Classes: Object Transfer: False
        .... 0... .... .... = Major Service Classes: Capturing: False
        .... .1.. .... .... = Major Service Classes: Rendering: True
        .... ..1. .... .... = Major Service Classes: Networking: True
        .... ...0 .... .... = Major Service Classes: Positioning: False
        .... .... 00.. .... = Major Service Classes: Reserved: 0x0
        .... .... ..0. .... = Major Service Classes: Limited Discoverable Mode: False
        .... .... ...0 0100 = Major Device Class: Audio/Video (0x04)
    [Response in frame: 62]
    [Command-Response Delta: 2.61ms]

Class of Device简称CoD,该命令用于向蓝牙芯片写入描述BR / EDR 设备的类型;

CoD用于指出此设备是何种类型,以及支持哪些服务;

  • Minor Device Class:Hands-free Device
  • Major Service Classes:Networking、Rendering、Audio
  • Major Device Class:Audio/Video
通用及设备特定的查询访问码(DIAC)
#LAP ValueUsage
00x9E8B33通用/无限查询访问码(GIAC)
10x9E8B00有限的专用查询访问码(LIAC)
2~630x9E8B01-0x9E8B32, 0x9E8B34-0x9E8B3F保留待将来使用

有限查询访问码(LIAC)仅用于限定的时段,在两边均已明确要进入此状态的情况下使用,通常由用户操作;

GIAC是在允许持续扫描通用查询访问码并在查询时响应;

Major Device Class

主要设备类型,定义蓝牙设备的最高粒度级别。设备的主要功能用于确定主要类分组,由5位二进制来表示,共有32中不同的主要类型;

12111098Major Device ClassDescValue
00000Miscellaneous其他0x00
00001Computer(desktop、notebook、PDA、organizer、...)计算机(台式机、笔记本、PDA)0x01
00010Phone(cellular、cordless、pay phone、modem、...)电话(手机、无线、支付电话、调制解调器)0x02
00011LAN/Network Access pointLAN/网络接入点0x03
00100Audio/Video (headset, speaker, stereo, video display, VCR, …音频/视频(耳机、扬声器、立体声、视频显示、VCR.....0x04
00101Peripheral (mouse, joystick, keyboard, … )配件(鼠标、游戏杆、键盘 .....)0x05
00110Imaging (printer, scanner, camera, display, …)成像(打印、扫描仪、相机、显示 ...)0x06
00111Wearable可穿戴0x07
01000Toy玩具0x08
01001Health健康0x09
11111Uncategorized: device code not specified未分类:未指定设备代码0x31
XXXXXAll other values reserved所有其他保留值0xXX

我们可以看到,在Major Device Class中,又可以划分为多个不同类型的细小的种类,例如,Audio/Video中,包含了headset、speaker、stereo等分类,该分类对应的就是Minor Device Class;

Minor Device Class

Minor Device Class 字段(CoD 中的位7至2)应仅在Major Device Class(但独立于服务类字段)环境中解释。因此随着“Major Device Class 字段”的值变动,位的意义也会改变。当Minor Device Class 字段指示设备类时,则应报告Major Device Class,例如也可以用作无线手持设备的手机应在Minor Device Class 中使用“Cellular”;

Computer Major Class

765432Minor Device ClassDescValue
000000Uncategorized, code for device not assigned未分类,未指定设备代码0x00
000001Desktop workstation台式机工作站0x01
000010Server-class computer服务器类计算机0x02
000011Laptop膝上型电脑0x03
000100Handheld PC/PDA (clamshell)手持 PC/PDA (clamshell)0x04
000101Palm-size PC/PDA掌上型 PC/PDA0x05
000110Wearable computer (watch size)可穿戴计算机(手表大小)0x06
000111Tablet平板电脑0x07
XXXXXXAll other values reserved所有其他保留值0xXX

Phone Major Class

765432Minor Device ClassDescValue
000000Uncategorized, code for device not assigned未分类,未指定设备代码0x00
000001Cellular手机0x01
000010Cordless无线0x02
000011Smartphone智能手机0x03
000100Wired modem or voice gateway有线调制解调或语音网关0x04
000101Common ISDN access通用 ISDN 接入0x05
XXXXXXAll other values reserved所有其他保留值0xXX

Audio/Video Major Class

765432Minor Device ClassDescValue
000000Uncategorized, code for device not assigned未分类,未指定设备代码0x00
000001Wearable Headset Device可穿戴耳机设备0x01
000010Hands-free Device免提设备0x02
000011(Reserved)(保留)0x03
000100Microphone麦克风0x04
000101Loudspeaker扬声器0x05
000110Headphones耳机0x06
000111Portable Audio便携式音频0x07
001000Car Audio汽车音频0x08
001001Set-top box机顶盒0x09
001010HiFi Audio DeviceHiFi 音频设备0x0A
001011VCRVCR0x0B
001100Video Camera摄像机0x0C
001101Camcorder便携式摄像放像一体机0x0D
001110Video Monitor视频监控0x0E
001111Video Display and Loudspeaker视频显示器和扬声器0x0F
010000Video Conferencing视频会议0x10
010001(Reserved)(保留)0x11
010010Gaming/Toy游戏/玩具0x12
XXXXXXAll other values reserved所有其他保留值0xXX
Major Service Classes

主要和次要设备类用于定义Bluetooth SIG成员希望与其应用程序相关联的设备通用系列。不应仅根据主要或次要设备类的分配而作出有关任何应用的特定功能或特性的假设。

bitMajor Service ClassDesc
13Limited Discoverable Mode有限可发现模式[Ref #1]
14LE audio低功耗Audio
15(reserved)保留
16Positioning(Location identification)定位(位置标识)
17Networking(LAN,Ad hoc)网络(LAN、Ad hoc ...)
18Rendering(Printing,Speakers,...)渲染(打印、扬声器 ...)
19Capturing(Scanner,Microphone,...)捕捉(扫描仪、麦克风 ...)
20Object Transfer(v-Inbox,v-Folder,...)对象传输(v-Inbox、v-Folder ...)
21Audio(Speaker,Microphone,Headset service,...)音频(扬声器、麦克风、耳机服务 ...)
22Telephony(Cordless telephony,Modem,Headset service,...)电话(无绳电话、调制解调器、耳机服务 ...)
23Informat(WEB-server,WAP-server,...)信息(WEB 服务器、WAP 服务器)
Write Page Timeout
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Write Page Timeout
    Command Opcode: Write Page Timeout (0x0c18)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0001 1000 = Opcode Command Field: Write Page Timeout (0x018)
    Parameter Total Length: 2
    Timeout: 8192 slots (5120 msec)
    [Response in frame: 64]
    [Command-Response Delta: 0.837ms]

该命令用于向蓝牙芯片写入寻呼超时时间;

  • Timeout:8192 slots,即5120 ms;

Response的Status为Success;

Write Default Link Policy Settings
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Write Default Link Policy Settings
    Command Opcode: Write Default Link Policy Settings (0x080f)
        0000 10.. .... .... = Opcode Group Field: Link Policy Commands (0x02)
        .... ..00 0000 1111 = Opcode Command Field: Write Default Link Policy Settings (0x00f)
    Parameter Total Length: 2
    .... .... .... ...1 = Enable Master Slave Switch: true (1)
    .... .... .... ..1. = Enable Hold Mode: true (1)
    .... .... .... .1.. = Enable Sniff Mode: true (1)
    .... .... .... 0... = Enable Park Mode: false (0)
    [Response in frame: 66]
    [Command-Response Delta: 1.518ms]

该命令用于向蓝牙芯片写入默认的连接策略;

  • Enable Master Slave Switch = true:支持主设备/从设备开关
ValueParameter DescriptionDesc
0x00Active mode
0x01Hold mode保持模式,蓝牙节能模式中最简单的一种,主从设备将对从设备处于保持模式的时间进行协商 ,一旦连接处于保持模式,此连接就不再支持数据包
0x02Sniff mode呼吸模式,通过减少主设备 发送数据 的时隙数并相应减少从设备监听的时隙数,从而达到节省电源的目的;
0x03Park State休眠模式,暂停模式可以最大限度的节省电源,但是,当设备处于暂停模式时,就不再发送和接收用户数据,也不能维护已经建立的SCO链路;
0x04-0xFFReserved for future use

Response的Status为Success;

Read Local Name
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Read Local Name
    Command Opcode: Read Local Name (0x0c14)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0001 0100 = Opcode Command Field: Read Local Name (0x014)
    Parameter Total Length: 0
    [Response in frame: 68]
    [Command-Response Delta: 3.78ms]

该命令用于读取芯片的蓝牙名称;

Bluetooth HCI H4
    [Direction: Rcvd (0x01)]
    HCI Packet Type: HCI Event (0x04)
Bluetooth HCI Event - Command Complete
    Event Code: Command Complete (0x0e)
    Parameter Total Length: 252
    Number of Allowed Command Packets: 1
    Command Opcode: Read Local Name (0x0c14)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0001 0100 = Opcode Command Field: Read Local Name (0x014)
    Status: Success (0x00)
    Device Name: 
    [Command in frame: 67]
    [Command-Response Delta: 3.78ms]

对应返回的Response的Device Name为空;

Change Local Name
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Change Local Name
    Command Opcode: Change Local Name (0x0c13)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0001 0011 = Opcode Command Field: Change Local Name (0x013)
    Parameter Total Length: 248
    Device Name: HAVAL_6720
    [Response in frame: 70]
    [Command-Response Delta: 15.528ms]

该命令用于设置本地蓝牙名称;

  • Device Name = HAVAL_6720:用于设置的蓝牙名称;

Response的Status为Success;

Write Extended Inquiry Response
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - Write Extended Inquiry Response
    Command Opcode: Write Extended Inquiry Response (0x0c52)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0101 0010 = Opcode Command Field: Write Extended Inquiry Response (0x052)
    Parameter Total Length: 241
    FEC Required: true (1)
    Extended Inquiry Response Data
        Device Name: HAVAL_6720
            Length: 11
            Type: Device Name (0x09)
            Device Name: HAVAL_6720
        16-bit Service Class UUIDs
            Length: 1
            Type: 16-bit Service Class UUIDs (0x03)
        32-bit Service Class UUIDs
            Length: 1
            Type: 32-bit Service Class UUIDs (0x05)
        128-bit Service Class UUIDs
            Length: 1
            Type: 128-bit Service Class UUIDs (0x07)
        Unused
    [Response in frame: 72]
    [Command-Response Delta: 6.164ms]

该命令用于把Extended inquiry response写入到芯片;主要用于设备被搜索时回复的Response;

  • FEC Required = true:表示是否需要FEC编码;0x00表示不需要,0x01表示需要

  • Extended Inquiry Response Data:需要设置的extended inquiry response数据,注意这个数据在执行HCI Reset以后不会被保留,初始值是全0;这个对应的就是低功耗蓝牙广播数据类型;

    • Device Name:蓝牙设备名称,0x09对应的为Complete Local Name;
    • 16-bit Service Class UUIDs:16位服务等级UUID列表,0x03对应的为Complete List of 16-bit Service Class UUIDs,完整列表;
    • 32-bit Service Class UUIDs:32位服务等级UUID列表,0x05对应的为Complete List of 32-bit Service Class UUIDs;
    • 128-bit Service Class UUIDs:128位服务等级UUID列表,0x07对应的为Complete List of 128-bit Service Class UUIDs;

目前的sent过程中并没有写入任何的Service UUID信息;

Response的Status为Success;

LE Set Random Address
Bluetooth HCI H4
    [Direction: Sent (0x00)]
    HCI Packet Type: HCI Command (0x01)
Bluetooth HCI Command - LE Set Random Address
    Command Opcode: LE Set Random Address (0x2005)
        0010 00.. .... .... = Opcode Group Field: LE Controller Commands (0x08)
        .... ..00 0000 0101 = Opcode Command Field: LE Set Random Address (0x005)
    Parameter Total Length: 6
    BD_ADDR: 76:18:40:57:a8:b2 (76:18:40:57:a8:b2)
    [Response in frame: 76]
    [Command-Response Delta: 0.76ms]

该命令用于设置一个新的Random地址,包括Resolvable private address类型的地址;

  • BD_ADDR = 76:18:40:57:a8:b2:本地设备的mac地址;
Resolvable private address应用场景及HCI命令介绍

BLE Resolvable private address的解析和过滤操作是在Link Layer实现的,因而为BLE的广播通信提供了一个相对安全的加密环境。Link Layer以Resolving List的形式,通过HCI向Host提供相关的控制API,以实现相应的功能,相关的HCI命令介绍如下:

  • LE Set Random Address Command:设置一个新的Random地址,包括Resolvable private address类型的地址。
  • LE Add Device to Resolving List Command:将指定的设备添加到本机的Resolving List中,需要指定的参数包括:需要添加设备的地址(包括地址类型)、需要添加设备的IRK、本设备的IRK。
  • LE Remove Device From Resolving List Command:将指定设备从本机的Resolving List中删除。
  • LE Clear Resolving List Command:清除本机的Resolving List。
  • LE Read Resolving List Size Command:读取本机Resolving List的大小。
  • LE Read Peer Resolvable Address Command:读取对端设备解析后的Resolvable private address。
  • LE Read Local Resolvable Address Command:读取本机设备解析后的Resolvable private address。
  • LE Set Address Resolution Enable Command:禁止/使能地址解析功能。

Response的Status为Success;

Write Extended Inquiry Response
Bluetooth HCI Command - Write Extended Inquiry Response
    Command Opcode: Write Extended Inquiry Response (0x0c52)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0101 0010 = Opcode Command Field: Write Extended Inquiry Response (0x052)
    Parameter Total Length: 241
    FEC Required: true (1)
    Extended Inquiry Response Data
        Device Name: HAVAL_6720
            Length: 11
            Type: Device Name (0x09)
            Device Name: HAVAL_6720
        16-bit Service Class UUIDs
            Length: 15
            Type: 16-bit Service Class UUIDs (0x03)
            UUID 16: OBEX Object Push (0x1105)
            UUID 16: Audio Sink (0x110b)
            UUID 16: A/V Remote Control Target (0x110c)
            UUID 16: A/V Remote Control (0x110e)
            UUID 16: Handsfree (0x111e)
            UUID 16: SIM Access (0x112d)
            UUID 16: PnP Information (0x1200)
        32-bit Service Class UUIDs
            Length: 1
            Type: 32-bit Service Class UUIDs (0x05)
        128-bit Service Class UUIDs
            Length: 1
            Type: 128-bit Service Class UUIDs (0x07)
        Unused
    [Response in frame: 100]
    [Command-Response Delta: 2.617ms]

  • OBEX Object Push
  • Audio Sink
  • A/V Remote Control Target
  • A/V Remote Control
  • Handsfree
  • SIM Access
  • PnP Information

这个过程其实就是为本地蓝牙设备适配对应的Service,在后续的过程中,决定了哪些相关的ProfileService需要被启动;

而且上述涉及到的Service都是通过一次次的write指令写入的,每次只能写入一个Service UUID;

Response的Status为Success;

Write Voice Setting
Bluetooth HCI Command - Write Voice Setting
    Command Opcode: Write Voice Setting (0x0c26)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0010 0110 = Opcode Command Field: Write Voice Setting (0x026)
    Parameter Total Length: 2
    0000 00.. .... .... = Unused bits: 0x00
    .... ..00 .... .... = Input Coding: Linear (0)
    .... .... 01.. .... = Input Data Format: 2's complement (1)
    .... .... ..1. .... = Input Sample Size: 16 bit (only for Linear PCM) (1)
    .... .... ...0 00.. = Linear PCM Bit Position: 0
    .... .... .... ..00 = Air Coding Format: CVSD (0)
    [Response in frame: 88]
    [Command-Response Delta: 5.55ms]

该命令用于将Voice Setting写入到芯片,主要是用于语音连接的相关配置;

  • Unused bits:
  • Input Coding:输入编码方式
  • Input Data Format:输入数据格式
  • Input Sample Size:采样率
  • Linear PCM Bit Position:
  • Air Coding Format:空中编码格式

Response的Status为Success;

Write Inquiry Scan Activity
Bluetooth HCI Command - Write Inquiry Scan Activity
    Command Opcode: Write Inquiry Scan Activity (0x0c1e)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0001 1110 = Opcode Command Field: Write Inquiry Scan Activity (0x01e)
    Parameter Total Length: 4
    Interval: 2048 slots (1280 msec)
    Window: 18 slots (11.25 msec)
    [Response in frame: 90]
    [Command-Response Delta: 19.117ms]

该命令用于向芯片写入查询扫描的周期性间隔大小和周期大小;

  • Interval:周期性扫描的间隔大小,2048 slots
  • Window:周期大小,18 slots

Response的Status为Success;

Write Scan Enable
Bluetooth HCI Command - Write Scan Enable
    Command Opcode: Write Scan Enable (0x0c1a)
        0000 11.. .... .... = Opcode Group Field: Host Controller & Baseband Commands (0x03)
        .... ..00 0001 1010 = Opcode Command Field: Write Scan Enable (0x01a)
    Parameter Total Length: 1
    Scan Enable: Inquiry Scan disabled/Page Scan enabled (0x02)
    [Response in frame: 94]
    [Command-Response Delta: 9.467ms]

该命令主要是用于设置Scan Enable的值,其中包含了两种Scan:inquiry Scan、Page Scan;

  • Scan Enable

    • Inquiry Scan:disabled
    • Page Scan:enabled
inquiry & page

我们描述一下inquiry和page的区分:

  • inquiry:让蓝牙芯片进入发现周围蓝牙设备的模式,对应的返回值为周围蓝牙设备的信号;

  • page:发起连接的蓝牙设备向被连接的蓝牙设备发起连接请求或者是认证,请求即一次page动作。

    手机点击请求连接目标蓝牙设备的过程中发送的很多命令都会进行page动作;

inquiry scan和page scan这两个实际上是一个状态的描述,inquiry和page代表的是动作;

  • inquiry scan:这就是我们通常看到的可被发现的设备。体现在上层就是我们在android系统中点击设备可被周围什么发现,那设备就处于这样的状态;
  • page scan:这个子状态是和page对应的,它就是等待被page的slave所处的状态,换句话说,若想被page到,我们就要处于page scan的状态;

Response的Status为Success;

总结

上述涉及到的HCI Command都是在BT 启动过程中涉及到的一些Command,用于初始化配置Host和Controller模块;