低耗电蓝牙Bluetooth Low Energy

低耗电蓝牙 (LE) 是一种规范,定义高能效设备之间的发现和通信协议。Bluetooth Low Energy (LE) is a specification that defines protocols for discovery and communication between power-efficient devices. 设备的发现通过通用访问配置文件 (GAP) 协议进行。Discovery of devices is done through the Generic Access Profile (GAP) protocol. 发现之后,设备到设备之间的通信通过通用属性 (GATT) 协议进行。After discovery, device-to-device communication is done through the Generic Attribute (GATT) protocol. 本主题提供 UWP 应用中的蓝牙 LE 的快速概述。This topic provides a quick overview of Bluetooth LE in UWP apps. 若要查看有关蓝牙 LE 的更多详细信息,请参阅蓝牙核心规范版本 4.0,其中介绍了蓝牙 LE。To see more detail about Bluetooth LE, see the Bluetooth Core Specification version 4.0, where Bluetooth LE was introduced.

蓝牙 LE 角色

GATT 和 GAP 角色是在 Windows 10 版本 1703 中引入的GATT and GAP roles were introduced in Windows 10 version 1703

可以使用以下命名空间在 UWP 应用中实现 GATT 和 GAP 协议。GATT and GAP protocols can be implemented in your UWP app by using the following namespaces.

中央和外围Central and Peripheral

发现的两个主要角色称为中央和外围。The two primary roles of discovery are called Central and Peripheral. 一般情况下,Windows 在中央模式下运行,并连接到不同的外围设备。In general, Windows operates in Central mode and connects to various Peripheral devices.

属性Attributes

在 Windows 蓝牙 API 中看到的一个常见首字母缩写是通用属性 (GATT)。A common acronym you will see in the Windows Bluetooth APIs is Generic Attribute (GATT). GATT 配置文件定义两个蓝牙 LE 设备用于进行通信的数据结构和操作模式。The GATT Profile defines the structure of data and modes of operation by which two Bluetooth LE devices communicate. 属性是 GATT 的主要构建基块。The attribute is the main building block of GATT. 属性的主要类型是服务、特征和描述符。The main types of attributes are services, characteristics and descriptors. 这些属性在客户端和服务器之间以不同方式执行,因此在相关章节中讨论其交互会更加有意义。These attributes perform differently between clients and servers, so it is more useful to discuss their interaction in the relevant sections.

常用配置文件中的典型属性层次结构

心率服务采用 GATT 服务器 API 形式进行表示The heart rate service is expressed in GATT Server API form

客户端和服务器Client and Server

建立连接之后,包含数据的设备(通常是小型 IoT 传感器或可穿戴设备)称为服务器。After a connection has been established, the device that contains the data (usually a small IoT sensor or wearable) is known as the Server. 使用该数据执行功能的设备称为客户端。The device that uses that data to perform a function is known as the Client. 例如,Windows 电脑(客户端)从心率监视器(服务器)读取数据以跟踪用户是否以最佳方式进行锻炼。For example, a Windows PC (Client) reads data from a heart rate monitor (Server) to track that a user is working out optimally. 有关详细信息,请参阅 GATT 客户端GATT 服务器主题。For more information, see the GATT Client and GATT Server topics.

观察程序和发布者(信标)Watchers and Publishers (Beacons)

除了中央和外围角色之外,还有观察者和广播者角色。In addition to the Central and Peripheral roles, there are Observer and Broadcaster roles. 广播者通常称为信标,它们不通过 GATT 进行通信,因为它们使用播发数据包中提供的有限空间进行通信。Broadcasters are commonly referred to as Beacons, they don't communicate over GATT because they use the limited space provided in the Advertisement packet for communication. 同样,观察者不必建立连接来接收数据,它会扫描附近的播发。Similarly, an Observer does not have to establish a connection to receive data, it scans for nearby advertisements. 若要配置 Windows 以观察附近的播发,请使用 BluetoothLEAdvertisementWatcher 类。To configure Windows to observe nearby advertisements, use the BluetoothLEAdvertisementWatcher class. 若要广播信标有效负载,请使用 BluetoothLEAdvertisementPublisher 类。In order to broadcast beacon payloads, use the BluetoothLEAdvertisementPublisher class. 有关详细信息,请参阅播发主题。For more information, see the Advertisement topic.

另请参阅See Also