Code example for WifiManager

Methods: getWifiStateisWifiEnabledsetWifiEnabledstartScan

0
        multicastLock.acquire();
    } 
     
    public int getWifiState() 
    { 
    	return this.mWiFiManager.getWifiState();
    } 
     
    public boolean isWifiEnabled() 
    { 
    	return this.mWiFiManager.isWifiEnabled();
    } 
     
    public String currentlyConnectedBSSID()
    { 
    	String bssid;
    	WifiInfo wifiInfo = this.mWiFiManager.getConnectionInfo();
    	if ( wifiInfo != null && ( bssid = wifiInfo.getBSSID() ) != null )
    		return bssid;
    	return null; 
    } 
     
	public void shutdown() 
	{ 
		setStateNone(); 
		try { mCrowdNet.unregisterReceiver( this.mWiFiReceiver ); } catch ( Exception e ) {}
	} 
	 
	public void turnWiFiOff() 
	{ 
		mWiFiManager.setWifiEnabled( false );
		mConnectedApBSSID = null;
	} 
	 
	public void turnWiFiOn() 
	{ 
		mWiFiManager.setWifiEnabled( true );
	} 
	 
	public void setWiFiAP(String ssid, String password, boolean enabled)
	{ 
		if ( currentlyConnectedBSSID() != null ) 
		{ 
			if ( !enabled ) return;
			mWiFiManager.disconnect();
			mState = WIFI_STATE_AP;
			mApSSID = ssid;
			mApPassword = password;
			return; 
		} 
		 
        WifiConfiguration wifiConfiguration = new WifiConfiguration(); 
 
        wifiConfiguration.SSID = ssid;
        wifiConfiguration.preSharedKey  = password;
        wifiConfiguration.hiddenSSID = false;
        wifiConfiguration.status = enabled ? WifiConfiguration.Status.ENABLED : WifiConfiguration.Status.DISABLED;   
 
        wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
 
        wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
 
        wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
 
        wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);           
        wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
 
        boolean foundApMethod = false;
        try 
        { 
        	 
        	Method[] wmMethods = mWiFiManager.getClass().getDeclaredMethods();
        	 
        	for( Method method: wmMethods )
        	{ 
        		//report(method.getName()); 
        		if ( method.getName().equals("setWifiApEnabled") )
        		{ 
        			report("Set AP for " + ssid + " " + password + " enabled " + enabled );
        			foundApMethod = true;
        			method.invoke(mWiFiManager, wifiConfiguration, enabled);
        			mApSSID = ssid;
        			mApPassword = password;
        			mState = WIFI_STATE_AP;
        			report ("isWiFiAPOn() returns " + isWiFiAPOn() ); 
        			break; 
        		} 
        		//int netty = wifi.addNetwork(_wifi_configuration); 
        		//wifi.enableNetwork(netty, true); 
        	} 
        } 
        catch ( Exception ex )
        { 
        	report("Problem setting AP mode " + ex );
        } 
        if ( !foundApMethod )
        	report("ApMethod not found"); 
    } 
 
	public boolean isWiFiAPOn() 
	{ 
		/* 
		 * TODO needs to throw exception if no such method 
		 */ 
		try 
		{ 
			Method method = mWiFiManager.getClass().getMethod( "getWifiApConfiguration" );
			WifiConfiguration config = (WifiConfiguration)method.invoke( mWiFiManager );
			return ( config != null && config.status == WifiConfiguration.Status.ENABLED );
		} 
		catch ( Exception e )
		{ 
			 
		} 
		return false; 
	} 
	 
	public void setWiFiAPOff( ) 
	{ 
		setWiFiAP( mApSSID, mApPassword, false );
		report ("setWiFiAPOff: isWiFiAPOn() returns " + isWiFiAPOn() ); 
	} 
	 
	public void setStateNone() 
	{ 
		switch ( mState )
		{ 
			case WIFI_STATE_NONE:
				return; 
				 
			case WIFI_STATE_AP:
				setWiFiAPOff( ); 
				break; 
				 
			case WIFI_STATE_SCAN:
				return; 
				 
			case WIFI_STATE_CONNECTED:
				break; 
		} 
		mState = WIFI_STATE_NONE;
	} 
 
	public void setToScan() 
	{ 
		switch ( mState )
		{ 
			case WIFI_STATE_NONE:
				break; 
				 
			case WIFI_STATE_AP:
				setWiFiAPOff( ); 
				break; 
				 
			case WIFI_STATE_SCAN:
				return; 
				 
			case WIFI_STATE_CONNECTED:
				break; 
		} 
		if ( isWifiEnabled() )  
			mWiFiManager.startScan();  
		else 
			if ( this.getWifiState() != WifiManager.WIFI_STATE_ENABLING )
				mWiFiManager.setWifiEnabled( true );
		mState = WIFI_STATE_SCAN;
	} 
 
	public String getWiFiManagerMethods()
	{