> ## 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 扫描。您可以检索属性列表、获取属性信息并选择特定属性。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="#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
```

**示例输出**

```text theme={null}
[bluetooth]# connect 6D:38:AF:C6:B5:62
Attempting to connect to 6D:38:AF:C6:B5:62
[CHG] Device 6D:38:AF:C6:B5:62 Connected: yes
Connection successful
[NEW] Primary Service (Handle 0x0000)
      /org/bluez/hci0/dev_6D_38_AF_C6_B5_62/service0001
      00001801-0000-1000-8000-00805f9b34fb
      Generic Attribute Profile
[NEW] Characteristic (Handle 0x0000)
      /org/bluez/hci0/dev_6D_38_AF_C6_B5_62/service0001/char0002
      00002a05-0000-1000-8000-00805f9b34fb
      Service Changed
[NEW] Characteristic (Handle 0x0000)
      /org/bluez/hci0/dev_6D_38_AF_C6_B5_62/service0001/char0004
      00002b3a-0000-1000-8000-00805f9b34fb
      Server Supported Features
[NEW] Characteristic (Handle 0x0000)
      /org/bluez/hci0/dev_6D_38_AF_C6_B5_62/service0001/char0006
      00002b29-0000-1000-8000-00805f9b34fb
      Client Supported Features
[NEW] Characteristic (Handle 0x0000)
      /org/bluez/hci0/dev_6D_38_AF_C6_B5_62/service0001/char0008
      00002b2a-0000-1000-8000-00805f9b34fb
      Database Hash
[CHG] Device 6D:38:AF:C6:B5:62 UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device 6D:38:AF:C6:B5:62 UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device 6D:38:AF:C6:B5:62 ServicesResolved: yes
```

## 启动蓝牙低功耗 GATT 扫描

要启动蓝牙低功耗 GATT 扫描，请在 `bluetoothctl` 菜单中运行以下命令：

```text theme={null}
scan le
```

<Note>
  要获取扫描结果，远程设备必须使用任意蓝牙低功耗应用进行广播。
</Note>

**示例输出**

```text theme={null}
[MyDeviceB]# scan le
Discovery started
[CHG] Controller 22:22:9B:2C:79:1E Discovering: yes
[CHG] Device F8:7D:76:9D:9B:6B RSSI: -70
[NEW] Device A4:30:7A:EE:AF:EF [TV] MyDeviceA (43)
[NEW] Device 52:2F:07:6F:AA:93 52-2F-07-6F-AA-93
[CHG] Device A4:30:7A:EE:AF:EF ManufacturerData Key: 0x0075
[CHG] Device A4:30:7A:EE:AF:EF ManufacturerData Value:
42 04 01 01 e7 a4 30 7a ee af ef a6 30 7a ee af B..... 0z....0z....
ee 01 3b 00 00 00 00 00                          ......
[CHG] Device A4:30:7A:EE:AF:EF Modalias: bluetooth:v04E8p8080d0000
[CHG] Device A4:30:7A:EE:AF:EF UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Device A4:30:7A:EE:AF:EF UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device A4:30:7A:EE:AF:EF UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device A4:30:7A:EE:AF:EF UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device A4:30:7A:EE:AF:EF UUIDs: 00001112-0000-1000-8000-00805f9b34fb
[CHG] Device A4:30:7A:EE:AF:EF UUIDs: 0000111f-0000-1000-8000-00805f9b34fb
[CHG] Device A4:30:7A:EE:AF:EF UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device A4:30:7A:EE:AF:EF ManufacturerData Key: 0x0075
[CHG] Device A4:30:7A:EE:AF:EF ManufacturerData Value:
42 04 01 01 e7 a4 30 7a ee af ef a6 30 7a ee af B..... 0z....0z....
ee 01 3b 00 00 00 00 00                          ......
[CHG] Device A4:30:7A:EE:AF:EF ManufacturerData Key: 0xff19
[CHG] Device A4:30:7A:EE:AF:EF ManufacturerData Value:
00 75 00 09 01 00 00 00 06 01 00 00 00 00 00 00 .u..................
00 00 00 00 00 00 00 00                         ................
[CHG] Device F8:7D:76:9D:9B:6B RSSI: -60
[CHG] Device A4:30:7A:EE:AF:EF ManufacturerData Key: 0x0075
[CHG] Device A4:30:7A:EE:AF:EF ManufacturerData Value:
42 04 01 20 e7 20 0d 00 02 01 2b 01 01 00 01 00 B .. ..... .......
00 00 00 00 00 00 00 00                        ........
[CHG] Device A4:30:7A:EE:AF:EF ManufacturerData Key: 0xff19
[CHG] Device A4:30:7A:EE:AF:EF ManufacturerData Value:
00 75 00 09 01 00 00 00 06 01 00 00 00 00 00 00  .u.............
00 00 00 00 00 00 00 00                            .......
[NEW] Device 4A:04:87:DF:CB:35 4A-04-87-DF-CB-35
[DEL] Device 52:2F:07:6F:AA:93 52-2F-07-6F-AA-93
```

## 停止蓝牙低功耗 GATT 扫描

要停止蓝牙低功耗 GATT 扫描，请在 `bluetoothctl` 菜单中运行以下命令：

```text theme={null}
scan off
```

**示例输出**

```text theme={null}
[MyDeviceB]# scan off
Discovery stopped
[CHG] Device A4:30:7A:EE:AF:EF RSSI is nil
[CHG] Device F8:7D:76:9D:9B:6B RSSI is nil
[CHG] Controller 22:22:9B:2C:79:1E Discovering: no
```

## 获取属性列表

要获取远程设备的属性列表，请在 `menu gatt` 菜单中运行以下命令：

```text theme={null}
list-attributes <bt_address>
```

**参数**

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

**示例**

要列出地址为 `6D:38:AF:C6:B5:62` 的远程设备的属性，请运行以下命令：

```text theme={null}
list-attributes 6D:38:AF:C6:B5:62
```

**示例输出**

```text theme={null}
[MyDeviceB]# list-attributes 6D:38:AF:C6:B5:62
Primary Service (Handle 0x0000)
      /org/bluez/hci0/dev_6D_38_AF_C6_B5_62/service0001
      00001801-0000-1000-8000-00805f9b34fb
      Generic Attribute Profile
