开始使用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
安装最新版本的 Xcode
如需在项目中使用 Nearby Connection,您需要使用 Xcode 13.0 或更高版本。
将 Nearby Connections 软件包添加到您的项目
Xcode 附带内置的软件包管理器。如需将 Nearby Connections 添加到 Xcode 项目中,请依次选择 File > Add Packages…,然后搜索 google/nearby
。然后点击 Add Package 并等待流程完成(这可能需要几分钟时间)。
请求访问受保护的资源
近距离连接需要同时获得蓝牙和本地网络访问权限,才能进行广播、发现、建立连接和传输数据。在应用的 Info.plist
中,为所需的每项资源提供使用情况说明。如果不这样做,尝试访问该资源的操作将会失败,甚至可能会导致应用崩溃。
必需的用途说明键:
NSBluetoothAlwaysUsageDescription
NSLocalNetworkUsageDescription
除了使用情况说明键之外,还需要添加一个 NSBonjourServices
键,其中包含应用将浏览的服务类型列表。必须添加的唯一服务类型可以通过获取应用服务 ID 的 SHA-256
哈希的前 12 个字节来生成。
您可以使用以下工具自动生成此值:
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-13。
[null,null,["最后更新时间 (UTC):2025-08-13。"],[],[],null,["# Get started\n\nInstall the latest version of Xcode\n-----------------------------------\n\nTo use Nearby Connection in your project,\n[Xcode](https://developer.apple.com/xcode/) **13.0 or later** is required.\n\nAdd the Nearby Connections package to your project\n--------------------------------------------------\n\nXcode comes with a built-in package manager. To add Nearby Connections to your\nXcode project, select **File** \\\u003e **Add Packages...** and search for\n`google/nearby`. Then click **Add Package** and wait for the process to complete\n(this may take a few minutes).\n\nRequest access to protected resources\n-------------------------------------\n\nNearby Connections needs access to both Bluetooth and the local network to\nadvertise, discover, make connections, and transfer data. Provide a usage\ndescription for each resource needed, in your app's `Info.plist`. If you don't,\nattempts to access the resource will fail, and might even cause your app to\ncrash.\n\nRequired usage description keys:\n\n- `NSBluetoothAlwaysUsageDescription`\n- `NSLocalNetworkUsageDescription`\n\nIn addition to usage description keys, an `NSBonjourServices` key with a list of\nthe service types that will be browsed by the app, will also need to be added.\nThe only service type that must be added can be generated by taking the first 12\nbytes of the `SHA-256` hash of your app's service ID.\n\nYou can use the following tool to automatically generate this value for you: \nService ID\nGenerate\n\n```\n\n```"]]