List of usage examples for android.content Context WIFI_SERVICE
String WIFI_SERVICE
To view the source code for android.content Context WIFI_SERVICE.
Click Source Link
From source file:Main.java
public static String getLocalIpAddress(Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo != null && wifiInfo.getIpAddress() != 0) { return android.text.format.Formatter.formatIpAddress(wifiInfo.getIpAddress()); } else {// w ww .j a v a 2 s.c o m try { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while (en.hasMoreElements()) { NetworkInterface intf = en.nextElement(); Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); while (enumIpAddr.hasMoreElements()) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().indexOf(":") == -1) { String ipAddress = inetAddress.getHostAddress(); if (!TextUtils.isEmpty(ipAddress) && !ipAddress.contains(":")) { return ipAddress; } } } } } catch (SocketException e) { e.printStackTrace(); } } return null; }
From source file:Main.java
/** * Returns true if WiFi connection was established * @param context - used to get connectivity manger * @return true if WiFi connection established, otherwise - false. *///from w ww . j a v a 2 s.c o m public static boolean isWifiConnected(Context context) { final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); final WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo == null) return false; return wifiInfo.getIpAddress() != 0; }
From source file:Main.java
/** * Returns the wi-fi broadcast address//w ww . j a v a 2s. co m * * @param context the app context * @return returns an the broadcast address * @throws IOException */ public static InetAddress getBroadcastAddress(Context context) throws IOException { WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo(); // handle null somehow int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); return InetAddress.getByAddress(quads); }
From source file:Main.java
public static String getWifiMac(Context context) { String macAddr;//from w ww .j a va2s .com WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); macAddr = wifiInfo.getMacAddress(); if (TextUtils.isEmpty(macAddr)) { NetworkInterface networkInterface = null; try { networkInterface = NetworkInterface.getByInetAddress(InetAddress.getByName(getIPAddr())); byte[] hardwareAddress = networkInterface.getHardwareAddress(); macAddr = byte2hex(hardwareAddress); } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } } return macAddr; }
From source file:Main.java
/** * get the local ip address by Android System * /*from w w w. jav a 2s .co m*/ * @param context * the context * @return the local ip addr allocated by Ap */ public static InetAddress getLocalInetAddress(Context context) { WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wm.getConnectionInfo(); int localAddrInt = wifiInfo.getIpAddress(); String localAddrStr = __formatString(localAddrInt); InetAddress localInetAddr = null; try { localInetAddr = InetAddress.getByName(localAddrStr); } catch (UnknownHostException e) { e.printStackTrace(); } return localInetAddr; }
From source file:Main.java
/** * Determines if the device's wifi is currently enabled or not. * // ww w . j av a 2s . co m * @param context A context with which to access wifi system service * @return True if wifi is enabled or enabling, false otherwise */ public static boolean wifiIsEnabled(ContextWrapper context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); int wifiState = wifiManager.getWifiState(); return (wifiState == WifiManager.WIFI_STATE_ENABLED || wifiState == WifiManager.WIFI_STATE_ENABLING); }
From source file:Main.java
public static void reconnectWifi(Context context, int networkId) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (ni != null) { WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (networkId > -1) { wm.enableNetwork(networkId, true); }/*from w w w .j av a 2 s . co m*/ } }
From source file:Main.java
/** * Enables or disables wifi. Requires CHANGE_WIFI_STATE permission. * //from w ww . j a v a 2 s .co m * @param context A context with which to access wifi system service * @param enable True if wifi should be enabled, false if it should be disabled */ public static void enableWifi(ContextWrapper context, boolean enable) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(enable); }
From source file:Main.java
public static int disconnectWifi(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (ni != null && ni.isConnectedOrConnecting()) { WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); int id = wm.getConnectionInfo().getNetworkId(); return wm.disconnect() ? id : -1; }/*from ww w . j a v a 2 s . com*/ return -1; }
From source file:Main.java
public static String getDeviceInfo(Context context) { try {//from w w w. ja v a2 s . c o m org.json.JSONObject json = new org.json.JSONObject(); android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); String device_id = tm.getDeviceId(); android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context .getSystemService(Context.WIFI_SERVICE); String mac = wifi.getConnectionInfo().getMacAddress(); json.put("mac", mac); if (TextUtils.isEmpty(device_id)) { device_id = mac; } if (TextUtils.isEmpty(device_id)) { device_id = android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID); } json.put("device_id", device_id); return json.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }