概览
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Nearby Connections 是一种点对点联网 API,可让应用轻松发现附近的设备、连接到这些设备并与这些设备实时交换数据,而无需考虑网络连接情况。
一些示例应用场景:
- 协作式白板:在共享的虚拟白板上与附近的参与者一起记下想法。
- 本地多人游戏:设置多人游戏并邀请附近的其他用户加入。
- 多屏游戏:使用手机或平板电脑作为游戏控制器,畅玩显示在附近的大屏 Android 设备(例如 Android TV)上的游戏。
- 离线文件传输:快速分享照片、视频或任何其他类型的数据,无需连接网络。
简介
Nearby Connections 支持以完全离线的对等方式在附近的设备之间进行通告、发现和连接。设备之间的连接具有高带宽、低延迟的特点,并且经过全面加密,可实现快速、安全的数据传输。
此 API 的主要目标是提供一个简单、可靠且高性能的平台。在底层,该 API 结合使用了蓝牙、BLE 和 Wi-Fi 技术,充分发挥每种技术的优势,同时避开各自的缺点。这有效地抽象了各种平台和操作系统版本中蓝牙和 Wi-Fi 的不确定性,使开发者能够专注于对用户重要的功能。
API 概览
API 的使用分为两个阶段:连接前和连接后。
在连接前阶段,广告客户会宣传自己,而发现者会发现附近的广告客户并发送连接请求。当发现者向广告客户发送连接请求时,会启动对称身份验证流程,最终双方会独立接受(或拒绝)连接请求。
当连接请求被双方接受后,连接即被视为已建立,设备进入连接后阶段,在此阶段双方可以交换数据。
广告和发现
广告客户将对其他发现者可见,并且当发现新的广告客户或广告客户丢失时,发现者会收到通知。
建立连接
当发现者希望与附近的广告客户建立连接时,可以请求连接。
双方都会收到有关连接启动过程的通知,并且双方都有机会接受或拒绝连接。
双方都做出回应后,系统会通知双方结果。如果双方都接受了连接,则连接被视为已建立,然后可以开始数据传输。
交换数据
连接建立后,后续 API 使用是对称的,因此不再区分广告客户和发现者。
现在,双方可以交换数据了。支持 3 种类型的载荷:
- 字节:字节数组限制为 32k;这些数组非常适合发送元数据或控制消息等内容。
- 文件:任意大小的文件;这些文件从应用传输到网络接口,在进程边界之间进行最少的复制。
- 流:动态生成的数据流,例如录制的音频/视频,事先不知道最终大小。
接收者会收到有关新传入数据以及任何进度更新、取消或失败的通知。
建立的连接是全双工的,这意味着广告客户和发现者可以同时发送和接收数据。
数据收集
使用 Google Play 服务的 Nearby SDK 时,系统会收集使用情况分析数据,以改进体验。
- 性能指标:使用 Nearby SDK 扫描和连接设备时,系统会收集发现和连接延迟时间、可靠性和吞吐量。
- 设备信息:系统会收集设备型号、国家/地区、build 版本和应用软件包名称,并将其与性能指标相关联,以确定区域和设备特定的细分。
最终用户可以前往“设置”>“Google”>“使用情况和诊断信息”,允许或拒绝此数据收集。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-13。
[null,null,["最后更新时间 (UTC):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."]]