1. 精讲蓝牙协议栈(Bluetooth Stack):SPP/A2DP/AVRCP/HFP/PBAP/IAP2/HID/MAP/OPP/PAN/GATTC/GATTS/HOGP等协议理论
2. 欢迎大家关注和订阅,【蓝牙协议栈】专栏会持续更新中.....敬请期待!
目录
1. 协议简述
1.1 PBAP
1.2 OBEX
2. PBAP协议栈
3. PBAP协议数据包分析
3.1 同步通讯录
3.1.1 获取联系人数量 - telecom
3.1.2 获取联系人数量 - SIM1/telecom
1. 协议简述
蓝牙电话应用不但需要HFP协议来支持打电话的功能,同时在很多车载蓝牙应用中,都支持查看通讯录和通话记录等信息,而这一部分的所涉及到的协议为PBAP.
1.1 PBAP
PBAP(Phone Book Access Profile):电话本访问协议 ,是一种基于OBEX的上层协议,该协议可以同步手机这些具有电话本功能设备上的通讯录和通话记录等信息,用于访问电话本对象(通过 Vcard形式),是基于客户端/服务器的模型,一般是 client从 server端下载电话本。这个协议是为 HFP/SIM协议设计.
1.2 OBEX
Object Exchange,对象交换协议,来源与红外通讯协议,但又不局限与具体的传输方式,后来被蓝牙组织SIG吸纳其中部分并进行优化处理作为蓝牙协议中的OBEX层用于蓝牙设备间的文件数据传输,如蓝牙传输文件(OPP)、同步电话簿(PBAP)和同步短信(MAP)等场景下都是以OBEX协议组织相关数据进行传输的;
OBEX协议有两种角色:Server和Client,通过request-response(请求-响应)形式进行交互,即客户端Client进行请求,服务端Server响应客户端请求的方式传输数据对象;应用于PBAP协议中,Client只能进行数据的读取操作,不能对源数据进行修改,保证了源数据的安全性;
2. PBAP协议栈
PBAP应用层协议处于最上层,之后就是数据格式处理方式,由于通讯录在手机中都是以vCard的格式存储的,所以这边为vCard的数据处理格式。在往下就是通过OBEX协议层联通蓝牙协议栈中的RFCOMM,最后通过统一的数据传输通道L2CAP链路发送数据;
3. PBAP协议数据包分析
在PBAP协议同步通讯录和通讯记录中,都是基于OBEX协议实现的,PBAP协议作为了应用层协议;
3.1 同步通讯录
3.1.1 获取联系人数量 - telecom
Request:
Frame 485: 84 bytes on wire (672 bits), 84 bytes captured (672 bits)
…………………………
OBEX Protocol
[Profile: PBAP (4)]
[Current Path: /]
.000 0011 = Opcode: Get (0x03)
1... .... = Final Flag: True
Packet Length: 70
[Response in Frame: 489]
Headers
Connection Id: 1
Header Id: Connection Id (0xcb)
11.. .... = Encoding: 4 byte quantity (network order) (0x3)
..00 1011 = Meaning: Connection Id (0x0b)
Connection ID: 1
Name: "telecom/pb.vcf"
Header Id: Name (0x01)
00.. .... = Encoding: Null terminated Unicode text, length prefixed with 2 byte Unsigned Integer (0x0)
..00 0001 = Meaning: Name (0x01)
Length: 33
Name: telecom/pb.vcf
Type: "x-bt/vcard-listing"
Header Id: Type (0x42)
01.. .... = Encoding: Byte sequence, length prefixed with 2 byte Unsigned Integer (0x1)
..00 0010 = Meaning: Type (0x02)
Length: 22
Type: x-bt/vcard-listing
Application Parameters
Header Id: Application Parameters (0x4c)
01.. .... = Encoding: Byte sequence, length prefixed with 2 byte Unsigned Integer (0x1)
..00 1100 = Meaning: Application Parameters (0x0c)
Length: 7
Parameter: Max List Count
Parameter Id: Max List Count (0x04)
Parameter Length: 2
Max List Count: 0 (0x0000)
-
Profile:PBAP (4),上层应用层协议为PBAP,OBEX协议的上层应用层协议除了PBAP,还有OPP、MAP协议;
-
Opcode:操作码,Get (0x03),即Request对应的code为Get;
-
Packet Length:70
-
Response in Frame:该request frame-485对应的Response frame为489;
-
Header - Connection Id = 1:PBAP连接指令中PSE回复的连接ID 号;
-
Header - Name = "telecom/pb.vcf":代表了访问的通讯录路径;
-
Header - Type = "x-bt/vcard-listing":代表了该Request对应的Function为PullvCardListing;
-
Header - Application Parameters
- Parameter Id = 0x04:该ID 对应了Max List Count
- Parameter - Max List Count = 0:在MaxListCount = 0 的情况下,Response返回的PhonebookSize为Name对应路径下所有通讯人的Count;
Response:
Frame 489: 29 bytes on wire (232 bits), 29 bytes captured (232 bits)
………………………………
OBEX Protocol
[Profile: PBAP (4)]
[Current Path: /]
.010 0000 = Response Code: Success (0x20)
1... .... = Final Flag: True
Packet Length: 15
[Request in Frame: 485]
Headers
Connection Id: 1
Header Id: Connection Id (0xcb)
11.. .... = Encoding: 4 byte quantity (network order) (0x3)
..00 1011 = Meaning: Connection Id (0x0b)
Connection ID: 1
Application Parameters
Header Id: Application Parameters (0x4c)
01.. .... = Encoding: Byte sequence, length prefixed with 2 byte Unsigned Integer (0x1)
..00 1100 = Meaning: Application Parameters (0x0c)
Length: 7
Parameter: Phonebook Size
Parameter Id: Phonebook Size (0x08)
Parameter Length: 2
Phonebook Size: 102 (0x0066)
-
Response Code:Success
-
Header - Connection Id = 1:对应了上述Request 的Connection Id;
-
Header - Application Parameters
- Parameter Id = 0x08:该Id代表了Phonebook Size
- Parameter - Phonebook Size = 102:代表了Name对应telecom/pb.vcf的总数
3.1.2 获取联系人数量 - SIM1/telecom
Request:
OBEX Protocol
[Profile: PBAP (4)]
[Current Path: /]
.000 0011 = Opcode: Get (0x03)
1... .... = Final Flag: True
Packet Length: 80
[Response in Frame: 492]
Headers
Connection Id: 1
Header Id: Connection Id (0xcb)
11.. .... = Encoding: 4 byte quantity (network order) (0x3)
..00 1011 = Meaning: Connection Id (0x0b)
Connection ID: 1
Name: "SIM1/telecom/pb.vcf"
Header Id: Name (0x01)
00.. .... = Encoding: Null terminated Unicode text, length prefixed with 2 byte Unsigned Integer (0x0)
..00 0001 = Meaning: Name (0x01)
Length: 43
Name: SIM1/telecom/pb.vcf
Type: "x-bt/vcard-listing"
Header Id: Type (0x42)
01.. .... = Encoding: Byte sequence, length prefixed with 2 byte Unsigned Integer (0x1)
..00 0010 = Meaning: Type (0x02)
Length: 22
Type: x-bt/vcard-listing
Application Parameters
Header Id: Application Parameters (0x4c)
01.. .... = Encoding: Byte sequence, length prefixed with 2 byte Unsigned Integer (0x1)
..00 1100 = Meaning: Application Parameters (0x0c)
Length: 7
Parameter: Max List Count
Parameter Id: Max List Count (0x04)
Parameter Length: 2
Max List Count: 0 (0x0000)
- Header - Name = "SIM1/telecom/pb.vcf":访问路径为SIM卡中的联系人总数;
- Parameter - Max List Count = 0:获取SIM中联系人总数;
Response:
OBEX Protocol
[Profile: PBAP (4)]
[Current Path: /]
.010 0000 = Response Code: Success (0x20)
1... .... = Final Flag: True
Packet Length: 11
[Request in Frame: 490]
Headers
Connection Id: 1
Header Id: Connection Id (0xcb)
11.. .... = Encoding: 4 byte quantity (network order) (0x3)
..00 1011 = Meaning: Connection Id (0x0b)
Connection ID: 1
End Of Body
Header Id: End Of Body (0x49)
01.. .... = Encoding: Byte sequence, length prefixed with 2 byte Unsigned Integer (0x1)
..00 1001 = Meaning: End Of Body (0x09)
Length: 3
Value: <MISSING>
-
End or Body:
- MaxListCount != 0:返回Name对应的数据,回复数据中的vCard对象只应包含使用属性选择器Attribute Selector参数指示的属性,并且应使用格式Format参数指示的格式组装数据;
- MaxListCount == 0:直接返回,代表没有获取的数据;