Characteristic (Handle 0x0000)
      /org/bluez/hci0/dev_6D_38_AF_C6_B5_62/service0001/char0002
      00002a05-0000-1000-8000-00805f9b34fb
      Service Changed
Characteristic (Handle 0x0000)
      /org/bluez/hci0/dev_6D_38_AF_C6_B5_62/service0001/char0004
      00002b3a-0000-1000-8000-00805f9b34fb
      Server Supported Features
Characteristic (Handle 0x0000)
      /org/bluez/hci0/dev_6D_38_AF_C6_B5_62/service0001/char0006
      00002b29-0000-1000-8000-00805f9b34fb
      Client Supported Features
Characteristic (Handle 0x0000)
      /org/bluez/hci0/dev_6D_38_AF_C6_B5_62/service0001/char0008
      00002b2a-0000-1000-8000-00805f9b34fb
      Database Hash
```

## 获取属性信息

要获取某个属性的信息，请在 `menu gatt` 菜单中运行以下命令：

```text theme={null}
attribute-info <attribute/UUID>
```

**参数**

`<attribute/UUID>` 是 UUID 或属性路径。

**示例**

要获取 UUID 为 `00002b29-0000-1000-8000-00805f9b34fb` 的属性信息，请运行以下命令：

```text theme={null}
attribute-info 00002b29-0000-1000-8000-00805f9b34fb
```

**示例输出**

```text theme={null}
[MyDeviceB]# attribute-info 00002b29-0000-1000-8000-00805f9b34fb
Characteristic - Client Supported Features
      UUID: 00002b29-0000-1000-8000-00805f9b34fb
      Service: /org/bluez/hci0/dev_73_F4_3F_FA_0A_DF/service0001
      Flags: read
      Flags: write
      MTU: 0x0205
```

## 选择属性

要使用 UUID 或路径选择属性，请在 `menu gatt` 菜单中运行以下命令：

```text theme={null}
select-attribute <attribute/UUID>
```

**参数**

`<attribute/UUID>` 是属性的 UUID 或路径。

**示例**

* 要使用 UUID `00006677-0000-1000-8000-00805f9b34fb` 选择属性，请运行以下命令：

  ```text theme={null}
  select-attribute 00006677-0000-1000-8000-00805f9b34fb
  ```

  **示例输出**

  ```text theme={null}
  Characteristic User Description
  [MyDeviceB]# select-attribute 00006677-0000-1000-8000-00805f9b34fb
  <lect-attribute 00006677-0000-1000-8000-00805f9b34fb[MyDeviceB:/service0028/char0029/desc002c]#
  [MyDeviceB:/service0028/char0029/desc002c]#
  ```

* 要使用属性路径 `/org/bluez/hci0/dev_6C_5F_B9_ED_5B_48/service0001/char0002` 选择属性，请运行以下命令：

  ```text theme={null}
  select-attribute /org/bluez/hci0/dev_6C_5F_B9_ED_5B_48/service0001/char0002
  ```

  **示例输出**

  ```text theme={null}
  [MyDeviceB:/service0001/char0004]# ED_5B_48/service0001/char0002
  [MyDeviceB:/service0001/char0002]#
  ```

## 读取特征值

要读取特征值，请在 `menu gatt` 菜单中执行以下操作：

1. <a href="#select-an-attribute" target="_self">选择目标属性。</a>

2. 运行以下命令读取所选属性：

   ```text theme={null}
   read 0
   ```

**示例输出**

```text theme={null}
[MyDeviceB:/service0028/char0029]# read 0
Attempting to read /org/bluez/hci0/dev_64_8C_C7_03_C4_B0/service0028/char0029
[CHG] Attribute /org/bluez/hci0/dev_64_8C_C7_03_C4_B0/service0028/char0029 Value:
   11
   11
