开始使用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
准备工作
开始使用 Nearby Connections API 进行编码之前,请执行以下操作:
请求权限
在使用 Nearby Connections 之前,您的应用必须先请求适当的
权限。向您的 AndroidManifest.xml
添加以下权限:
<!-- Required for Nearby Connections -->
<uses-permission android:maxSdkVersion="31" android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:maxSdkVersion="31" android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:maxSdkVersion="30" android:name="android.permission.BLUETOOTH" />
<uses-permission android:maxSdkVersion="30" android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:maxSdkVersion="28" android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:minSdkVersion="29" android:maxSdkVersion="31" android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:minSdkVersion="31" android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:minSdkVersion="31" android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:minSdkVersion="31" android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:minSdkVersion="32" android:name="android.permission.NEARBY_WIFI_DEVICES" />
<!-- Optional: only required for FILE payloads -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
开始时间
ACCESS_FINE_LOCATION、
BLUETOOTH_ADVERTISE、
BLUETOOTH_CONNECT、
BLUETOOTH_SCAN
和
READ_EXTERNAL_STORAGE
被视为危险的系统
权限,
除了将其添加到清单中外,您还必须请求这些权限
如请求
权限。
如果用户未授予所有必需的权限,则 Nearby Connections API
会拒绝允许您的应用开始投放广告或让用户发现您的应用。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-13。
[null,null,["最后更新时间 (UTC):2025-08-13。"],[[["\u003cp\u003eBefore using the Nearby Connections API, install the Google Play Services SDK and review the provided code samples.\u003c/p\u003e\n"],["\u003cp\u003eApps using Nearby Connections must request specific permissions in the \u003ccode\u003eAndroidManifest.xml\u003c/code\u003e to function correctly.\u003c/p\u003e\n"],["\u003cp\u003eSeveral permissions, like location and Bluetooth access, require runtime requests due to their sensitive nature.\u003c/p\u003e\n"],["\u003cp\u003eThe Nearby Connections API will not function if the user denies necessary permissions for advertising or discovery.\u003c/p\u003e\n"]]],["To use the Nearby Connections API, install the Google Play Services SDK and review the code samples. In your `AndroidManifest.xml`, add permissions for `ACCESS_WIFI_STATE`, `CHANGE_WIFI_STATE`, `BLUETOOTH`, `BLUETOOTH_ADMIN`, `ACCESS_COARSE_LOCATION`, `ACCESS_FINE_LOCATION`, `BLUETOOTH_ADVERTISE`, `BLUETOOTH_CONNECT`, `BLUETOOTH_SCAN`, and `NEARBY_WIFI_DEVICES`. For `ACCESS_FINE_LOCATION`, `BLUETOOTH_ADVERTISE`, `BLUETOOTH_CONNECT`, `BLUETOOTH_SCAN` and `READ_EXTERNAL_STORAGE` also request these permissions at runtime, as they are classified as dangerous. The app can not use the API if the user does not provide the permissions.\n"],null,["# Get started\n\nBefore you begin\n----------------\n\nBefore you start to code using the Nearby Connections API:\n\n- Install the [Google Play Services\n SDK](//developer.android.com/google/play-services/setup.html).\n- Download and review the [Nearby Connections API code\n samples](https://github.com/android/connectivity-samples/tree/main/NearbyConnectionsWalkieTalkie).\n\nRequest permissions\n-------------------\n\nBefore using Nearby Connections, your app must request the appropriate\npermissions. Add the following permissions to your `AndroidManifest.xml`: \n\n```gdscript\n\u003c!-- Required for Nearby Connections --\u003e\n\u003cuses-permission android:maxSdkVersion=\"31\" android:name=\"android.permission.ACCESS_WIFI_STATE\" /\u003e\n\u003cuses-permission android:maxSdkVersion=\"31\" android:name=\"android.permission.CHANGE_WIFI_STATE\" /\u003e\n\u003cuses-permission android:maxSdkVersion=\"30\" android:name=\"android.permission.BLUETOOTH\" /\u003e\n\u003cuses-permission android:maxSdkVersion=\"30\" android:name=\"android.permission.BLUETOOTH_ADMIN\" /\u003e\n\u003cuses-permission android:maxSdkVersion=\"28\" android:name=\"android.permission.ACCESS_COARSE_LOCATION\" /\u003e\n\u003cuses-permission android:minSdkVersion=\"29\" android:maxSdkVersion=\"31\" android:name=\"android.permission.ACCESS_FINE_LOCATION\" /\u003e\n\u003cuses-permission android:minSdkVersion=\"31\" android:name=\"android.permission.BLUETOOTH_ADVERTISE\" /\u003e\n\u003cuses-permission android:minSdkVersion=\"31\" android:name=\"android.permission.BLUETOOTH_CONNECT\" /\u003e\n\u003cuses-permission android:minSdkVersion=\"31\" android:name=\"android.permission.BLUETOOTH_SCAN\" /\u003e\n\u003cuses-permission android:minSdkVersion=\"32\" android:name=\"android.permission.NEARBY_WIFI_DEVICES\" /\u003e\n\u003c!-- Optional: only required for FILE payloads --\u003e\n\u003cuses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\"/\u003e\n```\n\nSince\n[ACCESS_FINE_LOCATION](https://developer.android.com/reference/android/Manifest.permission#ACCESS_FINE_LOCATION),\n[BLUETOOTH_ADVERTISE](https://developer.android.com/reference/android/Manifest.permission#BLUETOOTH_ADVERTISE),\n[BLUETOOTH_CONNECT](https://developer.android.com/reference/android/Manifest.permission#BLUETOOTH_CONNECT),\n[BLUETOOTH_SCAN](https://developer.android.com/reference/android/Manifest.permission#BLUETOOTH_SCAN)\nand\n[READ_EXTERNAL_STORAGE](https://developer.android.com/reference/android/Manifest.permission#READ_EXTERNAL_STORAGE)\nare considered to be [dangerous system\npermissions](//developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous),\nin addition to adding them to your manifest, you must request these permissions\nat runtime, as described in [Requesting\nPermissions](//developer.android.com/training/permissions/requesting.html).\n\nIf the user does not grant all required permissions, the Nearby Connections API\nwill refuse to allow your app to start advertising or discovering."]]