加入我們的
郵寄清單,取得關於「鄰近分享」的最新消息和公告。
總覽
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Nearby Connections 是一種對等網路 API,可讓應用程式輕鬆探索鄰近裝置、與這些裝置連線,並即時交換資料,無論網路連線狀態為何皆可使用。
部分應用實例包括:
- 協作白板:在共用的虛擬白板上,與附近的參與者一起記錄想法。
- 本機多人遊戲:設定多人遊戲,並邀請附近的其他使用者加入。
- 多螢幕遊戲:將手機或平板電腦當做遊戲控制器,在附近的 Android 大螢幕裝置 (例如 Android TV) 上玩遊戲。
- 離線檔案傳輸:快速分享相片、影片或任何其他類型的資料,不需要網路連線。
簡介
Nearby Connections 可讓鄰近裝置以完全離線的對等互連方式,進行廣告、探索及連線。裝置之間的連線頻寬高、延遲低,且完全加密,可快速安全地轉移資料。
這項 API 的主要目標是提供簡單、可靠且高效能的平台。在幕後,這項 API 會結合藍牙、BLE 和 Wi-Fi 技術,充分發揮各項技術的優勢,同時避開各自的缺點。這項功能可有效抽象化各種平台和 OS 版本中藍牙和 Wi-Fi 的不確定性,讓開發人員專注於對使用者重要的功能。
API 總覽
API 的使用分為兩個階段:連線前和連線後。
在連線前階段,廣告主會宣傳自己,而探索者會探索附近的廣告主並傳送連線要求。探索者向廣告主發出連線要求後,雙方會啟動對稱式驗證流程,並各自接受 (或拒絕) 連線要求。
雙方接受連線要求後,系統會視為連線已建立,裝置會進入連線後階段,雙方可在此階段交換資料。
廣告與探索
廣告主會向其他探索者顯示,而探索者在找到或失去新的廣告主時,會收到通知。
建立連線
當探索者想連線至附近的廣告主時,可以要求連線。
雙方都會收到連線啟動程序的通知,並可選擇接受或拒絕連線。
雙方都回覆後,系統會通知雙方結果。如果雙方都接受連線,系統就會視為連線已建立,並開始轉移資料。
交換資料
建立連線後,API 的使用方式會對稱,因此廣告主和探索者之間不再有區別。
雙方現在可以交換資料。支援的酬載有 3 種:
- 位元組:位元組陣列上限為 32k,適合傳送中繼資料或控制訊息等內容。
- 檔案:任何大小的檔案;這些檔案會從應用程式傳輸到網路介面,且跨程序界限的複製作業最少。
- 串流:即時產生的資料串流,例如錄製的音訊/視訊,事先不知道最終大小。
接收者會收到新傳入的資料、進度更新、取消或失敗通知。
建立的連線為全雙工,也就是說,廣告主和探索者可以同時傳送及接收資料。
資料收集
使用 Google Play 服務 Nearby SDK 時,系統會收集使用情況分析資料,以提升體驗。
- 效能指標:使用 Nearby SDK 掃描及連線至裝置時,系統會收集探索和連線延遲、可靠性及輸送量。
- 裝置資訊:系統會收集裝置型號、國家/地區、建構版本和應用程式套件名稱,並與效能指標建立關聯,以識別區域和裝置專屬的切片。
使用者可以前往「設定」>「Google」>「使用狀況與診斷」,允許或拒絕收集這類資料。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-13 (世界標準時間)。
[null,null,["上次更新時間:2025-08-13 (世界標準時間)。"],[[["\u003cp\u003eNearby Connections is a peer-to-peer networking API enabling apps to discover, connect, and exchange data with nearby devices in real-time, even without internet access.\u003c/p\u003e\n"],["\u003cp\u003eIt leverages Bluetooth, BLE, and Wi-Fi for high-bandwidth, low-latency, and encrypted communication, abstracting away the complexities of these technologies.\u003c/p\u003e\n"],["\u003cp\u003eThe API facilitates advertising, discovery, and connection establishment between devices, followed by data exchange using bytes, files, or streams.\u003c/p\u003e\n"],["\u003cp\u003eUsage analytics, including performance metrics and device information, are collected to enhance the API's performance but can be controlled by users in their device settings.\u003c/p\u003e\n"]]],[],null,["# Overview\n\nNearby Connections is a peer-to-peer networking API that allows apps to easily\ndiscover, connect to, and exchange data with nearby devices in real-time,\nregardless of network connectivity.\n\nSome example use cases:\n\n- **Collaborative whiteboard:** Jot ideas down with nearby participants on a shared virtual whiteboard.\n- **Local multiplayer gaming:** Set up a multiplayer game and invite other users nearby to join.\n- **Multi-screen gaming:** Use a phone or tablet as a game controller to play games displayed on a nearby large-screen Android device, such as Android TV.\n- **Offline file transfers:** Share photos, videos, or any other type of data quickly and without requiring a network connection.\n\nIntroduction\n------------\n\nNearby Connections enables advertising, discovery, and connections between\nnearby devices in a fully-offline peer-to-peer manner. Connections between\ndevices are high-bandwidth, low-latency, and fully encrypted to enable fast,\nsecure data transfers.\n\nA primary goal of this API is to provide a platform that is simple, reliable,\nand performant. Under the hood, the API uses a combination of Bluetooth, BLE,\nand Wi-Fi technologies, leveraging the strengths of each while circumventing\ntheir respective weaknesses. This effectively abstracts the vagaries of\nBluetooth and Wi-Fi across a range of platforms and OS versions, allowing\ndevelopers to focus on the features that matter to their users.\n\nAPI overview\n------------\n\nUsage of the API falls into two phases: pre-connection, and post-connection.\n\nIn the pre-connection phase, Advertisers advertise themselves, while Discoverers\ndiscover nearby Advertisers and send connection requests. A connection request\nfrom a Discoverer to an Advertiser initiates a symmetric authentication flow\nthat results in both sides independently accepting (or rejecting) the connection\nrequest.\n\nAfter a connection request is accepted by both sides, the connection is\nconsidered to be established and the devices enter the post-connection phase,\nduring which both sides can exchange data.\n\n### Advertising and discovery\n\nAdvertisers will become visible to other discovers and discoverers will be\nnotified when a new advertiser is found or has been lost.\n\n### Establishing connections\n\nWhen a discoverer wishes to connect to a nearby advertiser, the discoverer can\nrequest a connection.\n\nBoth sides will be notified of the connection initiation process and both will\nhave the opportunity to accept or reject the connection.\n\nOnce both sides have responded, each will be notified of the result. If both\nsides accepted the connection, the connection is considered established, and\ndata transference can then begin.\n\n### Exchanging data\n\nAfter a connection is established, further API usage is symmetrical, so there's\nno longer a distinction between Advertiser and Discoverer.\n\nBoth sides can now exchange data. There are 3 types of supported Payloads:\n\n- **Bytes:** Byte arrays limited to 32k; these are good for sending things such as metadata or control messages.\n- **File:** Files of any size; these are transferred from the app to the network interface with minimal copying across process boundaries.\n- **Stream:** A stream of data that is generated on the fly, as in the case of recorded audio/video, with no final size known beforehand.\n\nReceivers will be notified of new incoming data and any progress updates,\ncancellations or failures.\n\nThe connections established are full-duplex, which means that advertisers and\ndiscoverers can simultaneously send and receive data.\n\n### Data Collection\n\nWhen using the Google Play services Nearby SDK, usage analytics are collected to\nimprove the experience.\n\n- **Performance metrics:** Discovery and connection latency, reliability and throughput are collected when scanning for and connecting to devices using the Nearby SDK.\n- **Device information:** Device model, country, build version and the application package name are collected and associated with the performance metrics to identify regional and device-specific slices.\n\nEnd users can allow or deny this collection by navigating to Settings \\\u003e Google \\\u003e\nUsage \\& diagnostics."]]