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

您可以使用 `gattstest_menu` 选项执行蓝牙低功耗 GATT 服务器功能。

准备工作：

1. 在命令提示符下配置低功耗蓝牙 GATT 服务器，如下所示：

    1. 通过运行以下命令拉取服务器配置文件 `ServerConfigFile.txt`：

scp -r root@<IP_address>:/etc/bluetooth/ServerConfigFile.txt <destination_file_path>
            Copy to clipboard

        要将文件拉取到当前文件路径，请在命令中输入 `<destination_file_path>` 作为 `.`。

Note

提示输入密码时，输入 `oelinux123` 以通过安全复制协议 (SCP) 对文件传输进行身份验证。

        **示例**

        设备的 IP 地址是 `10.92.160.222`。请运行以下命令，以便从设备的 `/etc/bluetooth/` 目录拉取 `ServerConfigFile.txt` 文件：

scp -r root@10.92.160.222:/etc/bluetooth/ServerConfigFile.txt .
            Copy to clipboard

        有关服务器配置文件的更多信息，请参阅 [GATT 服务器配置参数](https://docs.qualcomm.com/doc/80-70018-13SC/topic/gatt_config_params.html#section-jsr-3lq-m1c)。
    2. 根据需要在文件中配置或添加服务器的服务、特征和描述符。
    3. 通过运行以下命令将修改后的服务器配置文件推送到设备：

scp -r ServerConfigFile.txt root@<IP_address>:/etc/bluetooth/ServerConfigFile.txt
            Copy to clipboard

        **示例**

        设备的 IP 地址是 `10.92.160.222`。请运行以下命令，以便将 `ServerConfigFile.txt` 文件推送回设备：

scp -r ServerConfigFile.txt root@10.92.160.222:/etc/bluetooth/ServerConfigFile.txt
            Copy to clipboard

        此命令可确保新添加或修改的参数反映在设备中。

2. 在命令窗口下配置低功耗蓝牙 GATT 广播，如下所示：

    1. 通过运行以下命令拉取广播客户配置文件 `AdvertiserConfigFile.txt`：

scp -r root@<IP_address>:/etc/bluetooth/AdvertiserConfigFile.txt <destination_file_path>
            Copy to clipboard

        要将文件拉取到当前文件路径，请在命令中输入 `<destination_file_path>` 作为 `.`。

        **示例**

        设备的 IP 地址是 `10.92.160.222`。请运行以下命令，以便从设备的 `/etc/bluetooth/` 目录拉取 `AdvertiserConfigFile.txt` 文件：

scp -r root@10.92.160.222:/etc/bluetooth/AdvertiserConfigFile.txt .
            Copy to clipboard

        有关广播者配置文件的更多信息，请参阅 [GATT 服务器配置参数](https://docs.qualcomm.com/doc/80-70018-13SC/topic/gatt_config_params.html#section-jsr-3lq-m1c)。
    2. 根据需要配置文件中的广播参数。
    3. 通过运行以下命令，将修改后的广播者配置文件推送到设备：

scp -r AdvertiserConfigFile.txt root@<IP_address>:/etc/bluetooth/AdvertiserConfigFile.txt
            Copy to clipboard

        **示例**

        设备的 IP 地址是 `10.92.160.222`。请运行以下命令，以便将 `AdvertiserConfigFile.txt` 文件推送回设备：

scp -r AdvertiserConfigFile.txt root@10.92.160.222:/etc/bluetooth/AdvertiserConfigFile.txt
            Copy to clipboard

        此命令可确保修改后的参数反映在设备中。
3. 设置设备并转到 GATT 服务器菜单，如[设置设备以使用低功耗蓝牙 GATT 功能](https://docs.qualcomm.com/doc/80-70018-13SC/topic/set_up_ble_gatt.html#context-o3z-5dt-s1c)中所述。

## 启动低功耗蓝牙 GATT 服务器

要启动低功耗蓝牙 GATT 服务器，执行以下操作：

Note

确保配置了[服务器配置文件](https://docs.qualcomm.com/doc/80-70018-13SC/topic/perform_ble_gatt_server_functions.html#step-gsp-htz-jbc-navyanka-05-14-24-1050-31-902)和[广播者配置文件](https://docs.qualcomm.com/doc/80-70018-13SC/topic/perform_ble_gatt_server_functions.html#step-jlx-hvz-jbc-navyanka-05-14-24-1105-51-83)。

1. 通过运行以下命令读取 `ServerConfigFile.txt` 文件：

gattstest_init_server
        Copy to clipboard

    **示例输出**

gattstest_menu
        
        ***************** Menu *******************
                 gattstest_init_server (only for Init time)
                 gattstest_addservers
                 gattstest_addservices<space><server instance><space><service instance>
                 gattstest_init_advertiser initialzes advertiser
                 gattstest_start_advertiser<space><server instance>
                 gattstest_readphy<space><remote address><server instance>
                 gattstest_set_preferred_phy<space><remote address><space><server instance><space><tx phy><space><rx phy><space><phy opt>
                 gattstest_stop<space><server_instance>
                 gattstest_disable
                 gattstest_cancel_connection<space><remote address>
                 gattstest_unregister_server<space><server instance>
                 main_menu
        ******************************************
        gattstest_init_server
        ENABLE GATTSTEST
        Initializing Gattstest
        Reading Server Configuration File ....
        File reading Done
        Copy to clipboard
2. 通过运行以下命令添加服务器实例：

gattstest_addservers
        Copy to clipboard
3. 通过运行以下命令向此服务器实例添加服务：

gattstest_addservices <server instance> <service instance>
        Copy to clipboard

    在此命令中，服务器实例后面的是必须为其添加的服务实例。服务编号在 `ServerConfigFile.txt` 中提供。有关更多信息，请参阅 [GATT 服务器配置参数](https://docs.qualcomm.com/doc/80-70018-13SC/topic/gatt_config_params.html#section-jsr-3lq-m1c)。

    **示例**

    要将 `ServerConfigFile.txt` 文件中记录的服务 `1` 添加到新创建的服务器 `1`，请运行以下命令：

gattstest_addservices 1 1
        Copy to clipboard

    **示例输出**

gattstest_addservers
        Adding Server 1
        gattstest_addservices 1 1
        AddServices
        Copy to clipboard
4. 通过运行以下命令初始化 GATT 广播：

gattstest_init_advertiser
        Copy to clipboard
5. 通过运行以下命令来宣传已添加和配置的服务器：

gattstest_start_advertiser <server instance>
        Copy to clipboard

    **示例**

    要通告已添加和配置的服务器实例 `1`，请运行以下命令：

gattstest_start_advertiser 1
        Copy to clipboard
6. 设备开始广播后，从另一台设备上的低功耗蓝牙应用程序连接它。

    **示例输出**

gattstest_start_advertiser 1
        StartAdvertisement
        onAdvertisingSetStarted - Success
        The device 22:22:85:3d:2e:50 got connected
        ACL state:0 change with reason 00 for device: 22:22:85:3d:2e:50
        Copy to clipboard

## 读取和设置 PHY 参数

要读取和设置 PHY 参数，执行以下操作：

1. 通过运行以下命令读取当前连接的 PHY 参数：

gattstest_readphy <remote address> <server instance>
        Copy to clipboard

    **示例**

    服务器实例 `1` 被发布给地址为 `22:22:85:3d:2e:50` 的远程设备。要读取此连接的 PHY 参数，运行以下命令：

gattstest_readphy 22:22:85:3d:2e:50 1
        Copy to clipboard

    **示例输出**

gattstest_readphy 22:22:85:3d:2e:50 1
        Read Phy
        User input is 22:22:85:3d:2e:50
        
        onPhyRead deviceAddress: 22:22:85:3d:2e:50, txPhy: 1 rxPhy: 1 status: 0
        Copy to clipboard
2. 通过运行以下命令设置当前连接的首选 PHY：

gattstest_set_preferred_phy <remote address> <server instance> <tx Phy> <rx Phy> <Phy option>
        Copy to clipboard

    有关 PHY 参数的更多信息，请参阅 [GATT 服务器配置参数](https://docs.qualcomm.com/doc/80-70018-13SC/topic/gatt_config_params.html#section-jsr-3lq-m1c)。

    **示例**

    地址为 `22:22:85:3d:2e:50` 的远程设备已连接至服务器实例 `1`。请运行以下命令，以便将此连接的 Tx PHY 设置为 `1 M`，Rx PHY 设置为 `1 M`，PHY 选项设置为 `0`：

gattstest_set_preferred_phy 22:22:85:3d:2e:50 1 1 1 0
        Copy to clipboard

    **示例输出**

gattstest_set_preferred_phy 22:22:85:3d:2e:50 1 1 1 0
        Set Preferred Phy
        the user options are address: 22:22:85:3d:2e:50 server_instance: 1 txoption: 1 rxoption: 1 phyoption: 0
        Copy to clipboard

## 断开远程设备的连接

要断远程端设备的连接，可运行以下命令：

gattstest_cancel_connection <remote address>
    Copy to clipboard

**示例**

请运行以下命令，以便断开地址为 `22:22:85:3d:2e:50` 的远程设备：

gattstest_cancel_connection 22:22:85:3d:2e:50
    Copy to clipboard

**示例输出**

gattstest_cancel_connection 22:22:85:3d:2e:50
    Cancel Connection
    The device 22:22:85:3d:2e:50 got disconnected
    ACL state:1 change with reason 16 for device: 22:22:85:3d:2e:50
    Copy to clipboard

## 停止低功耗蓝牙 GATT 服务器

要停止广播低功耗蓝牙 GATT 服务器实例，运行以下命令：

gattstest_stop <server_instance>
    Copy to clipboard

**示例**

请运行以下命令，以便停止服务器实例 `1`：

gattstest_stop 1
    Copy to clipboard

Last Published: Apr 30, 2025

[Previous Topic
通用属性 Profile (General Attribute Profile)](https://docs.qualcomm.com/bundle/publicresource/80-70018-13SC/topics/set_up_ble_gatt.md) [Next Topic
执行低功耗蓝牙 GATT 客户端功能](https://docs.qualcomm.com/bundle/publicresource/80-70018-13SC/topics/perform_ble_gatt_client_functions.md)