Class WiFi
Entry point for inspecting, scanning and connecting to WiFi networks.
The API is split into two tiers:
-
Information queries --
getCurrentSSID(),getBSSID(),getGateway(),getIp(). These methods read the device's current network configuration. On modern Android (10+) and iOS the SSID/BSSID queries require runtime permissions or entitlements that the build pipeline injects automatically based on classpath scanning. -
Active management --
scan()andconnect(SSID, password, security). Active management triggers a one-shot scan or attempts to associate the device with a specific network. On Android 10+ this is implemented withNetworkSpecifier; on iOS withNEHotspotConfiguration.
All callbacks are dispatched on the EDT so it is safe to update UI in response to them.
Required permissions
The build pipeline injects the necessary permissions automatically when
WiFi is referenced anywhere in the application:
- Android:
ACCESS_WIFI_STATE,CHANGE_WIFI_STATE,ACCESS_NETWORK_STATE,CHANGE_NETWORK_STATE,ACCESS_FINE_LOCATION(required for SSID readout on API 26+),NEARBY_WIFI_DEVICES(API 33+). - iOS:
com.apple.developer.networking.HotspotConfigurationentitlement for connect,com.apple.developer.networking.wifi-infoentitlement for the SSID/BSSID query, andNSLocationWhenInUseUsageDescription(CoreLocation authorization is required since iOS 13 to read SSID).
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidconnect(String ssid, String password, WiFiSecurity security, WiFiConnectCallback callback) Attempts to associate the device withssid.static voiddisconnect(String ssid) Disconnect the request made viaconnect.static StringgetBSSID()The BSSID (MAC address of the access point) of the currently associated WiFi network, formatted as colon-separated lowercase hex (e.g.aa:bb:cc:11:22:33), ornullif unavailable.static StringThe SSID of the currently associated WiFi network, ornullif not connected to WiFi or if permission was denied.static StringDefault gateway IP address as a dotted quad (e.g.192.168.1.1), ornullif no default gateway is configured.static StringgetIp()Local IP address on the WiFi interface as a dotted quad (e.g.192.168.1.42), ornullif WiFi is not active.static booleantrueif the current platform can query WiFi information.static booleantrueif the current platform supports active scan / connect.static voidscan(WiFiScanCallback callback) Triggers a one-shot WiFi scan and reports results tocallbackon the EDT.
-
Method Details
-
isInfoSupported
public static boolean isInfoSupported()trueif the current platform can query WiFi information. -
isManagementSupported
public static boolean isManagementSupported()trueif the current platform supports active scan / connect. -
getCurrentSSID
The SSID of the currently associated WiFi network, ornullif not connected to WiFi or if permission was denied. On iOS 13+ the OS returnsnullunless the app has CoreLocation authorization. -
getBSSID
The BSSID (MAC address of the access point) of the currently associated WiFi network, formatted as colon-separated lowercase hex (e.g.aa:bb:cc:11:22:33), ornullif unavailable. -
getGateway
Default gateway IP address as a dotted quad (e.g.192.168.1.1), ornullif no default gateway is configured. -
getIp
Local IP address on the WiFi interface as a dotted quad (e.g.192.168.1.42), ornullif WiFi is not active. -
scan
Triggers a one-shot WiFi scan and reports results to
callbackon the EDT. The callback receives an array ofWiFiNetworksorted by signal strength (strongest first). Passnullto cancel an in-progress scan.Behaviour:
- Android: uses
WifiManager.startScan(). On API 28+ the OS throttles scans to 4 per 2 minutes per foreground app; throttled scans return cached results. - iOS: not supported -- iOS does not expose a public WiFi scan
API.
callbackis invoked withnullanderrorset. - Simulator: returns a small synthetic list and prints a warning reminding the developer the data is fabricated.
- Android: uses
-
connect
public static void connect(String ssid, String password, WiFiSecurity security, WiFiConnectCallback callback) Attempts to associate the device with
ssid.passwordmay benullfor open networks.securitymust be one of theWiFiSecurityconstants and must match the security mode the access point advertises -- a mismatch will causeconnectto fail.Behaviour:
- Android 10+: uses
WifiNetworkSpecifierviaConnectivityManager.requestNetwork(). The OS shows a system dialog asking the user to approve the association; this dialog can't be bypassed. - Android 9 and below: uses the legacy
WifiConfigurationAPI andWifiManager.enableNetwork(). The user is not prompted but the call may be a no-op on OEM builds that removed the API early. - iOS 11+: uses
NEHotspotConfiguration. The user is shown a system prompt the first time the app tries to join the SSID. - Simulator: logs the request and reports a failure.
- Android 10+: uses
-
disconnect
Disconnect the request made viaconnect. On Android 10+ this releases theNetworkSpecifier; on iOS it removes the hotspot configuration. Apps can't force-disconnect a network the user joined manually.
-