当前位置:网站首页>Bluetooth development (11) -- ble interacts happily

Bluetooth development (11) -- ble interacts happily

2022-06-11 00:09:00 yuanyun_ elber

The previous chapter mobile phones and flip4 A connection has been established , The next packet format is data channel pdu 了 .

Data channel pdu The format is as follows :

 

We said before , Unlike classic Bluetooth ,LE only one header, and BR There are two Baotou ——packet header and payload header, Actually... Here LLID Be similar to BR Of payload header Medium LLID, there NESN and SN Be similar to BR Of packet header Medium SEQN, Of course, it's not exactly the same , Both the sender and the receiver of the former maintain one sequence number The latter is obviously maintained by only one party .

Similar to classic Bluetooth ,data channel pdu Also according to header Medium LLID Divide into control pdu and data pdu Two kinds of .

After the connection is established ,master and slave Will pass LLCP(link layer control protocl), adopt control pdu Control the connection .

control pdu The format is as follows :

 

among opcode Indicates the type of control package .

Let's see , After this connection , The two sides through LLCP The protocol interacts with some information .

Then you can go through data pdu To interact with the upper level information .

The first is a att exchange mtu Of , This is a mobile notification flip4 Its maximum reception MTU size Of . Notice the red line , First LLID It indicates that this package is a data pdu, then l2cap Layer of cid Seems to be fixed Of 0x4, Said is att package ,att payload Of opcode It means it's a exchange mtu request, Mobile phone mtu yes 185. bluetooth 4.0 The previous standard ,mtu Only 23 byte , This mtu Used to do OTA The application of firmware upgrade is really slow ,4.2 after mtu To expand to 255 byte .

After the exchange MTU after , Mobile phones need to know flip4 This server Supported services 、 characteristic 、 Feature description and other information , It is similar to the classic Bluetooth sdp link .

First you need to get service, obtain service There are two ways , First, access to all services , One is based on uuid Access to services , The method selected here is the former , The process goes like this :

First sent att_read_by_group_type_req Of start handle It must be 1,end handle yes 0xffff,attribute type yes primary service(2800), then server The terminal will start from handle 1 Search service , stay att_read_by_group_type_rsp Found in the reply service, Because I can't reply all at once , So this process needs to be executed circularly , The next time the mobile terminal sends att_read_by_group_type_req Packet start handle Than att_read_by_group_type_rsp Of end handle Add 1.

Let's see flip4 Your first reply :

This reply contains two primary service, Let's put it on record , Three times in total service After searching , a sheet service The list will be displayed :

 

 

Handle

Attribute Type

Attribute Value

1

2800(primary service, Bluetooth association regulations assigned numbers)

1801(GATT service, Bluetooth association regulations assigned numbers)

5

2800

1800(GAP service, Bluetooth association regulations assigned numbers)

10

2800

65786365-6C70-6F69-6E74-2E636F6D0000

16

2800

Fe8f

We know profile yes service Set , Find all service after , You also need to find each service Subset , Include include、characteristic wait , The commands used are ATT_READ_BY_TYPE_REQ.

If you find characteristic, And find characteristic descriptor wait , The command used is ATT_FIND_INFOMATION_REQ.

include It means that we should service Include others service, This process is called relationship discovery, We don't have this process here .

Characteristic It's more important , because service yes characteristic Set .

This process is called characteristic discovery:

Look what we found characteristic, By format , Found characteristic, Is itself a attribute,

Yes 2 Bytes of attribute handel, Here is 2,attribute type yes 2803(characteristic, This is in att_read_by_type_req It contains , Not looking for anything else ),attribute value Include 1 Bytes of property( Determines the read and write permissions, etc ),2 Bytes of value handle(0003), and 2 Bytes bluetooth UUID(2a05),assigned numbers Express service changed.

I usually find characteristic After that, I followed closely characteristic descriptor:

Last , We have formed a service Summary of :

Handle

Attribute Type

Attribute Value

1

2800(primary service, Bluetooth association regulations assigned numbers)

1801(GATT service, Bluetooth association regulations assigned numbers)

2

2803(characteristic)

{20,0003,service changed}

5

2800(primary service)

1800(GAP service, Bluetooth association regulations assigned numbers)

10

2800(primary service)

65786365-6C70-6F69-6E74-2E636F6D0000

11

2803(characteristic)

{12(read,notify), 000c, 65786365-6C70-6F69-6E74-2E636F6D0001}

14

2803(characteristic)

{08(write), 000f, 65786365-6C70-6F69-6E74-2E636F6D0002}

16

2800(primary service)

Fe8f

It can be seen from this table that ,10 Number service It is the core of our reading and writing services service, among , Yes 0x000c Number handle Can be used to read data ,0x000f Number handle Can be used to write data , As you can see below app Just use these two handle Number and flip4 Interact :

This is what I received notify Data packets :

This is a write Data packets of :

Specific data format , It's the top app Need to be concerned about , It's not about the protocol .

原网站

版权声明
本文为[yuanyun_ elber]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020629284167.html