Android蓝牙开发常见问题
1 H4
H4:5線串口,TxD/RxD/Gnd/CTS/RTS
Figure 1-1 Why call UART H4
2 BR and EDR
BR:
68-bit or 72-bit Access Code +
54-bit Header +
0-2790 bits Payload
EDR:
68-bit or 72-bit Access Code +
54-bit Header +
5-us Guard +
11-us Sync +
0-2790 bits EDR Payload +
2-symbol Trailer
In EDR, Access Code + Header use GFSK, Sync + EDR Payload + Trailer use DPSK.
BLE:
1-byte or 2-byte Preamble +
4-byte Access Address +
2-258 bytes PDU +
3-byte CRC +
16-160 us Constant Tone Extension
ScanA and AdvA in PDU are the Bluetooth MAC.
3 bluedroid
3.1 MSM NVM
NV項是被存儲在EFS文件系統(efs.img)中的 /nvm/num/ 或者 /nvm/context%d/ 子目錄下。
3.2 HAL
@ hardware/qcom/bt/libbt-vendor/
NV_BD_ADDR_I: 447
ro.hardware.bluetooth
@ device/qcom/common/bdroid_buildcfg.h
#define BTM_DEF_LOCAL_NAME "QCOM-BTD"
3.3 HCI dump
@ /system/etc/bluetooth/bt_stack.conf
BtSnoopLogOutput=true
BtSnoopFileName=/sdcard/btsnoop_hci.log
TRC_HCI=5
[Settings][Developer options][Enable Bluetooth HCI snoop log]
btsnoop_hci.log
3.4 判斷/dev/ttyS1是否被藍牙進程打開?
lsof | grep /dev/ttyS1
3.5 Bond
@ system/bt/btif/src/bluetooth.c
static int create_bond(
? ? ? ? const bt_bdaddr_t *bd_addr,
? ? ? ? int transport);
// For NFC Touch Pairing
static int create_bond_out_of_band(
? ? ? ? const bt_bdaddr_t *bd_addr,
? ? ? ? int transport,
? ? ? ? const bt_out_of_band_data_t *oob_data);
3.6 Wireshark
Keywords: Sent Inquiry, Sent Create Connection, Sent Authentication Request
4 BLE
4.1 2字節或者4字節UUID與128 bit UUID對應關系
假如16 bit的UUID為0x1234,則轉換成128 bit的UUID是:
0x00001234-0000-1000-8000-00805F9B34FB
假如32 bit的UUID是0x12345678,則轉換成128 bit的UUID是:
0x12345678-0000-1000-8000-00805F9B34FB
ATT協議解析UUID支持16 bit和128 bit兩種格式,所以32 bit UUID在ATT PDU發送的時候,需要轉成128 bit發送。
BLE的廣播報文中包含本機所有的服務16 bit UUID。
4.2 3種配對模式
JustWorks, use 000000
6-digit PIN
Out of band
1)雙方都將生成一個16字節的隨機數,同時再結合其它參數(包括密碼),各自生成16字節的Key。
2)然后雙方都將自己的隨機數和Key發給對方,各自計算對方的Key,同時與對方發送過來的Key進行比較,相同則配對成功。
4.3 跳頻
CONNECT_REQ報文中的5字節ChM(Channel Map,位圖方式,每個bit表示一個channel(1表示可用,0表示不可用)和5 bit Hop,5 bit Hop范圍是[5, 16]。
F(n+1) = (F(n) + hop) % 37
其中:
F(n+1)為下一個連接事件使用的信道;
F(n)為當前連接事件使用的信道;
4.4 ATT PDU格式
1字節opcode + 2字節handle + 2或者16字節UUID + 最多20字節數據 + 12字節加密數據
handle取值范圍[1, 0xFFFF]。
屬性權限由高層協議定義,對ATT協議不可見,也就是ATT PDU報文中看不到屬性的權限字段。
Service = Service聲明 + Characteristic聲明 + Characteristic Value聲明
ble - ATT profile詳解
5 BLE Pairing
5.1 配對種類
PIN Code Pairing:需要雙方藍牙設備輸入4位配對密碼
Secure Simple Pairing:SSP,只需要雙方確認屏幕上的6位隨機數相同即可
- BLE選擇何種配對方式,是跟雙方的IO_Capability有關
- 藍牙耳機使用SSP的Just Works -
? BT_SSP_VARIANT_CONSENT
5.2 IO Capability
- BTM_LOCAL_IO_CAPS
- DisplayYesNo - 設置只有輸入YES和NO的能力,能夠顯示
5.3 Android SSP Just Works代碼流程
typedef struct {
? ? [...]
? ? /** BT SSP Reply -
? ? ?* Just Works, Numeric Comparison and
? ? ?* Passkey
? ? ?* passkey shall be zero for
? ? ?* BT_SSP_VARIANT_PASSKEY_COMPARISON
? ? ?* & BT_SSP_VARIANT_CONSENT
? ? ?* For BT_SSP_VARIANT_PASSKEY_ENTRY,
? ? ?* if accept==FALSE, then passkey
? ? ?* shall be zero */
? ? int (*ssp_reply)(const bt_bdaddr_t *bd_addr,
? ? ? ? ? ? bt_ssp_variant_t variant,
? ? ? ? ? ? uint8_t accept,
? ? ? ? ? ? uint32_t passkey);
? ? [...]
};
<bta/include/bta_api.h>
/* Structure associated with
?* BTA_DM_SP_CFM_REQ_EVT */
typedef struct
{
? ? [...]
? ? /* IO Capabilities of local device */
? ? tBTA_IO_CAP ? ? loc_io_caps;
? ? /* IO Capabilities of remote device */
? ? tBTA_AUTH_REQ ? rmt_io_caps;
} tBTA_DM_SP_CFM_REQ;
5.4 CC2564 SSP Just Works
IO_Capability = icNoInputNoOutput
BLE模式免配對用的就是Just Works(密碼是000000)
MITM:Man in The Middle
6 CSR bluecore
6.1 SDK
xIDE for ADK:Audio or Application Development Kit
xIDE for Bluelab
USB-SPI
6.2 CSR芯片版本差異
- rom版本一般是固定功能的,比較常見的是用于dongle和耳機的rom版本芯片,可以修改參數,但基本功能是固定的。
- flash版本可以自己開發固件,相對來說靈活性高一些;開發環境里面自帶HCI以下協議。
7 藍牙抓包工具
- BEX400: Bluetooth Explorer 400, Switzerland Ellisys
- FTS: FrontLine Sniffer, USA LeCroy
- Nordic EN-Dongle: BLE Sniffer and Wireshark
- TI CC2540: SmartRF Packet Sniffer
8 Abbreviations
ADK:Audio or Application Development Kit
BCSP:BlueCore Serial Protocol
COD:Class of Device
CSR:Cambridge Silicon Radio
ECO: Engineering Change Order, for Tapeout bug fix, [8th-Mar-2022]
H2: Part H:2 HCI USB TRANSPORT LAYER
H4: Part H:4 HCI UART TRANSPORT LAYER
PB-ADV: Provisioning Bearer. BLE mesh from CSR Mesh
PSKey:BlueCore Persistent Store Key
transparent:數據的透傳
xIDE:CSR's Integrated Development Environment
總結
以上是生活随笔為你收集整理的Android蓝牙开发常见问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 定柱式旋臂起重机
- 下一篇: android sina oauth2.