一、HCI介紹
HCI: Host Controller Interface,it provides a uniform interface method of accessing a Bluetooth Controller’s capabilities. From [Vol 2] Part E: Host Controller Interface Functional Specification
通過HCI提供的接口,只要按照藍牙協議棧定義的數據格式發送指令/數據,藍牙設備就會執行相應的動作。
HCI在藍牙系統中的層次:
在Linux中,內核基於HCI Driver封裝出了AF_BLUETOOTH類型的socket,用戶空間通過socket接口就可以和peer之間收發數據。之前提到的hciconfig/hcitool工具就是基於socket實現的:
/* Open HCI socket */ if ((ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) { perror("Can't open HCI socket."); exit(1); }
更多細節查看相應命令的源碼。
二、廣播命令解析
注:HCI命令格式定義詳見:5.4 Exchange of HCI-Specific Information
# hcitool -i hci0 cmd 0x08 0x000a 01 < HCI Command: ogf 0x08, ocf 0x000a, plen 1 01 > HCI Event: 0x0e plen 4 01 0A 20 00
0x08:For the LE Controller Commands, the OGF code is defined as 0x08.
0x000A:HCI_LE_Set_Advertise_Enable
01:0x000A命令的參數,1為enable,0為disable廣播
HCI Event:HCI命令交互過程是Request-Response,所以這里就是命令的響應
再比如:
# hcitool -i hci0 cmd 0x08 0x0008 16 02 01 06 03 02 80 ff 0e 09 62 6c 65 5f 6e 61 6d 65 5f 5a 30 30 31
LE設置廣播數據 數據長度
廣播數據包,2個字節,內容01 06
廣播數據包,3個字節,內容02 80 ff
廣播數據包,14個字節,設備簡稱“ble_name_Z001”
廣播數據包格式:CS [Vol 3] Part C, Section 11
三、GATT
GATT:Generic Attribute Profile GATT is designed to be used by an application or another profile, so that a client can communicate with a server.
From [Vol 3] Part G:Generic Attribute Profile(GATT), also refer Part F: Attribute Protocol(ATT)
GATT是低功耗藍牙屬性應用規范,用於主機和從設備之間的數據傳輸。
bluez-5.50/tools/btgatt-server.c和bluez-5.50/tools/btgatt-client.c提供了使用GATT傳輸數據的范例,之前的心率計就是這里實現的,是研究的好資料。
參考
1、Bluetooth Core Specification Version 4.1
2、BlueZ