Open In App

Nearby Wi-Fi Access Permission in Android 13

Last Updated : 06 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Android 13 is another one of Google’s larger mid-cycle improvements to the operating system. These Quarterly Platform Releases (abbreviated QPR) introduce a few well-chosen new features, Significant privacy improvements were made with the last release, and Android 13 is continuing that trend. Going forward, we see even more implications put on to us as developers by Google, seems that now we will need special permissions to access the device’s real-time Wi-Fi scanning capabilities, and thus, we need to make our app ready for utilizing this permission, and keep our work getting done, otherwise as more and more devices continue to join the party, your app will slowly start to become outdated, if it relies on the nearby information from the Wi-Fi scanners. 

To solve this issue, this Geeks for Geeks article will help you write code to utilize and ask permission for scanning Wi-Fi.

Note: Now, starting from Android 13, if your app calls a Wi-Fi API without asking for permission first, then your app will crash and a security-exception would trigger.

The NEARBY WIFI DEVICES runtime permission must be requested by apps that handle Wi-Fi connections and are designed for Android 13 (API level 33) or higher. Earlier versions of Android required these apps to declare the ACCESS FINE LOCATION permission; this permission makes it simpler to explain an app’s access to adjacent Wi-Fi devices.

If your app does not rely on precise location, then declare it:

If your app does not ever obtain location data through Wi-Fi APIs, you should adamantly state such when you target Android 13 or higher. Set the usesPermissionFlags attribute in your app’s manifest file to neverForLocation to make this claim, as seen in the following code sample. When you claim that Bluetooth device information is never utilized for location, you go through a similar process

The Nearby devices permission group includes the NEARBY WIFI DEVICES permission. The permissions for Bluetooth and Ultra-wideband are also included in this group, which was added in Android 12 (API level 31). Any time you ask for more than one permission from this group, the system displays a single runtime prompt and requests the user’s approval before allowing access to adjacent devices for your program. The Nearby device’s permissions must be enabled or disabled together in the system settings; for instance, users cannot turn off Wi-Fi access while maintaining Bluetooth access for a specific app. To add this permission to your android app, simply do these steps.

Add the permission in your Android’s Manifest File:

Perhaps the first thing you will do to add the permission is to add it to your app’s Manifest file, to tell the system that your app is requesting this permission beforehand, to do it, simply:

<manifest ...>
    <uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"
                     android:usesPermissionFlags="neverForLocation" />
</manifest>

Lookup APIs that demand authorization:

Before moving forward it will be better to know if your app requires to use the request permission or not, if your app uses any of the following Wi-Fi APIs, your app must target Android 13 or higher and declare the NEARBY WIFI DEVICES permission:

  • WifiManager
    • startLocalOnlyHotspot()
  • WifiAwareManager
    • attach()
  • WifiAwareSession
    • publish()
    • subscribe()

If your app is targeting any of these APIs you should immediately ask for permission otherwise your app will crash on the newer Android Version.

Location permission is necessary for older versions and some APIs:

Even though your app is designed to run on Android 13 or higher, some Wi-Fi APIs still require the ACCESS FINE LOCATION permission. The following WifiManager class methods serve as examples:

  • startScan()
  • getScanResults() 

Additionally, to maintain backward compatibility in your app, you should keep any declarations for ACCESS FINE LOCATION since the NEARBY WIFI DEVICES permission is only available on Android 13 and higher. However, as seen in the following code snippet, you can set the maximum SDK version of this permission to 32 if your app doesn’t otherwise rely on exact location data.

Conclusion

This brings us to the end of this article, this is rather an important one, although the solution to it lies really short, without taking it, will lead your app to crash on the newer versions of Android (13+).


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads