自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
Wi-Fi Direct
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Wi-Fi Direct 功能 (也稱為 Wi-Fi P2P) 可讓支援的裝置在不透過網際網路或行動網路連線的情況下,直接使用 Wi-Fi Direct 通訊協定來尋找及連線。這項功能建構於 Wi-Fi Alliance (WFA) Wi-Fi Direct 規格之上,可讓信任的裝置和應用程式在離線時分享高處理量資料。
範例和來源
如要使用這項功能,裝置製造商必須實作 Wi-Fi 供應商和申請者 HAL 介面。
在 Android 13 中,Supplicant 介面會使用 AIDL 定義 HAL。在 Android 14 以上版本中,供應商 HAL 介面會使用 AIDL。在 Android 12 以下版本中,Supplicant 和供應商 HAL 介面會使用 HIDL 定義。
如要使用 Wi-Fi Direct 功能,必須使用下列 Wi-Fi HAL 途徑:
hardware/interfaces/wifi/aidl
或 hardware/interfaces/wifi/1.3
以上
hardware/interfaces/wifi/supplicant/aidl
或 hardware/interfaces/wifi/supplicant/1.2
以上
實作
裝置製造商必須同時提供架構和 HAL/韌體支援:
- 架構:
- Android 開放原始碼計畫程式碼
- 啟用 Wi-Fi Direct:需要功能旗標
- Wi-Fi Direct (P2P) HAL 支援 (這表示支援韌體)
如要實作這項功能,裝置製造商必須實作 Wi-Fi HIDL 或 AIDL 介面,並啟用 Wi-Fi Direct 的功能旗標。在 device/<oem>/<device>
中的 device.mk
中,修改 PRODUCT_COPY_FILES
環境變數,以便支援 Wi-Fi Direct 功能:
```
PRODUCT_COPY_FILES +=
frameworks/native/data/etc/android.hardware.wifi.direct.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.hardware.wifi.direct.xml
```
支援 Wi-Fi Direct 的所有其他規定都包含在 Android 開放原始碼計畫中。
MAC 隨機化
Android 要求 Wi-Fi Direct 裝置位址和介面位址必須隨機產生。這些值不得與裝置的真實 MAC 位址相同,且必須符合下列規定:
- 如果沒有儲存的永久群組,Wi-Fi Direct 裝置位址必須在建立介面時隨機產生;否則,裝置位址必須繼續使用上次產生的 MAC 位址。
- 每次建立連線時,Wi-Fi Direct 介面位址 (也稱為群組位址) 都必須隨機產生。
Wi-Fi Direct MAC 隨機產生功能是在 'wpa_supplicant' 中實作,並由 p2p_device_random_mac_addr
和 p2p_interface_random_mac_addr
兩個設定控制。
如要啟用這項功能,裝置製造商必須:
- 實作 Wi-Fi 申請者
ISupplicantP2pIface::setMacRandomization
API。視實作的介面而定,這會位於:
hardware/interface/wifi/supplicant/1.2
(適用於 HIDL)
- AIDL 的
hardware/interface/wifi/supplicant/aidl
- 在裝置自訂疊加層中將
config_wifi_p2p_mac_randomization_supported
設為「true」。
驗證
Android 提供一組單元測試、Compatibility Test Suite (CTS) 測試和 CTS Verifier 測試,用於驗證 Wi-Fi Direct 功能。您也可以使用供應商測試套件 (VTS) 測試 Wi-Fi Direct。
單元測試
使用下列測試驗證 Wi-Fi Direct 套件。
服務測試:
atest com.android.server.wifi.p2p
管理員測試:
atest android.net.wifi.p2p
Compatibility Test Suite (CTS) 測試
使用 CTS 測試驗證 Wi-Fi Direct 功能。CTS 會偵測功能啟用時間,並自動納入相關測試。
如要觸發 CTS 測試,請執行:
% atest android.net.wifi.p2p.cts
CTS 驗證器測試
CTS Verifier 測試會使用兩部裝置驗證 Wi-Fi Direct 行為:測試裝置和已知良好的裝置。如要執行測試,請開啟 CTS Verifier,然後前往「Wi-Fi Direct Tests」(Wi-Fi Direct 測試) 專區。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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-27 (世界標準時間)。"],[],[],null,["# Wi-Fi Direct\n\nThe\n[Wi-Fi Direct](https://developer.android.com/guide/topics/connectivity/wifip2p.html)\nfeature, also known as Wi-Fi P2P, allows supporting devices to discover and connect\nto one another directly\nusing the Wi-Fi Direct protocol without internet or cellular network access. This\nfeature, built upon the [Wi-Fi Alliance](https://www.wi-fi.org/)\n(WFA)\n[Wi-Fi Direct specification](https://www.wi-fi.org/wi-fi-direct)\nallows sharing of high-throughput data among\ntrusted devices and apps that are otherwise off-network.\n\nExamples and source\n-------------------\n\nTo use this feature, device manufacturers must implement the Wi-Fi\nVendor and Supplicant HAL interfaces.\n\nIn Android 13,\nthe Supplicant interface uses AIDL for the HAL definition. In\nAndroid 14 and higher, the Vendor HAL interface uses\nAIDL. In Android 12 and lower, the Supplicant and\nVendor HAL interfaces are defined using HIDL.\n\nThe following Wi-Fi HAL surfaces are required to employ the Wi-Fi Direct\nfeature:\n\n- `hardware/interfaces/wifi/aidl` or, `hardware/interfaces/wifi/1.3` or higher\n- `hardware/interfaces/wifi/supplicant/aidl` or, `hardware/interfaces/wifi/supplicant/1.2` or higher\n\nImplementation\n--------------\n\nDevice manufacturers need to provide both framework and HAL/firmware support:\n\n- Framework:\n - AOSP code\n - Enable Wi-Fi Direct: Requires a feature flag\n- Wi-Fi Direct (P2P) HAL support (which implies firmware support)\n\nTo implement this feature, device manufacturers implement the Wi-Fi HIDL or AIDL\ninterfaces and enable the feature flag for Wi-Fi Direct. In `device.mk` located\nin `device/\u003coem\u003e/\u003cdevice\u003e`, modify the `PRODUCT_COPY_FILES` environment variable\nto include support for the Wi-Fi Direct feature: \n\n ```\n PRODUCT_COPY_FILES +=\n frameworks/native/data/etc/android.hardware.wifi.direct.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.hardware.wifi.direct.xml\n ```\n\nAll other requirements for supporting Wi-FI Direct are included in AOSP.\n\nMAC randomization\n-----------------\n\nAndroid requires that the Wi-Fi Direct *device address* and *interface\naddress* are randomized. They must be different from the true MAC address of\nthe device and must meet the following requirements:\n\n- The Wi-Fi Direct device address must be randomized on interface creation if there is no persistent group saved; otherwise the device address must keep using the last generated MAC address.\n- The Wi-Fi Direct interface address, also known as group address, must be randomized every time a connection is established.\n\nWi-Fi Direct MAC randomization is implemented in 'wpa_supplicant' and controlled\nby two configurations, `p2p_device_random_mac_addr` and\n`p2p_interface_random_mac_addr`.\n\nTo enable this feature, device manufacturers must:\n\n- Implement the Wi-Fi Supplicant `ISupplicantP2pIface::setMacRandomization` API. Depending on the interface implemented, this is in:\n - `hardware/interface/wifi/supplicant/1.2` for HIDL\n - `hardware/interface/wifi/supplicant/aidl` for AIDL\n- Set `config_wifi_p2p_mac_randomization_supported` to 'true' in a device custom overlay.\n\nValidation\n----------\n\nAndroid provides a set of unit tests,\n[Compatibility Test Suite (CTS)](/docs/compatibility/cts) tests, and\n[CTS Verifier](/docs/compatibility/cts/verifier) tests to validate the Wi-Fi\nDirect feature. Wi-Fi Direct can also be tested using the\n[Vendor Test Suite (VTS)](/docs/compatibility/vts).\n\n### Unit tests\n\nVerify the Wi-Fi Direct package using the following tests.\n\nService tests: \n\n atest com.android.server.wifi.p2p\n\nManager tests: \n\n atest android.net.wifi.p2p\n\n### Compatibility Test Suite (CTS) tests\n\nUse CTS tests to validate the Wi-Fi Direct feature. CTS detects when the feature\nis enabled and automatically includes the associated tests.\n\nTo trigger the CTS tests, run: \n\n % atest android.net.wifi.p2p.cts\n\n### CTS Verifier tests\n\nCTS Verifier tests validate Wi-Fi Direct behavior using two devices: a test\ndevice and a *known good* device. To run the tests, open CTS Verifier and\nnavigate to the section titled Wi-Fi Direct Tests."]]