Java Code Examples for android.net.wifi.WifiManager#getConnectionInfo()

The following examples show how to use android.net.wifi.WifiManager#getConnectionInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NetworkUtil.java    From TvLauncher with Apache License 2.0 7 votes vote down vote up
/**
 * 获取wifi MAC地址
 *
 * @param context
 * @return
 */
public static final String getWIFIMac(Context context) {
    String wifiAdress = "";
    try {
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        if (info == null || info.getMacAddress() == null || info.getMacAddress().isEmpty()) {
            wifiAdress = "";
        } else {
            wifiAdress = info.getMacAddress();
        }
    } catch (Exception err) {
        err.printStackTrace();
    }

    Log.e(TAG, "wifiAdress: " + wifiAdress);
    return wifiAdress;

}
 
Example 2
Source File: ManageReposActivity.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
private void checkIfNewRepoOnSameWifi(NewRepoConfig newRepo) {
    // if this is a local repo, check we're on the same wifi
    if (!TextUtils.isEmpty(newRepo.getBssid())) {
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        String bssid = wifiInfo.getBSSID();
        if (TextUtils.isEmpty(bssid)) { /* not all devices have wifi */
            return;
        }
        bssid = bssid.toLowerCase(Locale.ENGLISH);
        String newRepoBssid = Uri.decode(newRepo.getBssid()).toLowerCase(Locale.ENGLISH);
        if (!bssid.equals(newRepoBssid)) {
            String msg = getString(R.string.not_on_same_wifi, newRepo.getSsid());
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        }
        // TODO we should help the user to the right thing here,
        // instead of just showing a message!
    }
}
 
Example 3
Source File: NetworkHelper.java    From letv with Apache License 2.0 6 votes vote down vote up
public static boolean wifiConnection(Context context, String wifiSSID, String password) {
    WifiManager wifi = (WifiManager) context.getSystemService("wifi");
    String strQuotationSSID = "\"" + wifiSSID + "\"";
    WifiInfo wifiInfo = wifi.getConnectionInfo();
    if (wifiInfo != null && (wifiSSID.equals(wifiInfo.getSSID()) || strQuotationSSID.equals(wifiInfo.getSSID()))) {
        return true;
    }
    List<ScanResult> scanResults = wifi.getScanResults();
    if (scanResults == null || scanResults.size() == 0) {
        return false;
    }
    for (int nAllIndex = scanResults.size() - 1; nAllIndex >= 0; nAllIndex--) {
        String strScanSSID = ((ScanResult) scanResults.get(nAllIndex)).SSID;
        if (wifiSSID.equals(strScanSSID) || strQuotationSSID.equals(strScanSSID)) {
            WifiConfiguration config = new WifiConfiguration();
            config.SSID = strQuotationSSID;
            config.preSharedKey = "\"" + password + "\"";
            config.status = 2;
            return wifi.enableNetwork(wifi.addNetwork(config), false);
        }
    }
    return false;
}
 
Example 4
Source File: XLUtil.java    From MiniThunder with Apache License 2.0 5 votes vote down vote up
public static String getSSID(Context context) {
    if (context == null) {
        XLLog.e(TAG, "getSSID, context invalid");
        return null;
    }
    WifiManager wifiManager = (WifiManager) context.getSystemService("wifi");
    if (wifiManager != null) {
        WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        if (connectionInfo != null) {
            return connectionInfo.getSSID();
        }
    }
    return null;
}
 
Example 5
Source File: MobileInfo.java    From FoodOrdering with Apache License 2.0 5 votes vote down vote up
public static String GetMacAddress(Context context) {
    try {
        WifiManager wifi = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info.getMacAddress();
    } catch (Exception e) {
        return "";
    }

}
 
Example 6
Source File: DeviceWaitingSearch.java    From ShareBox with Apache License 2.0 5 votes vote down vote up
/**
 * 获取本机在Wifi中的IP
 */
private String getOwnWifiIP() {
    WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    if (!wm.isWifiEnabled()) {
        return "";
    }

    // 需加权限:android.permission.ACCESS_WIFI_STATE
    WifiInfo wifiInfo = wm.getConnectionInfo();
    int ipInt = wifiInfo.getIpAddress();
    String ipAddr = int2Ip(ipInt);
    Log.i(TAG, String.format("%s %s: 本机IP=%s",TAG, mDeviceName,ipAddr));
    return int2Ip(ipInt);
}
 
Example 7
Source File: NetworkUtils.java    From BookReader with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前连接wifi的名称
 *
 * @return
 */
public static String getConnectWifiIp(Context context) {
    if (isWifiConnected(context)) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        if (ipAddress == 0) {
            return null;
        }
        return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."
                + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
    }
    return null;
}
 
