對 Wear OS 應用程式進行偵錯
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要對實體手錶上的 Wear OS 應用程式偵錯,請使用 Wi-Fi 或藍牙將開發機器連線至手錶。如果手錶有 USB 連接埠,也可以透過 USB 連線。
如果應用程式需要支援跨多部裝置運作的工作流程,例如在手錶和手機上運作,您也可以測試這類支援功能。如要進一步瞭解如何設定連線及進行測試,請參閱「將手錶連線至手機」。
注意:如果沒有實體手錶,也可以在 Android Studio 的模擬器上測試及執行應用程式。
升級至最新版 Wear OS
為確認應用程式可在最新版軟體上正常運作,請檢查測試裝置是否搭載裝置支援的最新版 Wear OS。
如要在測試裝置上檢查是否有系統更新,請完成下列步驟:
- 將裝置連上 Wi-Fi 網路。
- 開始為裝置充電。
- 等待裝置充電至 50% 以上。
-
在測試裝置上,依序前往「設定」>「系統」>「系統更新」。
如果有可用的系統更新,會下載並安裝到測試裝置上。
安裝 Wear OS 特定版本 (選用)
如果應用程式的測試案例需要使用 Wear OS 特定版本,您可以將軟體映像檔刷新至支援 USB 數據連線的手錶上。適用對象
例如
原廠映像檔
或
完整 OTA 映像檔
並安裝在 Google Pixel Watch 3 或 Google Pixel Watch 2 裝置上。
在手錶上啟用開發人員選項
在手錶上偵錯前,請先啟用開發人員選項。這個步驟只需要執行一次。開發人員選項會保持啟用狀態,直到您停用為止。如要啟用開發人員選項,請按照下列步驟操作:
- 開啟手錶的「設定」。
- 依序輕觸「系統」>「關於」>「版本」。
- 輕觸版本號碼七下。
- 系統會顯示訊息,確認開發人員選項已啟用。
透過 Wi-Fi 連線偵錯
開始之前,請按照上文在手錶上啟用開發人員選項。
將手錶連上 Wi-Fi 網路
- 開啟手錶的「設定」。
- 輕觸「連線」>Wi-Fi。
- 選擇網路,並視需要輸入密碼。
注意:手錶和開發機器必須連上相同的網路。並非所有存取點都適合。您可能需要使用已正確將防火牆設為支援 adb
的存取點。
啟用 Wi-Fi 偵錯功能
- 開啟手錶的「設定」。
- 依序輕觸「開發人員選項」>「透過 Wi-Fi 連線進行偵錯」。
- 稍候片刻,螢幕畫面會顯示手錶的 IP 位址,例如
192.168.1.100
。請記下此資訊,下個步驟會用到。
將偵錯工具與手錶配對
- 在 Wear OS 裝置上,找到系統中的無線偵錯選項
可以管理叢集設定,像是節點
資源調度、安全性和其他預先設定項目選取「配對新裝置」。
- 記下 Wi-Fi 配對碼、IP 位址和通訊埠。
-
在開發機器的終端機視窗中輸入以下內容
指令:
adb pair IP_ADDRESS:PORT
-
當系統提示時,輸入步驟 2 的 Wi-Fi 配對碼。終端機會輸出配對是否成功。舉例來說:
Enter pairing code: 123456
Successfully paired to 192.168.1.100:5555
配對完成後,主機電腦會顯示在 Wear OS 裝置的「無線偵錯」
畫面
「已配對的裝置」
清單中。
將偵錯工具連線至手錶
- 將手錶和開發機器連上相同的網路。
使用手錶的 IP 位址和通訊埠號碼,將偵錯工具連上手錶。舉例來說,如果 IP 位址為 192.168.1.100
,通訊埠號碼為 5555
,則 adb connect
指令及其回應如下所示:
adb connect 192.168.1.100:5555
connected to 192.168.1.100:5555
手錶現已連線至偵錯工具,您隨時可以開始偵錯。
請使用 -s
旗標將 adb
指令傳送至手錶,並指定手錶的 IP 位址和通訊埠號碼:
adb -s 192.168.1.100:5555 <command>
如果您並非使用模擬器,且只有一部連線裝置要偵錯,則不需指定位址:
adb <command>
透過藍牙偵錯
注意:藍牙偵錯功能僅適用於搭載 Wear OS 2 的 Android 配對手錶。
開始前,請先配對智慧手錶和手機,然後依照上文在手錶上啟用開發人員選項。接著開啟手機的「設定」選單,然後尋找「開發人員選項」,檢查手機是否已啟用開發人員選項。如有需要,請在手機上啟用開發人員選項。
在手機上啟用 USB 偵錯功能
- 開啟手機的「設定」選單。
- 選取「開發人員選項」,並啟用「USB 偵錯」。
在手錶上啟用 ADB 或藍牙偵錯功能
- 開啟手錶的「設定」選單。
- 捲動至「開發人員選項」。
- 確認已啟用「ADB 偵錯」。
- 啟用「透過藍牙偵錯」。
在手機上啟用藍牙偵錯功能
- 在手機上開啟 Wear OS 隨附應用程式。
- 捲動至「進階設定」,輕觸即可查看「進階設定」選項。
- 啟用「透過藍牙偵錯」。選項下方會顯示下列狀態訊息:
Host: disconnected
Target: connected
此時開發機器 (主機) 並未與手錶 (目標) 通訊。您必須完成連結操作。
注意:一次只能透過一部裝置偵錯。如果您配對了多支手錶,則只有在主畫面上選取的裝置,才能啟用藍牙偵錯功能。
將偵錯工具連線至手錶
在最後一個步驟中,偵錯工具、手機和手錶全都會派上用場。
- 使用 USB 傳輸線將手機連接至開發機器。
- 在偵錯工具中執行以下兩個指令:
adb forward tcp:4444 localabstract:/adb-hub
adb connect 127.0.0.1:4444
注意:您必須使用 IP 位址 127.0.0.1。請在開發機器上使用任一可用的通訊埠,並在兩個指令中使用相同的通訊埠。在這個範例中,通訊埠是 4444。
- 輸入連線指令後,手錶會要求您確認是否允許 ADB 偵錯。
- 在手機上檢查 Wear OS 隨附應用程式顯示的狀態,如下所示:
Host: connected
Target: connected
手錶現已連線至偵錯工具,您隨時可以開始偵錯。
使用藍牙對手錶偵錯時,adb
一律會使用 IP 位址 127.0.0.1
以及您指定的通訊埠。在這個範例中,通訊埠是 4444
。所有 adb
指令都會使用下列格式:
adb -s 127.0.0.1:4444 <command>
如果您並非使用模擬器,且只有一部連線裝置要偵錯,則不需指定位址:
adb <command>
使用 Wear OS 的螢幕錄影功能
如果您是在 macOS 上開發應用程式,可以使用 GitHub 專案 (Mac 版 Android 工具) 在 Wear OS 裝置上錄製影片。
您也可以按照以下步驟,在 Wear OS 裝置上錄製影片:
- 在手錶上錄製原始影格:
adb shell screenrecord --time-limit 30 --output-format raw-frames --verbose /sdcard/video.raw
- 將原始檔案複製到開發機器:
adb pull /sdcard/video.raw video.raw
- 使用
ffmpeg
將原始檔案轉換為 MP4 格式:
ffmpeg -f rawvideo -vcodec rawvideo -s 400x400 -pix_fmt rgb24 -r 10 -i video.raw -an -c:v libx264 -pix_fmt yuv420p video.mp4
注意:如需有關下載和安裝的操作說明,請參閱 FFmpeg 網站。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],null,["# Debug a Wear OS app\n\nTo debug your Wear OS app on a physical watch, connect your development machine to the watch using\nWi-Fi or Bluetooth. Alternatively, if your watch has a USB port,\n[connect using USB](/training/wearables/apps/creating#connect-watch-wired).\n\n\nYou can also test workflows that span multiple devices, such as a watch and a\nphone, if your app requires this support. To learn more about how to set up\na connection for testing, visit\n[connect a watch to a\nphone](/training/wearables/get-started/connect-phone).\n\n**Note:** If you don't have a physical watch, you can also\ntest and [run\nyour app on an emulator](/training/wearables/get-started/creating#run-emulator) in Android Studio.\n\nUpdate to the latest version of Wear OS\n---------------------------------------\n\nTo help verify that your app behaves as expected on the latest software,\ncheck that your test device is running the most up-to-date version of Wear OS\nthat your device supports.\n\nTo check for a system update on your test device, complete these steps:\n\n1. Connect the device to a Wi-Fi network.\n2. Start charging the device.\n3. Wait for the device's battery to charge to 50% or higher.\n4. On your test device, navigate to\n **Settings \\\u003e System \\\u003e System updates**.\n\n If a system update is available, it'll be downloaded and installed onto\n your test device.\n\n### Install a specific version of Wear OS (optional)\n\nIf your app's test case depends on a specific version of Wear OS, you can\nflash a software image onto watches that support a USB data connection. For\nexample, you can flash a\n[factory image](https://developers.google.com/android/images-watch)\nor a\n[full OTA image](https://developers.google.com/android/ota-watch)\nonto a Google Pixel Watch 3 or Google Pixel Watch 2 device.\n\nEnable developer options on your watch\n--------------------------------------\n\nBefore you can debug on your watch, enable developer options.\nYou only need to do this one time. Developer options remain enabled until\nyou disable them. To enable developer options, do the following:\n\n1. Open the watch's **Settings**.\n2. Tap **System \\\u003e About \\\u003e Versions**.\n3. Tap the **Build number** item seven times.\n4. A message appears confirming that you are now a developer.\n\nDebug over Wi-Fi\n----------------\n\nBefore you begin, enable developer options on the watch as described previously.\n\n### Connect the watch to a Wi-Fi network\n\n1. Open the watch's **Settings**.\n2. Tap **Connectivity \\\u003e Wi-Fi**.\n3. Choose a network and enter its password if necessary. **Note:**\n The watch and your development machine must be connected to the same network. Not all\n access points are suitable. You may need to use an access point whose firewall is configured\n properly to support `adb`.\n\n### Enable Wi-Fi debugging\n\n1. Open the watch's **Settings**.\n2. Tap **Developer options \\\u003e Debug over Wi-Fi**.\n3. After a moment, the screen displays the watch's IP address, such as `192.168.1.100`. You need this for the next step, so make a note of it.\n\n### Pair the debugger to the watch\n\n1. On your Wear OS device, find the wireless debugging options in system settings. Select **Pair new device**.\n2. Note the **Wi-Fi pairing code** and the **IP address \\& port.**\n3. In a terminal window on your development machine, type the following command: \n\n ```bash\n adb pair IP_ADDRESS:PORT\n ```\n4. When prompted, enter the Wi-Fi pairing code from step 2. The terminal will output whether pairing was successful. As an example: \n\n ```\n Enter pairing code: 123456\n Successfully paired to 192.168.1.100:5555\n ```\n\nAfter pairing, the host computer appears in the list of **Paired devices** on your Wear OS device's **Wireless Debugging** screen.\n\n### Connect the debugger to the watch\n\n1. Connect your watch and development machine to the same network.\n2. Connect the debugger to the watch using the watch's IP address and a port\n number. For example, if the IP address is\n `192.168.1.100` and the port number is `5555`, the\n `adb connect` command and its response look like this:\n\n ```\n adb connect 192.168.1.100:5555\n connected to 192.168.1.100:5555\n ```\n\nThe watch is now connected to the debugger and you are ready to start debugging.\nSend `adb` commands\nto the watch using the `-s` flag, specifying the watch's IP address and\nthe port number: \n\n```\nadb -s 192.168.1.100:5555 \u003ccommand\u003e\n```\n\nIf you are not using the emulator and have only one device connected for debugging, you don't\nneed to specify the address: \n\n```\nadb \u003ccommand\u003e\n```\n\nDebug over Bluetooth\n--------------------\n\n**Note**: Bluetooth debugging works only for Android-paired\nwatches running Wear OS 2.\n\nBefore you\nbegin, pair the watch and phone and enable [developer options](#enable-dev-options) on\nthe watch as described previously. Check that developer options are enabled on the\nphone by opening the phone's\n**Settings** menu and looking for **Developer Options** .\nIf necessary, [enable developer\noptions](/studio/debug/dev-options#enable) on the phone.\n\n### Enable USB debugging on the phone\n\n1. Open the phone's **Settings** menu.\n2. Select **Developer Options** and enable **USB debugging**.\n\n### Enable ADB or Bluetooth debugging on the watch\n\n1. Open the watch's **Settings** menu.\n2. Scroll to **Developer Options**.\n3. Confirm that **ADB debugging** is enabled.\n4. Enable **Debug over Bluetooth**.\n\n### Enable Bluetooth debugging on the phone\n\n1. On the phone, open the Wear OS companion app.\n2. Scroll to **Advanced Settings** and tap to view the **Advanced Settings** options.\n3. Enable **Debugging over Bluetooth** . The following status message appears under the option: \n\n ```\n Host: disconnected\n Target: connected\n ```\n\nAt this point the development machine---the host---is not communicating with\nthe watch---the target. You need to complete the link.\n\n**Note**: You can only debug with one device at a time.\nIf you have multiple watches paired, Bluetooth debugging is only enabled\nwith the device selected on the main screen.\n\n### Connect the debugger to the watch\n\nIn this final step, you use everything: the debugger, the phone, and the watch.\n\n1. Connect the phone to your development machine with a USB cable.\n2. Run these two commands in the debugger: \n\n ```\n adb forward tcp:4444 localabstract:/adb-hub\n adb connect 127.0.0.1:4444\n ```\n\n **Note**: You must use the IP address 127.0.0.1. Use any available port\n on your development machine, and use the same port in both commands. In this example, the\n port is 4444.\n3. After you enter the connect command, the watch asks you to confirm that you are allowing **ADB Debugging**.\n4. On the phone, check the status display in the Wear OS companion app for the following: \n\n ```\n Host: connected\n Target: connected\n ```\n5. The watch is now connected to the debugger, and you're ready to start debugging.\n\nWhen you debug a watch using Bluetooth, `adb` always uses the IP address `127.0.0.1`\nplus the port that you assign. In this example, the port is `4444`. All `adb` commands use the\nfollowing format: \n\n```\nadb -s 127.0.0.1:4444 \u003ccommand\u003e \n```\n\nIf you are not using the emulator and have only one device connected for debugging, you don't\nneed to specify the address: \n\n```\nadb \u003ccommand\u003e\n```\n\nUse screen record for Wear OS\n-----------------------------\n\nIf you develop on macOS, you can use the GitHub project\n[Android tool for Mac](https://github.com/mortenjust/androidtool-mac/blob/master/README.md) to record a video from your Wear OS device.\n\nAlternatively, record a video from your Wear OS device using the following steps:\n\n1. Record raw frames on the watch: \n\n ```\n adb shell screenrecord --time-limit 30 --output-format raw-frames --verbose /sdcard/video.raw\n ```\n2. Copy the raw file to your development machine: \n\n ```\n adb pull /sdcard/video.raw video.raw\n ```\n3. Use `ffmpeg` to convert the raw file to MP4: \n\n ```\n ffmpeg -f rawvideo -vcodec rawvideo -s 400x400 -pix_fmt rgb24 -r 10 -i video.raw -an -c:v libx264 -pix_fmt yuv420p video.mp4\n ```\n\n **Note:**\n Refer to the [FFmpeg](https://www.ffmpeg.org/download.html) website for download and\n installation instructions.\n\n\nRefer to the following related resources:\n\n- [Android Debug Bridge (adb)](/studio/command-line/adb)\n- [Debug your app](/studio/debug)\n- [Design principles](/training/wearables/design/design-principles)\n- [Capture Wear UI screens](/training/wearables/get-started/screenshots)"]]