Explain the BLE connection establishment process in detail

The same mobile phone, why can it be successfully connected with some devices, but not with other devices? The same device, why can establish a connection with some mobile phones, but cannot establish a connection with other mobile phones? The same mobile phone and the same device, why are they sometimes connected quickly and sometimes very slowly? What is Master? What is slave? What is Connection event and slave latency? Hope this article can help you answer the above questions.

BLE connection example

Suppose we have a mobile phone A (take an Android phone as an example) and a device B (device name: Nordic_HRM), as shown below, we can connect the two through the Bluetooth interface in the Android settings menu.

  1. Open the Android settings menu
  2. Select the "Bluetooth" entry
  3. Turn on bluetooth
  4. Wait for the system search results, if nothing else, the device "Nordic_HRM" will appear in the results list
  5. Click "Nordic_HRM", the phone will establish a connection with this device

The above is the "connection" that everyone intuitively feels. Then the mobile phone needs to establish a connection with the device Nordic_HRM. What are the specific processes? Why can they connect successfully? Here's to you all.

broadcasting

Before the mobile phone establishes a connection with device B, device B needs to broadcast first, that is, device B (Advertiser) continuously sends the following broadcast signals, and t is the broadcast interval. Each time an advertising packet is sent, we call it an advertising event, so t is also called the advertising event interval. Although the broadcast event is represented by a line in the figure, the broadcast event actually has a duration.

 

The above is just a schematic diagram. According to the Bluetooth spec, each broadcast event actually contains three broadcast packets, that is, the same information is broadcast on the three channels 37/38/39 at the same time, that is, the real broadcast event is as follows. .

 

Device B continuously sends broadcast signals to the mobile phone (Observer). If the mobile phone does not open the scanning window, the mobile phone cannot receive the broadcast of device B. As shown in the figure below, not only the mobile phone needs to open the radio frequency receiving window, but also only the radio frequency receiving window of the mobile phone. The mobile phone can receive the broadcast signal of device B only if it matches the transmission window sent by the broadcast successfully. Since the successful matching is a probabilistic event, it is also a probabilistic event that the mobile phone scans device B. That is to say, the mobile phone sometimes scans device B quickly. For example, only one broadcast event is required, and the mobile phone is sometimes very slow to scan. To device B, say 10 broadcast events or more are needed.

 

 

establish a connection

According to the Bluetooth spec, 150us (T_IFS) after the advertiser sends a broadcast packet, the advertiser must open the RF Rx window for a period of time to receive packets from the observer. The Observer can send a connection request to the advertiser during this time. As shown in the figure below, the mobile phone scans device B during the third broadcast event and sends a connection request conn_req.

 

The interaction process in the above figure is relatively rough. For this reason, we introduce the following figure to describe the connection establishment process in detail.

Figure 5 : Connection Establishment Process

Note: M in the figure represents the mobile phone, S represents the device B , M->S means that the mobile phone sends the data packet to the device B , that is, the mobile phone opens the Tx window, and the device B opens the Rx window; S->M is the opposite, indicating that the device B will The data packet is sent to the mobile phone, that is, device B opens the Tx window, and the mobile phone opens the Rx window.

As shown in the figure, after the mobile phone receives the A1 broadcast packet ADV_IND, it uses it as the initial anchor point (this anchor point is not the anchor point of the connection), and after T_IFS sends a connection request command to the Advertiser, that is, the A2 data packet, telling the advertiser that I It's coming to even you, please be prepared. Advertiser is ready to receive according to connect_req command information, connect_req contains the following key information:

  • Transmit window offset, defined as shown in Figure 5
  • Transmit window size, defined as shown in Figure 5
  • The complete definition of the connect_req packet is as follows  

connect_req is actually telling the advertiser that the mobile phone will send the first synchronization packet (P1) to you during the Transmit Window, please open your RF receiving window during this time. After device B receives P1, it will reply data packet P2 to the mobile phone after T_IFS time. Once the mobile phone receives the data packet P2, the connection can be considered to be established successfully. Subsequent mobile phones will use P1 as the anchor point (origin) and the Connection Interval as the period to periodically send Packet to Device B. In addition to the data transmission function, Packet has the following two very important functions:

  1. Synchronize the clocks of the mobile phone and the device, that is to say, every time the device receives a packet from the mobile phone, it will reset its own timing origin to synchronize with the mobile phone.
  2. Tell the device that you can now pass data to me. After the connection is successful, the BLE communication will become the master-slave mode, so the connection initiator (mobile phone) is called Master or Central , and the connected party (previous Advertiser) is called Slave or Peripheral . The reason why BLE communication is in the master-slave mode is that the slave cannot send information to the master "randomly". It can only send its own data back to the master after the master sends it a packet.

Connection failed

There are several typical connection failure situations as follows:

  1. As shown in Figure 5, if the slave does not receive the P1 sent by the master during the transmit window, the connection will fail. At this time, you should check the problem on the master side to see why the master did not send P1 at the agreed time.
  2. If the master sends P1 during the transmit window, that is to say, the master sends P1 according to the timing agreed by connect_req, but the slave does not send P2 back, then the connection will also fail. At this time, you should investigate the problem on the slave side and see why the slave did not return P2 to the past.
  3. If the master sends P1, and the slave sends P2 back, the host still reports a connection failure. In this case, there may be a problem with the master software, and you need to check the master software carefully.
  4. There is also a more common connection failure situation: too much radio frequency interference in the air. At this time, you should find a clean environment, such as a shielded room, and test whether the connection is normal after eliminating the interference.

Connection events

After the connection is successful, the master and the slave must interact once at the beginning of each connection interval, that is, the master sends a packet to the slave, and the slave sends another packet to the master. The entire interaction process is called a connection event. It is impossible for the master to send data to the slave all the time, so the master sends empty packets to the slave most of the time. Similarly, the slave does not have data to the master all the time, so the packets that the slave replies to the master are mostly empty packets. In addition, during a connection event, the master can also send multiple packets to the slave to improve throughput. In summary, the communication sequence diagram after a successful connection should be as follows:

 

Figure 7: Communication sequence diagram after successful connection (only one packet is sent per connection event)

Figure 9: Communication sequence diagram after successful connection (connection event may send multiple packets)

 

Figure 10: Connection event details

Slave latency

Figure 10 shows slave latency (slave latency = 2), so what is slave latency?

As mentioned above, at the beginning of each connection interval, Master and Slave must interact once, even if the interaction between the two is empty packet (empty packet), but if the slave defines slave latency, such as slave latency = 9, At this time, the slave can only reply to the master every 9 connection intervals, that is to say, the slave can sleep during the first 8 connection intervals, and only reply a packet to the master after the arrival of the 9th connection interval, which will greatly save the slave. power consumption and improve battery life. Of course, if the slave has data that needs to be reported to the master, it can also not wait until the ninth connection interval to report, and just transmit it as normal, which not only saves power consumption, but also improves the real-time performance of data transmission.

GAP Role Summary

For the mobile phone and device B mentioned above, during the BLE communication process, their states are changing over time, and the relationship between the two is also changing. For this reason, the Bluetooth spec gives The phone and device B have different names, that is, GAP defines the roles shown in Figure 11. Figure 11 links the observer, initiator and central through time. In fact, these three roles are independent of each other, that is to say, a device can only support the observer role, but not the initiator and central roles. Similarly, Figure 11 also links the advertiser and the peripheral. In fact, the advertiser and the peripheral are independent of each other, that is, a device can only act as an advertiser and does not support the peripheral role.

Figure 11: GAP role

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325135942&siteId=291194637