Search in sources :

Example 6 with WifiInfo

use of android.net.wifi.WifiInfo in project SmartAndroidSource by jaychou2012.

the class SystemInfo method getWifiIpAddress.

/**
	 * getWifiIpAddress
	 * 
	 * @return the wifi's ipAddress
	 */
public int getWifiIpAddress() {
    WifiManager wifi_service = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifi_service.getConnectionInfo();
    return wifiInfo.getIpAddress();
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiInfo(android.net.wifi.WifiInfo)

Example 7 with WifiInfo

use of android.net.wifi.WifiInfo in project SmartAndroidSource by jaychou2012.

the class SystemInfo method getMacAddress.

/**
	 * 
	 * Get the Device's MacAddress,need android.permission.ACCESS_WIFI_STATE
	 * 
	 * @return the Device's MacAddress
	 */
public String getMacAddress() {
    String[] other = { "null", "null" };
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo.getMacAddress() != null) {
        other[0] = wifiInfo.getMacAddress();
    } else {
        other[0] = "Fail";
    }
    return other[0];
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiInfo(android.net.wifi.WifiInfo)

Example 8 with WifiInfo

use of android.net.wifi.WifiInfo in project qksms by moezbhatti.

the class NetworkIdentity method buildNetworkIdentity.

/**
     * Build a {@link android.net.NetworkIdentity} from the given {@link NetworkState},
     * assuming that any mobile networks are using the current IMSI.
     */
public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state) {
    final int type = state.networkInfo.getType();
    final int subType = state.networkInfo.getSubtype();
    // TODO: consider moving subscriberId over to LinkCapabilities, so it
    // comes from an authoritative source.
    String subscriberId = null;
    String networkId = null;
    boolean roaming = false;
    if (isNetworkTypeMobile(type)) {
        final TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        roaming = telephony.isNetworkRoaming();
        if (state.subscriberId != null) {
            subscriberId = state.subscriberId;
        } else {
            subscriberId = telephony.getSubscriberId();
        }
    } else if (type == TYPE_WIFI) {
        if (state.networkId != null) {
            networkId = state.networkId;
        } else {
            final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            final WifiInfo info = wifi.getConnectionInfo();
            networkId = info != null ? info.getSSID() : null;
        }
    }
    return new NetworkIdentity(type, subType, subscriberId, networkId, roaming);
}
Also used : WifiManager(android.net.wifi.WifiManager) TelephonyManager(android.telephony.TelephonyManager) WifiInfo(android.net.wifi.WifiInfo)

Example 9 with WifiInfo

use of android.net.wifi.WifiInfo in project android by owncloud.

the class ConnectivityActionReceiver method onReceive.

@Override
public void onReceive(final Context context, Intent intent) {
    // LOG ALL EVENTS:
    Log_OC.v(TAG, "action: " + intent.getAction());
    Log_OC.v(TAG, "component: " + intent.getComponent());
    Bundle extras = intent.getExtras();
    if (extras != null) {
        for (String key : extras.keySet()) {
            Log_OC.v(TAG, "key [" + key + "]: " + extras.get(key));
        }
    } else {
        Log_OC.v(TAG, "no extras");
    }
    /**
         * There is an interesting mess to process WifiManager.NETWORK_STATE_CHANGED_ACTION and
         * ConnectivityManager.CONNECTIVITY_ACTION in a simple and reliable way.
         *
         * The former triggers much more events than what we really need to know about Wifi connection.
         *
         * But there are annoying uncertainties about ConnectivityManager.CONNECTIVITY_ACTION due
         * to the deprecation of ConnectivityManager.EXTRA_NETWORK_INFO in API level 14, and the absence
         * of ConnectivityManager.EXTRA_NETWORK_TYPE until API level 17. Dear Google, how should we
         * handle API levels 14 to 16?
         *
         * In the end maybe we need to keep in memory the current knowledge about connectivity
         * and update it taking into account several Intents received in a row
         *
         * But first let's try something "simple" to keep a basic retry of instant uploads in
         * version 1.9.2, similar to the existent until 1.9.1. To be improved.
         */
    if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
        NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
        String bssid = intent.getStringExtra(WifiManager.EXTRA_BSSID);
        if (// not enough; see (*) right below
        networkInfo.isConnected() && wifiInfo != null && !UNKNOWN_SSID.equals(wifiInfo.getSSID().toLowerCase()) && bssid != null) {
            Log_OC.d(TAG, "WiFi connected");
            wifiConnected(context);
        } else {
            // TODO tons of things to check to conclude disconnection;
            // TODO maybe alternative commented below, based on CONNECTIVITY_ACTION is better
            Log_OC.d(TAG, "WiFi disconnected ... but don't know if right now");
        }
    }