[MyDeviceB:/service0028/char0029]#
```

## 写入特征值

要写入特征值，请在 `menu gatt` 菜单中执行以下操作：

1. <a href="#select-an-attribute" target="_self">选择目标属性。</a>

2. 运行以下命令向所选属性写入值：

   ```text theme={null}
   write "<value>"
   ```

   **参数**

   `<value>` 是您要写入的值。

   **示例**

   要将属性值写为 `0x11`，请运行以下命令：

   ```text theme={null}
   write "0x11"
   ```

**示例输出**

```text theme={null}
[MyDeviceB:/service0028/char0029]# read 0b
Attempting to read /org/bluez/hci0/dev_64_8C_C7_03_C4_B0/service0028/char0029
[CHG] Attribute /org/bluez/hci0/dev_64_8C_C7_03_C4_B0/service0028/char0029 Value:
   22
   22
[MyDeviceB:/service0028/char0029]# write "0x11"
Attempting to write /org/bluez/hci0/dev_64_8C_C7_03_C4_B0/service0028/char0029
[MyDeviceB:/service0028/char0029]# read 0
Attempting to read /org/bluez/hci0/dev_64_8C_C7_03_C4_B0/service0028/char0029
[CHG] Attribute /org/bluez/hci0/dev_64_8C_C7_03_C4_B0/service0028/char0029 Value:
   11    
   11
[MyDeviceB:/service0028/char0029]#
```

## 读取描述符值

要读取描述符值，请在 `menu gatt` 菜单中执行以下操作：

1. <a href="#select-an-attribute" target="_self">选择目标属性。</a>

2. 运行以下命令读取所选属性：

   ```text theme={null}
   read 0
   ```

**示例输出**

```text theme={null}
[MyDeviceB]# select-attribute 00006677-0000-1000-8000-00805f9b34fb
<lect-attribute 00006677-0000-1000-8000-00805f9b34fb[MyDeviceB:/service0028/char0029/desc002c]#
[MyDeviceB:/service0028/char0029/desc002c]#
[MyDeviceB:/service0028/char0029/desc002c]# read 0
Attempting to read /org/bluez/hci0/dev_55_B1_D2_67_7C_CD/service0028/char0029/desc002c
[CHG] Attribute /org/bluez/hci0/dev_55_B1_D2_67_7C_CD/service0028/char0029/desc002c Value:
   00
   00
[MyDeviceB:/service0028/char0029/desc002c]# write "0x11"
Attempting to write /org/bluez/hci0/dev_55_B1_D2_67_7C_CD/service0028/char0029/desc002c
[MyDeviceB:/service0028/char0829/desc002c]# read 0
Attempting to read /org/bluez/hci0/dev_55_B1_D2_67_7C_CD/service0028/char0029/desc002c
[CHG] Attribute /org/bluez/hci0/dev_55_B1_D2_67_7C_CD/service0028/char0029/desc002c Value:
   11
   11
[MyDeviceB:/service0028/char0029/desc002c]#
```

## 写入描述符值

要写入描述符值，请在 `menu gatt` 菜单中执行以下操作：

1. <a href="#select-an-attribute" target="_self">选择目标属性。</a>

2. 运行以下命令向所选属性写入值：

   ```text theme={null}
   write "<value>"
   ```

   **参数**

   `<value>` 是您要写入的值。

   **示例**

   要将属性值写为 `0x11`，请运行以下命令：

   ```text theme={null}
   write "0x11"
   ```

**示例输出**

```text theme={null}
[MyDeviceB]# select-attribute 00006677-0000-1000-8000-00805f9b34fb
<lect-attribute 00006677-0000-1000-8000-00805f9b34fb[MyDeviceB:/service0028/char0029/desc002c]#
[MyDeviceB:/service0028/char0029/desc002c]#
[MyDeviceB:/service0028/char0029/desc002c]# read 0
Attempting to read /org/bluez/hci0/dev_55_B1_D2_67_7C_CD/service0028/char0029/desc002c
[CHG] Attribute /org/bluez/hci0/dev_55_B1_D2_67_7C_CD/service0028/char0029/desc002c Value:
   00
   00
