# 执行低功耗蓝牙 GATT 客户端功能

Source: [https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html)

您可以使用 GATT
            submenu 选项和 `bluetoothctl` 命令执行低功耗蓝牙 GATT 客户端功能。

## 连接到远程设备

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

    connect <bt_address>Copy to clipboard

**参数**

`<bt_address>` 是远程设备的蓝牙地址。 
注释： 要获取远程设备的蓝牙地址，请运行[蓝牙低功耗 GATT 扫描](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html#bluez-perform-bluetooth-low-energy-gatt-client-functions__section_oyf_kts_lcc_navyanka_08-21-24-1425-50-130)。

**示例**

要使用 `<bt_address>` `6D:38:AF:C6:B5:62` 连接到服务器设备，请运行以下命令：

    connect 6D:38:AF:C6:B5:62Copy to clipboard

**示例输出**

[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: yesCopy to clipboard

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

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

    scan leCopy to clipboard

注释： 
                
要获取扫描结果，远程设备必须使用任何蓝牙低功耗应用程序进行公布。

**示例输出**

[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-93Copy to clipboard

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

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

    scan offCopy to clipboard

**示例输出**

[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: noCopy to clipboard

## 获取属性列表

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

    list-attributes <bt_address>Copy to clipboard

**参数**

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

**示例**

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

    list-attributes 6D:38:AF:C6:B5:62Copy to clipboard

**示例输出**

[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 HashCopy to clipboard

## 获取属性信息

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

    attribute-info <attribute/UUID>Copy to clipboard

**参数**

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

**示例**

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

    attribute-info 00002b29-0000-1000-8000-00805f9b34fbCopy to clipboard

**示例输出**

[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: 0x0205Copy to clipboard

## 选择一个属性

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

    select-attribute <attribute/UUID>Copy to clipboard

**参数**

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

**示例**

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

        select-attribute 00006677-0000-1000-8000-00805f9b34fbCopy to clipboard

    **示例输出**

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]#Copy to clipboard
- 要使用属性路径 /org/bluez/hci0/dev\_6C\_5F\_B9\_ED\_5B\_48/service0001/char0002 选择属性，请运行以下命令：

        select-attribute /org/bluez/hci0/dev_6C_5F_B9_ED_5B_48/service0001/char0002Copy to clipboard

    **示例输出**

[MyDeviceB:/service0001/char0004]# ED_5B_48/service0001/char0002
        [MyDeviceB:/service0001/char0002]#Copy to clipboard

## 读取特征值

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

1. [选择所需的属性。](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html#bluez-perform-bluetooth-low-energy-gatt-client-functions__section_orr_wsr_mcc_navyanka_08-25-24-052-26-396)
2. 通过运行以下命令读取所选属性：

        read 0Copy to clipboard

**示例输出**

[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]#Copy to clipboard

## 写入特征值

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

1. [选择所需的属性。](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html#bluez-perform-bluetooth-low-energy-gatt-client-functions__section_orr_wsr_mcc_navyanka_08-25-24-052-26-396)
2. 通过运行以下命令将值写入所选属性：

        write "<value>"Copy to clipboard

    **参数**

    `<value>` 为待写入值。

    **示例**

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

        write "0x11"Copy to clipboard

**示例输出**

[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]#Copy to clipboard

## 读取描述符值

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

1. [选择所需的属性。](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html#bluez-perform-bluetooth-low-energy-gatt-client-functions__section_orr_wsr_mcc_navyanka_08-25-24-052-26-396)
2. 通过运行以下命令读取所选属性：

        read 0Copy to clipboard

**示例输出**

[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]#Copy to clipboard

## 写入描述符值

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

1. [选择所需的属性。](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html#bluez-perform-bluetooth-low-energy-gatt-client-functions__section_orr_wsr_mcc_navyanka_08-25-24-052-26-396)
2. 通过运行以下命令将值写入所选属性：

        write "<value>"Copy to clipboard

    **参数**

    `<value>` 为待写入值。

    **示例**

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

        write "0x11"Copy to clipboard

**示例输出**

[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]#Copy to clipboard

## 读取加密的特征值

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

1. [选择所需的属性。](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html#bluez-perform-bluetooth-low-energy-gatt-client-functions__section_orr_wsr_mcc_navyanka_08-25-24-052-26-396)
2. 通过运行以下命令读取所选属性：

        read 0Copy to clipboard
3. 当提示确认读数时，输入 `yes`。

**示例输出**

[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: phoneCopy to clipboard

## 写入加密的特征值

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

1. [选择所需的属性。](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html#bluez-perform-bluetooth-low-energy-gatt-client-functions__section_orr_wsr_mcc_navyanka_08-25-24-052-26-396)
2. 通过运行以下命令将值写入所选属性：

        write "<value>"Copy to clipboard

    **参数**

    `<value>` 为待写入值。

    **示例**

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

        write "0x11"Copy to clipboard
3. 当提示确认写入时，输入 `yes`。

## 打开属性通知

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

1. [选择所需的属性。](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html#bluez-perform-bluetooth-low-energy-gatt-client-functions__section_orr_wsr_mcc_navyanka_08-25-24-052-26-396)
2. 通过运行以下命令启用属性通知：

        notify onCopy to clipboard

    如果属性发生任何更改，所有用户均会收到通知。

**示例输出**

[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                                            fCopy to clipboard

## 关闭属性通知

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

1. [选择所需的属性。](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-perform-bluetooth-low-energy-gatt-client-functions.html#bluez-perform-bluetooth-low-energy-gatt-client-functions__section_orr_wsr_mcc_navyanka_08-25-24-052-26-396)
2. 通过运行以下命令禁用该属性的通知：

        notify offCopy to clipboard

**示例输出**

[MyDeviceB:/service0028/char0029]# notify off
    [CHG] Attribute /org/bluez/hci0/dev_44_38_FB_67_B2_E2/service0028/char0029 Notifying: no
    Notify stoppedCopy to clipboard

**上一级主题：** [通用属性配置文件](https://docs.qualcomm.com/doc/80-70017-13SC/topic/bluez-gatt.html)

Last Published: Jan 23, 2025

[Previous Topic
执行低功耗蓝牙 GATT 服务器功能](https://docs.qualcomm.com/bundle/publicresource/80-70017-13SC/topics/bluez-perform-bluetooth-low-energy-gatt-server-functions.md) [Next Topic
基于 GATT 配置文件的 Human Interface Device](https://docs.qualcomm.com/bundle/publicresource/80-70017-13SC/topics/bluez-hogp.md)