

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 執行低功耗藍牙測試
<a name="afr-bridgekeeper-dt-bt"></a>

本節說明如何使用 AWS IoT Device Tester for FreeRTOS 設定和執行低功耗藍牙測試。

 核心資格不需要進行藍牙測試。如果您不想使用 FreeRTOS 藍牙支援測試裝置，可以略過此設定，請務必將 device.json 中的 BLE 功能設為 `No`。

## 先決條件
<a name="dt-bt-prereq"></a>
+ 請遵循中的說明進行[微控制器電路板的第一個測試](qual-steps.md)
+ Raspberry Pi 4B 或 3B\$1。(需要執行 Raspberry Pi BLE 配套應用程式)
+ Raspberry Pi 軟體使用的 MicroSD 卡和 SD 卡的轉接卡。

 

## Raspberry Pi 設定
<a name="dt-bt-pi-setup"></a>

若要測試待測裝置的 BLE 功能 (DUT)，您必須擁有 Raspberry Pi Model 4B 或 3B\$1。

**設定您的 Raspberry Pi 以執行 BLE 測試**

1. 下載其中一個自訂 Yocto 映像，其中包含執行測試所需的軟體。
   + [ Raspberry Pi 4B 的影像](https://docs.aws.amazon.com/freertos/latest/userguide/freertos/IDTFR_BLE_RaspberryPi4B_1.0.0_2021-04-13.rpi-sd.img) 
   + [ Raspberry Pi 3B\$1 的影像](https://docs.aws.amazon.com/freertos/latest/userguide/freertos/IDTFR_BLE_RaspberryPi3Bplus_1.0.0_2021-04-13.rpi-sd.img) 
**注意**  
Yocto 映像只能用於透過 AWS IoT Device Tester for FreeRTOS 進行測試，不能用於任何其他用途。

1. 將 yocto 映像刷到 Raspberry Pi 的 SD 卡上。

   1. 使用 SD 卡片撰寫工具 (例如 [Etcher](https://www.balena.io/etcher)) 將下載的 `image-name.rpi-sd.img` 檔案刷到 SD 卡片上。由於作業系統映像較大，此步驟需要時間才能完成。從電腦退出 SD 卡，並將 microSD 卡插入 Raspberry Pi。

1. 設定您的 Raspberry Pi。

   1. 第一次啟動時，建議您將 Raspberry Pi 連接到螢幕、鍵盤和滑鼠。

   1. 將您的 Raspberry Pi 連接到 micro USB 電源。

   1. 使用預設的登入資料進行登入。使用者 ID 請輸入 **root**。密碼請輸入 **idtafr**。

   1. 使用乙太網路或 Wi-Fi 連線將 Raspberry Pi 連接到您的網路。

      1. 若要透過 Wi-Fi 連接 Raspberry Pi，請開啟 Raspberry Pi 的 `/etc/wpa_supplicant.conf`，然後將您的 Wi-Fi 登入資料新增到 `Network` 組態。

         ```
         ctrl_interface=/var/run/wpa_supplicant
         ctrl_interface_group=0
         update_config=1
         
         network={
                 scan_ssid=1
                 ssid="your-wifi-ssid"
                 psk="your-wifi-password"
                 }
         ```

      1. 執行 `ifup wlan0` 以啟動 Wi-Fi 連線。連接到 Wi-Fi 網路可能需時一分鐘。

   1. 若為乙太網路連線，請執行 `ifconfig eth0`。若為 Wi-Fi 連線，請執行 `ifconfig wlan0`。請記下在命令輸出中顯示為 `inet addr` 的 IP 地址。在此程序稍後您將需要該 IP 地址。

   1. (選用) 該測試會使用 yocto 映像預設的登入資料，透過 SSH 在 Pi Raspberry 上執行命令。如需增加安全性，我們建議您為 SSH 設定公開金鑰身分驗證並停用以密碼為基礎的 SSH。

      1. 使用 OpenSSL `ssh-keygen` 命令建立 SSH 金鑰。如果您的主機電腦上已有 SSK 金鑰對，最佳實務是建立新的金鑰對，以允許 AWS IoT Device Tester FreeRTOS 登入您的 Raspberry Pi。
**注意**  
Windows 不附帶已安裝的 SSH 用戶端。有關如何在 Windows 安裝 SSH 用戶端的詳細資訊，請參閱[下載 SSH 軟體](https://www.ssh.com/ssh/#sec-Download-client-software)。

      1. `ssh-keygen` 命令會提示您提供金鑰對的存放名稱和路徑。根據預設，該金鑰對檔案會命名為 `id_rsa` (私有金鑰) 和 `id_rsa.pub` (公有金鑰)。在 macOS 和 Linux 上，這些檔案的預設位置是 `~/.ssh/`。在 Windows 上，預設位置為 `C:\Users\user-name`。

      1. 提示您輸入金鑰字詞時，只要按 Enter 鍵即可。

      1. 若要將 SSH 金鑰新增至 Raspberry Pi AWS IoT Device Tester ，以便 FreeRTOS 可以登入裝置，請從主機電腦使用 `ssh-copy-id`命令。此命令會將您的公有金鑰新增至 Raspberry Pi 上的 `~/.ssh/authorized_keys` 檔案。

         `ssh-copy-id root@raspberry-pi-ip-address`

      1. 提示您輸入密碼時，請輸入 **idtafr**。此為 yocto 映像預設的密碼。
**注意**  
`ssh-copy-id` 命令會假設公有金鑰名稱為 `id_rsa.pub`。在 macOS 和 Linux，預設位置是 ` ~/.ssh/`。在 Windows 上，預設位置為 `C:\Users\user-name\.ssh`。如果您給公有金鑰不同的名稱，或將其存放在不同的位置中，則必須在 `ssh-copy-id` 中使用 `-i` 選項，以指定 SSH 公有金鑰的完整路徑 (例如，`ssh-copy-id -i ~/my/path/myKey.pub`)。如需有關建立 SSH 金鑰和複製公有金鑰的詳細資訊，請參閱 [SSH-COPY-ID](https://www.ssh.com/ssh/copy-id)。

      1. 若要測試公有金鑰身分驗證運作正常，請執行 `ssh -i /my/path/myKey root@raspberry-pi-device-ip`。

         如果您未被提示輸入密碼，則您的公開金鑰身分驗證運作正常。

      1. 請確認您是用公開金鑰登入您的 Raspberry Pi，然後停用密碼為基礎的 SSH。

         1. 在 Raspberry Pi 上編輯 `/etc/ssh/sshd_config` 檔案。

         1. 將 `PasswordAuthentication` 屬性設為 `no`。

         1. 儲存並關閉 `sshd_config` 檔案。

         1. 執行 `/etc/init.d/sshd reload` 以重新載入 SSH 伺服器。

   1. 建立 `resource.json` 檔案。

      1. 在您解壓縮 AWS IoT Device Tester 的目錄中，建立名為 的檔案`resource.json`。

      1. 新增有關您 Raspberry Pi 的以下資訊至檔案，再以 Raspberry Pi 的 IP 地址替換 *rasp-pi-ip-address*。

         ```
         [
             {
                 "id": "ble-test-raspberry-pi",
                 "features": [
                     {"name":"ble", "version":"4.2"}
                 ],
                 "devices": [
                     {
                         "id": "ble-test-raspberry-pi-1",
                         "connectivity": {
                             "protocol": "ssh",
                             "ip": "rasp-pi-ip-address"
                         }
                     }
                 ]
             }
         ]
         ```

      1. 如果您未選擇對 SSH 使用公有金鑰身分驗證，請將以下內容新增至 `resource.json` 檔案的 `connectivity`區段。

         ```
         "connectivity": {
             "protocol": "ssh",
             "ip": "rasp-pi-ip-address",
             "auth": {
                 "method": "password",
                 "credentials": {
                     "user": "root",
                     "password": "idtafr"
                 }
             }
         }
         ```

      1. (選用) 如果您選擇使用 SSH 的公開金鑰身分驗證，請將下列項目新增至 `resource.json` 檔案的 `connectivity` 部分。

         ```
         "connectivity": {
             "protocol": "ssh",
             "ip": "rasp-pi-ip-address",
             "auth": {
                 "method": "pki",
                 "credentials": {
                     "user": "root",
                     "privKeyPath": "location-of-private-key"
                 }
             }
         }
         ```

## FreeRTOS 裝置設定
<a name="afr-device-setup"></a>

在您的 `device.json` 檔案中將 `BLE` 功能設為 `Yes`。如果您在藍牙測試可用之前開始使用 `device.json` 檔案，則需要將 BLE 功能新增到 `features` 陣列：

```
{
    ...
    "features": [
        {
            "name": "BLE",
            "value": "Yes"
        },
    ...
}
```

## 執行 BLE 測試
<a name="running-ble-test"></a>

在您啟用 `device.json` 中的 BLE 功能後，BLE 測試會在您執行 `devicetester_[linux | mac | win_x86-64] run-suite` 時執行，且*不會指定群組 ID*。

如果您想要單獨執行 BLE 測試，您可以指定 BLE 的群組 ID：`devicetester_[linux | mac | win_x86-64] run-suite --userdata path-to-userdata/userdata.json --group-id FullBLE`。

為獲得最可靠效能，請將您的 Raspberry Pi 靠近待測裝置 (DUT)。

## 故障診斷 BLE 測試
<a name="troubleshooting-ble"></a>

確定您已遵循 [微控制器電路板的第一個測試](qual-steps.md) 中的步驟。如果 BLE 以外的測試失敗，則該問題可能並非因為 藍牙組態所導致。