Before you begin
Before you start to code using the Nearby Connections API:
- Install the Google Play Services SDK.
- Download and review the Nearby Connections API code samples.
Request permissions
Before using Nearby Connections, your app must request the appropriate
permissions. Add the following permissions to your 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"/>
Since ACCESS_FINE_LOCATION, BLUETOOTH_ADVERTISE, BLUETOOTH_CONNECT, BLUETOOTH_SCAN and READ_EXTERNAL_STORAGE are considered to be dangerous system permissions, in addition to adding them to your manifest, you must request these permissions at runtime, as described in Requesting Permissions.
If the user does not grant all required permissions, the Nearby Connections API will refuse to allow your app to start advertising or discovering.