[MyDeviceB:/service0028/char0029/desc002c]# write "0x11"
Attempting to write /org/bluez/hci0/dev_55_B1_D2_67_7C_CD/service0028/char0029/desc002c
[MyDeviceB:/service0028/char0829/desc002c]# read 0
Attempting to read /org/bluez/hci0/dev_55_B1_D2_67_7C_CD/service0028/char0029/desc002c
[CHG] Attribute /org/bluez/hci0/dev_55_B1_D2_67_7C_CD/service0028/char0029/desc002c Value:
   11
   11
[MyDeviceB:/service0028/char0029/desc002c]#
```

## 读取加密特征值

要读取加密特征值，请在 `menu gatt` 菜单中执行以下操作：

1. <a href="#select-an-attribute" target="_self">选择目标属性。</a>

2. 运行以下命令读取所选属性：

   ```text theme={null}
   read 0
   ```

3. 出现确认读取的提示时，输入 `yes`。

**示例输出**

```text theme={null}
[MyDeviceB:/service0028/char0029]# read 0
Attempting to read /org/bluez/hci0/dev_4F_A4_EC_0D_D2_F2/service0028/char0029
[DEL] Device 60:A3:37:5A:35:5F 60-A3-37-5A-35-5F
[NEW] Device 60:A3:37:5A:35:5F 60-A3-37-5A-35-5F
Request confirmation
[agent] Confirm passkey 161326 (yes/no): yes
[CHG] Attribute /org/bluez/hci0/dev_4F_A4_EC_0D_D2_F2/service0028/char0029 Value:
   00
[CHG] Device D4:8A:39:78:27:41 Address: D4:8A:39:78:27:41
[CHG] Device D4:8A:39:78:27:41 AddressType: public
   00
[CHG] Device D4:8A:39:78:27:41 Bonded: yes
[CHG] Device D4:8A:39:78:27:41 Paired: yes
[CHG] Device D4:8A:39:78:27:41 Class: 0x005a020c
[CHG] Device D4:8A:39:78:27:41 Icon: phone
```

## 写入加密特征值

要写入加密特征值，请在 `menu gatt` 菜单中执行以下操作：

1. <a href="#select-an-attribute" target="_self">选择目标属性。</a>

2. 运行以下命令向所选属性写入值：

   ```text theme={null}
   write "<value>"
   ```

   **参数**

   `<value>` 是您要写入的值。

   **示例**

   要将属性值写为 `0x11`，请运行以下命令：

   ```text theme={null}
   write "0x11"
   ```

3. 出现确认写入的提示时，输入 `yes`。

## 为属性打开通知

要为某个属性打开通知，请在 `menu gatt` 菜单中执行以下操作：

1. <a href="#select-an-attribute" target="_self">选择目标属性。</a>

2. 运行以下命令为该属性启用通知：

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

   属性发生任何更改时，您都会收到通知。

**示例输出**

```text theme={null}
[MyDeviceB:/service0028/char0029]# notify on
[CHG] Attribute /org/bluez/hci0/dev_44_38_FB_67_B2_E2/service0028/char0029 Notifying: yes
Notify started
[CHG] Attribute /org/bluez/hci0/dev_44_38_FB_67_B2_E2/service0028/char0029 Value:
   55                                            U
[CHG] Attribute /org/bluez/hci0/dev_44_38_FB_67_B2_E2/service0028/char0029 Value:
   88                                            .
[CHG] Attribute /org/bluez/hci0/dev_44_38_FB_67_B2_E2/service0028/char0029 Value:
   55 55                                         UU
[CHG] Device D4:8A:39:78:27:41 RSSI: -74
[CHG] Attribute /org/bluez/hci0/dev_44_38_FB_67_B2_E2/service0028/char0029 Value:
   55                                            U
[CHG] Attribute /org/bluez/hci0/dev_44_38_FB_67_B2_E2/service0028/char0029 Value:
   11                                            U
[CHG] Attribute /org/bluez/hci0/dev_44_38_FB_67_B2_E2/service0028/char0029 Value:
   66                                            f
```

## 为属性关闭通知

要为某个属性关闭通知，请在 `menu gatt` 菜单中执行以下操作：

1. <a href="#select-an-attribute" target="_self">选择目标属性。</a>

2. 运行以下命令为该属性禁用通知：

   ```text theme={null}
   notify off
   ```

**示例输出**

```text theme={null}
[MyDeviceB:/service0028/char0029]# notify off
[CHG] Attribute /org/bluez/hci0/dev_44_38_FB_67_B2_E2/service0028/char0029 Notifying: no
Notify stopped
```
