BLE指示

use*_*067 1 push-notification bluetooth-lowenergy

据我了解,BLE指示是一种可靠的通信方法。您如何知道您的指示未传达。我正在为外围设备/服务器编写代码,当前,当我发送通知时,我收到了来自中央的手动响应。我读到如果使用指示,确认会自动在L2CAP层中进行,因此通信速度更快,但是我的嵌入式控制器如何知道蓝牙模块无法成功通过链路获取数据包?我们正在使用Microchip RN4030蓝牙模块。

Emi*_*mil 5

Let's make things clear.

The BLE stack looks roughly like the following. The stack has these layers in this order:

  • Link Layer
  • HCI (if controller and host are separated)
  • L2CAP
  • ATT
  • GATT
  • Application

The Link Layer is a reliable protocol in the sense that all packets are protected by a CRC and every packet is acknowledged by the receiving device. If a packet is not acknowledged, it is resent until an acknowledge is received. There can also only be one outstanding packet, which means no reordering of packets are possible. Acknowledges are normally not being presented to upper layers.

The HCI layer is the communication protocol between the controller and the host.

The L2CAP layer does almost nothing if you use the standard MTU size of 23. It has a length header and a type code ("channel") which indicates what type of packet is being sent (usually ATT).

On the ATT level, there are two types of packets that are sent from the server that are not sent as a response to a client request. Those are notifications and indications. Sending one notification or indication has the same "performance" since the type is just a tag of a packet that's sent over the lower layers. The differences are listed below:

Indications:

  • When an indication packet is sent to the client, the client must acknowledge the packet by sending a confirmation packet when it has received the indication packet. Even if the indication packet is invalid, a confirmation shall be sent back.
  • Upper layers are not involved sending back the confirmation.
  • The server may not send a new indication until it has received a confirmation from the previous one.
  • The only time you won't receive a confirmation after an indication is if the link is dropped (you should then get a disconnected event), or there is some bug in some of the BLE stacks.
  • After the app has sent an indication, most BLE stack confirms to the app that that a confirmation has been received by the client as that the indication operation has completed.

Notifications:

  • No ATT layer acknowledges are sent.
  • "Commands and notifications that are received but cannot be processed, due to buffer overflows or other reasons, shall be discarded. Therefore, those PDUs must be considered to be unreliable." However I have never noticed an implementation actually following this rule, i.e. all notifications are delivered to the application. Or I've never hit the max buffer size.

The GATT layer is mostly a definition of how the attribute protocol should be used and defines a DB structure of characteristics.

Implications

According to my opinion, there are several flaws or issues with the BLE standard. There are two things that might make indications useless and unreliable in practice:

  • There are no way to send back an error response instead of a confirmation.
  • The fact that it is the ATT layer that sends back the confirmation directly when it has received the indication, and not the app when it has successfully handled the indication.

This means that if for example, some bug or other issue causing that the BLE stack couldn't send the indication to the app, or your app crashed, or your app found your value to be invalid, there is no way your peripheral can aware of that. Since it got the confirmation it thinks everything is fine.

I can't understand why they defined indications this way. Since the app doesn't send the confirmation but a lower layer does, there seems to be no point at all in having an ATT layer acknowledge instead of just using the Link Layer acknowledge. Both are just acknowledges that the packet has been received halfway of its destination.

If we draw a parallel to a HTTP POST and internet, we could consider the Link Layer acknowledge as when the network card of the destination receives the request and the ATT confirmation as a confirmation that the TCP stack received the packet. You have no way of knowing that your web server software actually did receive your request, and it processed it with success.

The fact that notifications are allowed to be dropped by the receiver is also bad. Normally notifications are used if the peripheral wants to stream a lot of data to the central and then you don't want dropped packets. They should have designed the flow control so that the Link Layer stopped acknowledge incoming packets instead until the app are ready to process the next notifications. This is even already implemented at the LL + HCI + Host layers.

Windows

至少对于Windows BLE堆栈而言,一件有趣的事情是,如果它接收到的指示比应用程序处理指示的速度快,即使由于“缓冲区溢出或其他原因”而只允许丢弃通知,它也会开始删除指示。 。在我的测试中,它最多可缓冲512个指示。

那说

只需使用通知,如果您需要某种确认,请让客户端在收到数据并成功处理后发送一个写数据包。