Android Wifi Manager Documentation

If you have any questions or issues please email me at jschieck@gmail.com and thanks for your support!

NOTICE: Unity 2018.2 is not fully supported due to this bug.

Overview

Welcome to the Android Wifi Manager documentation page. Android Wifi Manager provides access to the WifiManager class on the Android platform. This allows control over the devices wifi enabled/disabled state, scanning for nearby wifi networks, and connecting to them.

Getting Started

Setup

After importing the package, a dialog prompt will appear asking to add the required permissions to the AndroidManifest.xml file located at Plugins/Android/AndroidManifest.xml. If there isn't already a manifest file in the project, a template provided in the plugin will be copied there. If for some reason the dialog does not automatically pop-up, it can be accessed via Window/Android Wifi Manager/Setup Permissions... This can also be used to re-run the setup process at any time.

Settings for this setup process are stored in ProjectSettings/AndroidWifiManager.asset.

Required Permissions

Three Android permissions are required for full functionality of the plugin.

Plugin Usage

All classes in the plugin are located in the FSG.Android.Wifi namespace.

All functionality can be accessed on the static class AndroidWifiManager.

AndroidWifiManager Methods

AndroidWifiManager.SetWifiEnabled(bool enabled)
	// Enable or disable Wi-Fi.        
	
AndroidWifiManager.IsWifiEnabled()	
	// Return whether Wi-Fi is enabled or disabled.	  
	
AndroidWifiManager.StartScan()
	// Begins a scan to find available wifi networks. Limited to 4 times per 2 minutes while the app is open.
        
AndroidWifiManager.GetScanResults()
	// Return the results of the latest access point scan.
        
AndroidWifiManager.GetConfiguredNetworks()
	// Return a list of all the networks configured for the current foreground user. 
        
AndroidWifiManager.GetWifiState()
	// Gets the Wi-Fi enabled state.	
        
AndroidWifiManager.AddNetwork(AndroidWifiConfiguration config)	
	// Add a new network description to the set of configured networks. 
	// The networkId field of the supplied configuration object is ignored. The new network will be marked DISABLED by default. 
	// To enable it, called EnableNetwork
        
AndroidWifiManager.RemoveNetwork(int networkId)	
	// Remove the specified network from the list of configured networks
        
AndroidWifiManager.EnableNetwork(int networkId, bool autoConnect)	
	// Allow a previously configured network to be associated with. 
	// If attemptConnect is true, an attempt to connect to the selected network is initiated.
        
AndroidWifiManager.DisableNetwork(int networkId)	
	// Disables a network based off of it's networkId
        
AndroidWifiManager.Disconnect()	
	// Disconnects from the currently connected to network
	// Simply disabling wifi also works
        
AndroidWifiManager.Connect(string SSID, string password)	
	// Connect to the specified SSID using the supplied password. Use an empty password if the network has no security.
				

Scan For Nearby Wifi

private void Start()
{
	// Start the scan
	StartCoroutine(PrintWifiNetworks());
}

private IEnumerator PrintWifiNetworks()
{
	// Check if wifi is enabled
	if (AndroidWifiManager.IsWifiEnabled() == false)
	{
		// If not, enable it
		AndroidWifiManager.SetWifiEnabled(true);
		// Give the device time to enable wifi
		yield return new WaitForSeconds(1);
	}

	// Initiate a scan (not always needed)
	AndroidWifiManager.StartScan();

	// Wait for the scan to complete
	yield return new WaitForSeconds(1);

	// Get the list of scan results
	var results = AndroidWifiManager.GetScanResults();
    foreach (AndroidWifiScanResults result in results)
    {
    	Debug.LogFormat("SSID: {0} Signal: {1}dBm Security Type: {2}", result.SSID, result.level, result.securityType);
    }
}
			

Connecting to a Network

private void Start()
{
    // Connect to the network
    StartCoroutine(ConnectToWifi("My Wifi Network", "mywifipassword"));
}

// Connects to the target SSID network
// NOTE: passwords are not required if the network returns AndroidWifiSecurityType.OPEN from scan results
private IEnumerator ConnectToWifi(string ssid, string password = "")
{
	// Check if wifi is enabled
	if (AndroidWifiManager.IsWifiEnabled() == false)
	{
		// If not, enable it
		AndroidWifiManager.SetWifiEnabled(true);
		
		// Give the device time to enable wifi
		yield return new WaitForSeconds(1);
	}	
	
	// Connect to the network with the provided password
	var result = AndroidWifiManager.Connect(ssid, password);
	if (result == AndroidWifiManager.ConnectResult.SUCCESS)
	{
		Debug.LogFormat("Successfully connected to network {0}", ssid);
	}
	else
	{
		Debug.LogErrorFormat("Unable to connect to network {0}: Reason: {1}", ssid, result);
	}
}
			

If you have any questions or issues please email me at jschieck@gmail.com and thanks for your support!