// (*) When WiFi is lost, an Intent with network state CONNECTED and SSID "<unknown ssid>" is
//      received right before another Intent with network state DISCONNECTED; needs to
//      be differentiated of a new Wifi connection.
//
//  Besides, with a new connection two Intents are received, having only the second the extra
//  WifiManager.EXTRA_BSSID, with the BSSID of the access point accessed.
//
//  Not sure if this protocol is exact, since it's not documented. Only found mild references in
//   - http://developer.android.com/intl/es/reference/android/net/wifi/WifiInfo.html#getSSID()
//   - http://developer.android.com/intl/es/reference/android/net/wifi/WifiManager.html#EXTRA_BSSID
//  and reproduced in Nexus 5 with Android 6.
/**
         * Possible alternative attending ConnectivityManager.CONNECTIVITY_ACTION.
         *
         * Let's see what QA has to say
         *
        if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            NetworkInfo networkInfo = intent.getParcelableExtra(
                    ConnectivityManager.EXTRA_NETWORK_INFO      // deprecated in API 14
            );
            int networkType = intent.getIntExtra(
                    ConnectivityManager.EXTRA_NETWORK_TYPE,     // only from API level 17
                    -1
            );
            boolean couldBeWifiAction =
                    (networkInfo == null && networkType < 0)    ||      // cases of lack of info
                    networkInfo.getType() == ConnectivityManager.TYPE_WIFI  ||
                    networkType == ConnectivityManager.TYPE_WIFI;

            if (couldBeWifiAction) {
                if (ConnectivityUtils.isAppConnectedViaWiFi(context)) {
                    Log_OC.d(TAG, "WiFi connected");
                    wifiConnected(context);
                } else {
                    Log_OC.d(TAG, "WiFi disconnected");
                    wifiDisconnected(context);
                }
            } /* else, CONNECTIVIY_ACTION is (probably) about other network interface (mobile, bluetooth, ...)
        }
        */
}
Also used : NetworkInfo(android.net.NetworkInfo) Bundle(android.os.Bundle) WifiInfo(android.net.wifi.WifiInfo)

Example 10 with WifiInfo

use of android.net.wifi.WifiInfo in project robolectric by robolectric.

the class ShadowWifiInfoTest method shouldReturnRssi.

@Test
public void shouldReturnRssi() {
    WifiManager wifiManager = (WifiManager) application.getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    // WifiInfo.INVALID_RSSI
    assertThat(wifiInfo.getRssi()).isEqualTo(-127);
    shadowOf(wifiInfo).setRssi(10);
    wifiManager = (WifiManager) application.getSystemService(WIFI_SERVICE);
    wifiInfo = wifiManager.getConnectionInfo();
    assertThat(wifiInfo.getRssi()).isEqualTo(10);
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiInfo(android.net.wifi.WifiInfo) Test(org.junit.Test)

Aggregations

WifiInfo (android.net.wifi.WifiInfo)98 WifiManager (android.net.wifi.WifiManager)52 WifiConfiguration (android.net.wifi.WifiConfiguration)16 NetworkInfo (android.net.NetworkInfo)13 Intent (android.content.Intent)11 IOException (java.io.IOException)8 Test (org.junit.Test)8 Bundle (android.os.Bundle)7 ScanResult (android.net.wifi.ScanResult)6 WifiAssociationTestRunner (com.android.connectivitymanagertest.WifiAssociationTestRunner)6 PendingIntent (android.app.PendingIntent)5 ConnectivityManager (android.net.ConnectivityManager)5 Network (android.net.Network)5 CellIdentityCdma (android.telephony.CellIdentityCdma)5 CellIdentityGsm (android.telephony.CellIdentityGsm)5 CellIdentityLte (android.telephony.CellIdentityLte)5 CellIdentityWcdma (android.telephony.CellIdentityWcdma)5 CellInfo (android.telephony.CellInfo)5 CellInfoCdma (android.telephony.CellInfoCdma)5 CellInfoGsm (android.telephony.CellInfoGsm)5