Example 8
Source File: JacksonActivity.java    From My-MVP with Apache License 2.0 5 votes vote down vote up
@Nullable
    private String getWifiSSID() {

        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);


        if (wifiManager != null && wifiManager.isWifiEnabled()) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            if (wifiInfo != null) {
                NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
                    return wifiInfo.getSSID();
                }
            }
        }

//        Optional.ofNullable(wifiManager).ifPresent((WifiManager wifiManager1) -> {
//            if (wifiManager1.isWifiEnabled()) {
//                WifiInfo wifiInfo = wifiManager1.getConnectionInfo();
//                Optional.ofNullable(wifiInfo).ifPresent((Consumer<WifiInfo>) (WifiInfo wifiInfo1) -> {
//                    NetworkInfo.DetailedState detailedState = WifiInfo.getDetailedStateOf(wifiInfo1.getSupplicantState());
//                    if (detailedState == NetworkInfo.DetailedState.CONNECTED || detailedState == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
//
//                        return wifiInfo1.getSSID();
//                    }
//                });
//            }
//
//
//        });
        return null;
    }
 
Example 9
Source File: NetWorkUtil.java    From Viewer with Apache License 2.0 5 votes vote down vote up
public static String getWifiName(Context context)
{
	ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo wifi_connect = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
	WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
	WifiInfo info = wifi.getConnectionInfo();
	if (wifi_connect.isConnected())
	{
		return info.getSSID().toString().replaceAll("\"", "");
	} else
	{
		return "";
	}
}
 
Example 10
Source File: PhoneUtils.java    From Mobilyzer with Apache License 2.0 5 votes vote down vote up
/** Returns current Wi-Fi SSID, or null if Wi-Fi is not connected. */
private String getWifiCarrierName() {
	WifiManager wifiManager =
			(WifiManager) context.getSystemService(Context.WIFI_SERVICE);
	WifiInfo wifiInfo = wifiManager.getConnectionInfo();
	if (wifiInfo != null) {
		return wifiInfo.getSSID();
	}
	return null;
}
 
Example 11
Source File: NetworkUtils.java    From fangzhuishushenqi with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前连接wifi的名称
 *
 * @return
 */
public static String getConnectWifiIp(Context context) {
    if (isWifiConnected(context)) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        if (ipAddress == 0) {
            return null;
        }
        return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."
                + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
    }
    return null;
}
 
Example 12
Source File: SDKTools.java    From QuickSDK with MIT License 5 votes vote down vote up
/**
 * 获取mac地址
 * @param activity
 * @return
 */
public static String getMacAddress(Activity activity)
{
    WifiManager localWifiManager = (WifiManager)activity.getSystemService("wifi");
    WifiInfo localWifiInfo = localWifiManager == null ? null : localWifiManager.getConnectionInfo();
    if (localWifiInfo != null)
    {
      String str = localWifiInfo.getMacAddress();
      if ((str == null) || (str.equals(""))) {
        str = "null";
      }
      return str;
    }
    return "null";
}
 
Example 13
Source File: NetworkMonitor.java    From android-utilset with Apache License 2.0 5 votes vote down vote up
/**
 * Returns MAC Address of WiFi Adapter<br>
 * Requires READ_PHONE_STATE, ACCESS_NETWORK_STATE and ACCESS_WIFI_STATE
 * permissions<br>
 * 
 * @return String representing MAC Address of WiFi Adapter; Empty String in
 *         some cases such as No WiFi Adapter is installed or WiFi is turned
 *         off.
 */
public String getWifiMacAddress() {
	WifiManager wifiMan = (WifiManager) context
			.getSystemService(Context.WIFI_SERVICE);
	if (wifiMan != null) {
		WifiInfo wifiInf = wifiMan.getConnectionInfo();
		return wifiInf.getMacAddress();
	}
	return "";
}
 
Example 14
Source File: ApplicationContextClient.java    From anetty_client with Apache License 2.0 5 votes vote down vote up
/**
 * 生成Device对象
 * 
 * @param appKey
 * @param appPackage
 * @param deviceId
 * @param imei
 * @return
 */

public Device makeDevice(Context context, String appKey, String appPackage) {

	Device deviceInfo = this.getDeviceInfoByAppPackage(appPackage);
	if (deviceInfo == null || (deviceInfo.getAppKey() != null && !deviceInfo.getAppKey().equals(appKey))) {
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String imei = tm.getDeviceId();

		String macAddress = null;
		WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
		if (null != info) {
			macAddress = info.getMacAddress();
		}

		String deviceId = Md5Util.toMD5(SystemConsts.CHANNEL + appKey + macAddress + imei);
		if (deviceInfo == null) {
			deviceInfo = new Device();
		}
		deviceInfo.setAppKey(appKey);
		deviceInfo.setAppPackage(appPackage);
		deviceInfo.setDeviceId(deviceId);
		deviceInfo.setImei(imei);
		deviceInfo.setIsOnline(DEVICE_OFFLINE);
	}
	return deviceInfo;
}
 
