蓝牙编程扫盲 基本概念

今天发现了一个贼好贼好的文章:

An Introduction to Bluetooth Programming

初步感受:

蓝牙编程和网络编程(socket)很像,网络通信需要知道主机名(ip地址)和端口,蓝牙通信需要知道蓝牙的MAC地址和端口(RFCOMM叫channel,L2CAP叫Service Multiplexers)。

蓝牙设备的MAC地址,是出厂就带的,有IEEE机构负责分配。

传输层协议

有了MAC地址和端口号,就可以通信。剩下的就是选择什么样的传输层协议了(程序员一般不关注链路层)。

蓝牙有2个传输层协议:

  • RFCOMM:类似tcp,可靠的,但效率低下

    使用channel来区分蓝牙设备里的具体service,只有30个channel(1-30)

  • L2CAP:类似udp,不可靠,但效率高

    使用protocol Service Multiplexers区分蓝牙设备里的具体service,一个有32767(1 - 32767)

对比表

协议 术语 保留/ well-known 的端口 dynamically assigned ports
TCP port 1-1024 1025-65535
UDP port 1-1024 1025-65535
RFCOMM channel none 1-30
L2CAP PSM odd numbered 1-4095 odd numbered 4097 - 32765

由于RFCOMM只有30个channel,不够用。蓝牙使用Service Discovery Protocol (SDP)来解决这个问题。

下面是摘录的SDP描述:

Instead of agreeing upon a port to use at application design time, the Bluetooth approach is to assign ports at runtime and follow a publish-subscribe model. The host machine operates a server application, called the SDP server, that uses one of the few L2CAP reserved port numbers. Other server applications are dynamically assigned port numbers at runtime and register a description of themselves and the services they provide (along with the port numbers they are assigned) with the SDP server. Client applications will then query the SDP server (using the well defined port number) on a particular machine to obtain the information they need.

This raises the question of how do clients know which description is the one they are looking for. The standard way of doing this in Bluetooth is to assign a 128-bit number, called the Universally Unique Identifier (UUID), at design time. Following a standard method [1] of choosing this number guarantees choosing a UUID unique from those chosen by anyone else following the same method. Thus, a client and server application both designed with the same UUID can provide this number to the SDP server as a search term.

As with RFCOMM and L2CAP, only a small portion of SDP has been described here - those parts most relevant to a network programmer. Among the other ways SDP can be used are to describe which transport protocols a server is using, to give information such as a human-readable description of the service provided and who is providing it, and to search on fields other than the UUID such as the service name. Another point worth mentioning is that SDP is not even required to create a Bluetooth application. It is perfectly possible to revert to the TCP/IP way of assigning port numbers at design time and hoping to avoid port conflicts, and this might often be done to save some time. In controlled settings such as the computer science laboratory, this is quite reasonable. Ultimately, however, to create a portable application that will run in the greatest number of scenarios, the application should use dynamically assigned ports and SDP.

百度翻译的中文:

蓝牙方法不是在应用程序设计时约定要使用的端口,而是在运行时分配端口并遵循发布-订阅模型。主机运行一个名为SDP server的服务器应用程序,它使用为数不多的L2CAP保留端口号之一。其他服务器应用程序在运行时动态分配端口号,并向SDP服务器注册它们自己和它们提供的服务的描述(以及它们被分配的端口号)。然后,客户机应用程序将查询特定计算机上的SDP服务器(使用定义良好的端口号),以获取所需的信息。

这就提出了一个问题:客户如何知道他们要寻找的是哪种描述。蓝牙的标准方法是在设计时分配一个128位的号码,称为通用唯一标识符(UUID)。遵循一个选择这个数字的标准方法[1],可以保证从遵循相同方法的任何其他人选择的UUID中选择一个唯一的UUID。因此,使用相同的UUID设计的客户机和服务器应用程序可以将此编号作为搜索项提供给SDP服务器。

与RFCOMM和L2CAP一样,这里只描述了SDP的一小部分-这些部分与网络程序员最相关。可以使用SDP的其他方法包括描述服务器正在使用的传输协议,提供诸如所提供的服务和谁提供的服务的可读描述之类的信息,以及搜索UUID以外的字段,例如服务名称。另一点值得一提的是,SDP甚至不需要创建蓝牙应用程序。完全可以恢复到TCP/IP的方式,在设计时分配端口号并希望避免端口冲突,这样做通常可以节省一些时间。在计算机科学实验室这样的受控环境中,这是相当合理的。然而,最终,为了创建一个可移植的应用程序,该应用程序应该使用动态分配的端口和SDP。

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

posted @ 2020-07-24 18:35  小石王  阅读(96)  评论(0编辑  收藏