Exploring Android 13: Nearby Wi-Fi Devices Permission
Android 13 (Tiramisu)’s new APIs and features are ready to be tried out by developers. One of the new features offered in this phase is Nearby Wi-Fi Devices Permission.
Before Android 13, if you want to use Wi-Fi-related features, you would have to ask for ACCESS_FINE_LOCATION
permission. This is not ideal as users could be confused and suspicious that your application is actually tracking their location, instead of accessing their Wi-Fi service. Android 13's new Nearby Wi-Fi Devices permission addresses this issue by introducing new permission for accessing Wi-Fi-related features: NEARBY_WIFI_DEVICES
.
However, please notice that this permission can only be used for some use cases. For other cases, you still need to use the ACCESS_FINE_LOCATION
permission.
In this article, we will build a simple application that utilizes Nearby Wi-Fi Devices permission. Then, to demonstrate that the permission works, we will call a Wi-Fi related feature. In this article, I will call a Local Only Hotspot feature. Here’s what it will look like at the end:
Where’s the Source Code?
Step by Step Explanation
Follow the steps provided below to make this application.
1. As of the writing of this article, Android 13 can only be used with the preview release of Android Studio. Therefore, you must go to this page, and download the Canary Build of Android Studio.
2. Once installed, you need to install the Android 13 SDK. You can do this by clicking Tools > SDK Manager. In the SDK Platforms tab, select Android 13. In the SDK Tools tab, select Android SDK Build-Tools 33. Click OK to install the SDK.
3. Once SDK is installed, open the Android Studio. Create new project with Empty Activity option.
4. First off, you need to change the target and compile SDK version of this app. Open app/build.gradle
and you would see something like this:
Change the compileSdk
and targetSdk
to use the Android 13 SDK like shown in the code snippet below. Don’t forget to click Sync Now after.
5. First, we will add permissions to our AndroidManifest.xml
file. We will be adding 2 permissions: NEARBY_WIFI_DEVICES
(the topic of this article) and CHANGE_WIFI_STATE
(This permission is needed by Local Only Hotspot). Notice how the Nearby Wi-Fi Devices permission uses android:usesPermissionFlags=”neverLocation”
to assert that your app doesn’t derive physical location.
6. Next, we will create our layout. The image below shows how activity_main.xml
will look like. It consists of a button that start Local Only Hotspot on the device and a text that displays whether the Hotspot has started or not. Before attempting to start it however, your app will ask for Nearby Wi-Fi Devices permission to be granted by user.
activity_main.xml
should look like.This is how activity_main.xml
content looks like:
7. Lastly, we will update our MainActivity.kt
file. You may copy-paste the code snippet below. The most important thing to notice is that the Nearby Wi-Fi Devices permission behaves just like usual runtime permission that the user has to grant in order to be activated. Please note this best practice for when you request permission to user.
8. That’s it! Now run your app and you can see Nearby Wi-Fi Devices permission in action like this:
As always, thanks for reading!
—
Want to learn more about Android 13?
Check out all of our articles from “Exploring Android 13” series here:
![](https://miro.medium.com/v2/resize:fill:388:388/1*Dg4lY8zAp10wmFC--FnShA.png)
![](https://miro.medium.com/v2/resize:fill:388:388/1*OVhAPRL9ANPXfCVpYC86YQ.png)
![](https://miro.medium.com/v2/resize:fill:388:388/1*pofnqHxiVFkUj1-nibcVew.png)