Example 15
Source File: FieldTripServerService.java    From buffer_bci with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    Log.i(TAG, "Buffer Service Running");
    // If no buffer is running.
    if (buffer == null) {

        final int port = intent.getIntExtra("port", 1972);
        // Get Wakelocks

        final PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                C.WAKELOCKTAG);
        wakeLock.acquire();

        final WifiManager wifiMan = (WifiManager) getSystemService(WIFI_SERVICE);
        wifiLock = wifiMan.createWifiLock(C.WAKELOCKTAGWIFI);
        wifiLock.acquire();

        // Create Foreground Notification

        // Get the currently used ip-address
        final WifiInfo wifiInf = wifiMan.getConnectionInfo();
        final int ipAddress = wifiInf.getIpAddress();
        final String ip = String.format(Locale.ENGLISH, "%d.%d.%d.%d",
                ipAddress & 0xff, ipAddress >> 8 & 0xff,
                ipAddress >> 16 & 0xff, ipAddress >> 24 & 0xff);

        // Create notification text
        final Resources res = getResources();
        final String notification_text = String.format(
                res.getString(R.string.notification_text), ip + ":" + port);

        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(res.getString(R.string.notification_title))
                .setContentText(notification_text);

        // Create a buffer and start it.
        if (isExternalStorageWritable()) {
            Log.i(TAG, "External storage is writable");
            long time = Calendar.getInstance().getTimeInMillis();
            File file = getStorageDir("buffer_dump_" + String.valueOf(time));
            buffer = new BufferServer(port, intent.getIntExtra("nSamples", 100),
                    intent.getIntExtra("nEvents", 100), file);
        } else {
            Log.i(TAG, "External storage is sadly not writable");
            Log.w(TAG, "Storage is not writable. I am not saving the data.");
            buffer = new BufferServer(port, intent.getIntExtra("nSamples", 100),
                    intent.getIntExtra("nEvents", 100));
        }
        monitor = new BufferMonitor(this, ip + ":" + port,
                System.currentTimeMillis());
        buffer.addMonitor(monitor);

        // Start the buffer and Monitor
        buffer.start();
        Log.i(TAG, "1");
        monitor.start();
        Log.i(TAG, "Buffer thread started.");

        // Turn this service into a foreground service
        startForeground(1, mBuilder.build());
        Log.i(TAG, "Fieldtrip Buffer Service moved to foreground.");


        if (buffer != null) {
            this.registerReceiver(mMessageReceiver, intentFilter);
            Log.i(TAG, "Registered receiver with buffer:" + buffer.getName());
        }


    }
    return START_NOT_STICKY;
}
 
Example 16
Source File: NetworksUtils.java    From WADB with Apache License 2.0 4 votes vote down vote up
public static String getLocalIPAddress(Context context) {
    WifiManager wifiManger = context.getApplicationContext().getSystemService(WifiManager.class);
    if (wifiManger == null) return intToIp(0);
    WifiInfo wifiInfo = wifiManger.getConnectionInfo();
    return intToIp(wifiInfo.getIpAddress());
}
 
Example 17
Source File: NetworkUtil.java    From ShareBox with Apache License 2.0 4 votes vote down vote up
public static WifiInfo getConnectWifiInfo(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    return wifiManager.getConnectionInfo();
}
 
Example 18
Source File: WifiUtils.java    From SmallGdufe-Android with GNU General Public License v3.0 4 votes vote down vote up
public WifiUtils(Context context) {
    // 取得WifiManager对象
    mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    // 取得WifiInfo对象
    mWifiInfo = mWifiManager.getConnectionInfo();
}
 
Example 19
Source File: CommonUtils.java    From lunzi with Apache License 2.0 4 votes vote down vote up
public static String getMacAddress(Context context) {
    WifiManager wifi = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    return info.getMacAddress();
}
 
Example 20
Source File: PhoneWifiHelper.java    From arcusandroid with Apache License 2.0 2 votes vote down vote up
/**
 * Gets WifiInfo record of the currently active WiFi network or null if no network is currently
 * active or if the connection cannot be determined for some reason.
 *
 * @param context
 * @return A WifiInfo object describing the current network or null if no Wifi network is
 * active to describe.
 */
public static WifiInfo getCurrentWifiNetwork (Context context) {
    final WifiManager wifiManager = getWiFiManager(context);
    return wifiManager == null || wifiManager.getConnectionInfo() == null ?
            null : wifiManager.getConnectionInfo();
}