> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 执行蓝牙低功耗 GATT 服务器功能

> 您可以使用 **GATT 子菜单**选项和 ``bluetoothctl`` 命令执行蓝牙低功耗 GATT 服务器功能。蓝牙低功耗 GATT 服务器功能包括连接和断开远程设备、添加主服务以及定义特征和描述符。GATT 功能还支持添加包含服务、注册应用程序以及启动广播以使设备可被发现。

## 前提条件

开始之前，请按照 <a href="verify-gatt-functions#set-up-the-device-for-bluetooth-low-energy-gatt-functions" target="_self">为蓝牙低功耗 GATT 功能设置设备</a> 中的说明设置设备。

## 连接远程设备

要连接远程设备，请在 `bluetoothctl` 菜单中运行以下命令：

```text theme={null}
connect <bt_address>
```

**参数**

`<bt_address>` 是远程设备的蓝牙地址。

<Note>
  要获取远程设备的蓝牙地址，请运行<a href="perform-bluetooth-low-energy-gatt-client-functions#stop-bluetooth-low-energy-gatt-scan" target="_self">蓝牙低功耗 GATT 扫描</a>。
</Note>

**示例**

要连接 `<bt_address>` 为 `6D:38:AF:C6:B5:62` 的客户端，请运行以下命令：

```text theme={null}
connect 6D:38:AF:C6:B5:62
```

## 添加主服务

要向 GATT 服务器添加主服务，请在 `menu gatt` 菜单中运行以下命令：

```text theme={null}
register-service <UUID> [handle]
```

**示例**

如果服务的 UUID 为 `FF01`，句柄为 `30`，请运行以下命令：

```text theme={null}
register-service FF01 30
```

**示例输出**

```text theme={null}
[MyDeviceB:/service0001/char0008]# register-service FF01 30
[NEW] Primary Service (Handle 0x001e)
      /org/bluez/app/service0
      FF01
[/org/bluez/app/service0] Primary (yes/no): yes
```

## 添加特征

要向服务器的某个服务添加特征，请在 `menu gatt` 菜单中运行以下命令：

```text theme={null}
register-characteristic <UUID> <Flags=read,write,notify...> [handle]
```

**参数**

`<Flags>` 是特征的标志值。有关取值，请参阅 <a href="#flag-values" target="_self">标志值</a>。

**示例**

某服务的 UUID 为 `FF02`，标志为 `read,write,notify`，句柄为 `31`。要添加特征，请运行以下命令：

```text theme={null}
register-characteristic FF02 read,write,notify 31
```

**示例输出**

```text theme={null}
[MyDeviceB]# register-characteristic FF02 read,write,notify 31
[NEW] Characteristic (Handle 0x001f)
      /org/bluez/app/service0/chrc0
      FF02
<egister-characteristic FF02 read,write,notify 31[/org/bluez/app/service0/chrc0] Enter value: 20
```

## 添加描述符

要向服务器中的某个特征添加描述符，请在 `menu gatt` 菜单中运行以下命令：

```text theme={null}
register-descriptor <UUID> <Flags=read,write...> [handle]
```

**参数**

`<Flags>` 是描述符的标志值。有关取值，请参阅 <a href="#flag-values" target="_self">标志值</a>。

**示例**

某服务的 UUID 为 `FF03`，标志为 `read,write`，句柄为 `33`。要添加描述符，请运行以下命令：

```text theme={null}
register-descriptor FF03 read,write 33
```

**示例输出**

```text theme={null}
[MyDeviceB]# register-descriptor FF03 read,write 33
[NEW] Descriptor (Handle 0x0021)
   /org/bluez/app/service0/chrc0/desc0
   FF03
<egister-descriptor FF03 read,write 33[/org/bluez/app/service0/chrc0/desc0] Enter value: 21
```

## 添加包含服务

开始之前，请先将目标服务添加为<a href="#add-a-primary-service" target="_self">主服务</a>。

要将一个包含服务添加到另一个服务中，请在 `menu gatt` 菜单中运行以下命令：

```text theme={null}
register-includes <UUID> <UUID>
```

**示例**

假设有两个主服务，UUID 分别为 `FF01` 和 `1112`。要将服务 `1112` 作为包含服务添加到服务 `FF01`，请运行以下命令：

```text theme={null}
register-includes FF01 1112
```

**示例输出**

```text theme={null}
[MyDeviceB]# register-includes FF01 1112
[NEW] Primary Service (Handle 0x001e)
      /org/bluez/app/service0
      FF01
[NEW] Primary Included Service (Handle 0x0000)
      /org/bluez/app/service1
      1112
      Unknown
```

## 注册应用程序

要发布服务器上可用或已添加的服务，请在 `menu gatt` 菜单中运行以下命令：

```text theme={null}
register-application [UUID]
```

**示例**

服务的 UUID 为 `FF01`。要发布服务器上可用或已添加的服务，请运行以下命令：

```text theme={null}
register-application FF01
```

**示例输出**

```text theme={null}
[MyDeviceB]# register-application FF01
[CHG] Secondary Service (Handle 0x0015)
   /org/bluez/app/service2
   1112
   Unknown
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000111f-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000180a-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001112-0000-1000-8000-00805f9b34fb
[CHG] Primary Service (Handle 0x0016)
   /org/bluez/app/service1    
   FF01
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000111f-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000180a-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000ff01-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001112-0000-1000-8000-00805f9b34fb
Application registered
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000111f-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000180a-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000ff01-0000-1000-8000-00805f9b34fb
[CHG] Controller 22:22:9B:2C:79:1E UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
```

## 启动广播

要启动 GATT 广播，请在 `bluetoothctl` 菜单中运行以下命令：

```text theme={null}
advertise on
```

**示例输出**

```text theme={null}
[MyDeviceB]# advertise on
[CHG] Controller 22:22:9B:2C:79:1E SupportedInstances: 0x0f (15)
[CHG] Controller 22:22:9B:2C:79:1E ActiveInstances: 0x01 (1)
Advertising object registered
Tx Power: off
Name: off
Appearance: off
Discoverable: on
```

## 断开远程设备

要断开远程设备，请在 `bluetoothctl` 菜单中运行以下命令：

```text theme={null}
disconnect <bt_address>
```

**参数**

`<bt_address>` 是远程设备的蓝牙地址。

**示例**

要断开 `<bt_address>` 为 `6D:38:AF:C6:B5:62` 的客户端，请运行以下命令：

```text theme={null}
disconnect 6D:38:AF:C6:B5:62
```

## 标志值

`<Flags>` 的取值可以是：

| `broadcast`              | `authenticated-signed-writes` | `encrypt-authenticated-read`  | `encrypt-authenticated-notify`   |
| ------------------------ | ----------------------------- | ----------------------------- | -------------------------------- |
| `read`                   | `extended-properties`         | `encrypt-authenticated-write` | `secure-notify`                  |
| `write`                  | `reliable-write`              | `secure-read`                 | `encrypt-indicate`               |
| `write-without-response` | `writable-auxiliaries`        | `secure-write`                | `encrypt-authenticated-indicate` |
| `notify`                 | `encrypt-read`                | `authorize`                   | `secure-indicate`                |
| `indicate`               | `encrypt-write`               | `encrypt-notify`              |                                  |
