Java Code Examples for android.net.wifi.WifiInfo#getMacAddress()

The following examples show how to use android.net.wifi.WifiInfo#getMacAddress() . 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: NetHelper.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
/**
 * 获取mac
 */
public static String getMacString(Context context) {
    String mac = null;
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    boolean bOpenWifi = false;
    int state = wifiManager.getWifiState();
    if (state != WifiManager.WIFI_STATE_ENABLED && state != WifiManager.WIFI_STATE_ENABLING) {
        bOpenWifi = wifiManager.setWifiEnabled(true);

    }
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null) {
        mac = wifiInfo.getMacAddress();
    }
    if (bOpenWifi) {
        wifiManager.setWifiEnabled(false);
    }
    return mac == null ? "" : mac.replace(":", "");
}
 
Example 2
Source File: DeviceUtils.java    From XKnife-Android with Apache License 2.0 6 votes vote down vote up
/**
 * 获取设备MAC地址
 * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>}</p>
 */
@SuppressLint("HardwareIds")
private static String getMacAddressByWifiInfo(Context context) {
    if (context == null) {
        return DEFAULT_MAC_ADDR;
    }
    try {
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifi != null) {
            WifiInfo info = wifi.getConnectionInfo();
            if (info != null) return info.getMacAddress();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return DEFAULT_MAC_ADDR;
}
 
Example 3
Source File: DeviceUtils.java    From Android-utils with Apache License 2.0 5 votes vote down vote up
@SuppressLint({"HardwareIds", "MissingPermission", "WifiManagerLeak"})
private static String getMacAddressByWifiInfo() {
    try {
        WifiManager wifi = (WifiManager) UtilsApp.getApp().getSystemService(Context.WIFI_SERVICE);
        if (wifi != null) {
            WifiInfo info = wifi.getConnectionInfo();
            if (info != null) return info.getMacAddress();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "02:00:00:00:00:00";
}
 
Example 4
Source File: DeviceUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
@SuppressLint({"MissingPermission", "HardwareIds"})
private static String getMacAddressByWifiInfo() {
    try {
        final WifiManager wifi = (WifiManager) Utils.getApp()
                .getApplicationContext().getSystemService(WIFI_SERVICE);
        if (wifi != null) {
            final WifiInfo info = wifi.getConnectionInfo();
            if (info != null) return info.getMacAddress();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "02:00:00:00:00:00";
}
 
Example 5
Source File: DeviceUtils.java    From AndroidWear-OpenWear with MIT License 5 votes vote down vote up
public static String getMacAddress(Context context) {
    String macAddress = null;
    try {
        WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
        if (null != info) {
            macAddress = info.getMacAddress();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
    }
    return macAddress == null ? "" : macAddress;
}
 
Example 6
Source File: DeviceUtils.java    From fangzhuishushenqi with Apache License 2.0 5 votes vote down vote up
/**
 * 获取 MAC 地址
 * 须配置android.permission.ACCESS_WIFI_STATE权限
 */
public static String getMacAddress(Context context) {
    //wifi mac地址
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    String mac = info.getMacAddress();
    LogUtils.i(TAG, " MAC:" + mac);
    return mac;
}
 
Example 7
Source File: SystemUtils.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
private String getMacAddress(){
    String result = "";
    WifiManager wifiManager = (WifiManager) AppMain.getInstance().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    result = wifiInfo.getMacAddress();
    return result;
}
 
Example 8
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 9
Source File: DeviceUtil.java    From DoingDaily with Apache License 2.0 5 votes vote down vote up
/**
 * @param context
 * @return
 */
public static String getWifiMAC(Context context) {
    String mac = null;

    WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    if (wifiManager == null) {
    }
    try {
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        mac = wifiInfo.getMacAddress();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mac;
}
 
Example 10
Source File: Util.java    From Leanplum-Android-SDK with Apache License 2.0 5 votes vote down vote up
@RequiresPermission(ACCESS_WIFI_STATE_PERMISSION)
private static String getWifiMacAddressHash(Context context) {
  String logPrefix = "Skipping wifi device id; ";
  if (context.checkCallingOrSelfPermission(ACCESS_WIFI_STATE_PERMISSION) !=
      PackageManager.PERMISSION_GRANTED) {
    Log.v(logPrefix + "no wifi state permissions.");
    return null;
  }
  try {
    WifiManager manager = (WifiManager) context.getApplicationContext()
        .getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = manager.getConnectionInfo();
    if (wifiInfo == null) {
      Log.i(logPrefix + "null WifiInfo.");
      return null;
    }
    @SuppressLint("HardwareIds")
    String macAddress = wifiInfo.getMacAddress();
    if (macAddress == null || macAddress.isEmpty()) {
      Log.i(logPrefix + "no mac address returned.");
      return null;
    }
    if (Constants.INVALID_MAC_ADDRESS.equals(macAddress)) {
      // Note(ed): this is the expected case for Marshmallow and later, as they return
      // INVALID_MAC_ADDRESS; we intend to fall back to the Android id for Marshmallow devices.
      Log.v(logPrefix + "Marshmallow and later returns a fake MAC address.");
      return null;
    }
    @SuppressLint("HardwareIds")
    String deviceId = md5(wifiInfo.getMacAddress());
    Log.v("Using wifi device id: " + deviceId);
    return checkDeviceId("mac address", deviceId);
  } catch (Exception e) {
    Log.w("Error getting wifi MAC address.");
  }
  return null;
}
 
Example 11
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 12
Source File: Wifi.java    From batteryhub with Apache License 2.0 5 votes vote down vote up
/**
 * Get Wifi MAC ADDR. Hashed and used in UUID calculation.
 */
@SuppressLint("HardwareIds")
public static String getMacAddress(final Context context) {
    if (Build.VERSION.SDK_INT >= 23) return getMacAddressMarshmallow();

    WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (manager == null) return null;

    WifiInfo wifiInfo = manager.getConnectionInfo();
    return (wifiInfo == null) ? null : wifiInfo.getMacAddress();
}
 
Example 13
Source File: UID.java    From cordova-plugin-uid with MIT License 5 votes vote down vote up
/**
 * Get the Media Access Control address (MAC).
 *
 * @param context The context of the main Activity.
 * @return
 */
public String getMac(Context context) {
	final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
	final WifiInfo wInfo = wifiManager.getConnectionInfo();
	String mac = wInfo.getMacAddress();
	return mac;
}
 
Example 14
Source File: WifiDirectManager.java    From ShareBox with Apache License 2.0 5 votes vote down vote up
public String getWifiMacAddress(Context context) {
	WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
    ALog.i(TAG, "getWifiMacAddress macAddress:" + macAddress);
    return macAddress;
}
 
Example 15
Source File: DeviceUidGenerator.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
public static String generate(Context context) throws Exception {
      Monitor.start();
      String uid;
      while (true) {
          TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
          uid = tm.getDeviceId();
          if (!TextUtils.isEmpty(uid)) {
              Monitor.log("from imei");
              break;
          }

          WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
          WifiInfo info = wifi.getConnectionInfo();
          uid = info.getMacAddress();
          if (!TextUtils.isEmpty(uid)) {
              Monitor.log("from mac");
              break;
          }

          if (Build.VERSION.SDK_INT >= 9 && !TextUtils.isEmpty(Build.SERIAL)
                  && !"unknown".equalsIgnoreCase(Build.SERIAL)) {
              uid = Build.SERIAL;
              Monitor.log("from serial id");
              break;
          }

          String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
          if (!TextUtils.isEmpty(androidId) && !BuildConfig.DEVICE_ID.equals(androidId)) {
              uid = androidId;
              Monitor.log("from android id");
              break;
          }

          try {
              uid = fromUuid(context);
              Monitor.log("from uuid");
              break;
          } catch (Exception e) {
              Monitor.finish("Could not generate uid from device!!!");
              throw new RuntimeException("Could not generate uid from device!!!");
          }
      }

/*//uid 服务器长度
if (uid.length() > 14) {
	uid = uid.substring(0, 13);
}*/
      Monitor.finish(uid);
      return uid;
  }
 
Example 16
Source File: VMNetwork.java    From VMLibrary with Apache License 2.0 4 votes vote down vote up
/**
 * 获取当前设备 mac 地址
 *
 * @param context 上下文对象
 */
public static String getMacAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    return wifiInfo.getMacAddress();
}
 
Example 17
Source File: WifiHelper.java    From AndroidBasicProject with MIT License 4 votes vote down vote up
/**
 * 获取mac地址
 * @return
 */
@RequiresPermission(Manifest.permission.ACCESS_WIFI_STATE)
public String getMacAddress(){
    WifiInfo info = mWifiManager.getConnectionInfo();
    return info != null ? info.getMacAddress() : "";
}
 
Example 18
Source File: DatapointHelper.java    From ExtensionsPack with MIT License 4 votes vote down vote up
/**
 * Gets the device's Wi-Fi MAC address.
 * <p>
 * Note: this method will return null if {@link permission#ACCESS_WIFI_STATE} is not available. This method will also return
 * null on devices that do not have Wi-Fi. Even if the application has {@link permission#ACCESS_WIFI_STATE} and the device
 * does have Wi-Fi hardware, this method may still return null if Wi-Fi is disabled or not associated with an access point.
 *
 * @param context The context used to access the Wi-Fi state.
 * @return A SHA-256 of the Wi-Fi MAC address. Null if a MAC is not available, or if {@link permission#ACCESS_WIFI_STATE} is
 *         not available.
 */
public static String getWifiMacHashOrNull(final Context context)
{
    if (Constants.CURRENT_API_LEVEL >= 8)
    {
        final Boolean hasWifi = ReflectionUtils.tryInvokeInstance(context.getPackageManager(), "hasSystemFeature", STRING_CLASS_ARRAY, HARDWARE_WIFI); //$NON-NLS-1$

        if (!hasWifi.booleanValue())
        {
            if (Constants.IS_LOGGABLE)
            {
                Log.i(Constants.LOG_TAG, "Device does not have Wi-Fi; cannot read Wi-Fi MAC"); //$NON-NLS-1$
            }

            return null;
        }
    }

    /*
     * Most applications using the Localytics library probably shouldn't have Wi-Fi permissions. This is primarily for
     * customers who *really* need to identify devices.
     */
    String id = null;
    if (context.getPackageManager().checkPermission(permission.ACCESS_WIFI_STATE, context.getPackageName()) == PackageManager.PERMISSION_GRANTED)
    {
        final WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        final WifiInfo info = manager.getConnectionInfo();
        if (null != info)
        {
            id = info.getMacAddress();
        }
    }
    else
    {
        if (Constants.IS_LOGGABLE)
        {
            /*
             * Yes, this log message is different from the one that reads telephony ID. MAC address is less important than
             * telephony ID and most applications probably don't need the ACCESS_WIFI_STATE permission.
             */
            Log.i(Constants.LOG_TAG, "Application does not have permission ACCESS_WIFI_STATE; determining MAC address is not possible."); //$NON-NLS-1$
        }
    }

    if (null == id)
    {
        return null;
    }

    return getSha256_buggy(id);
}
 
Example 19
Source File: NetworkConnUtil.java    From RairDemo with Apache License 2.0 2 votes vote down vote up
/**
 * 获取Mac地址
 *
 * @param context
 * @return
 */
public static String getLocalMacAddress(Context context) {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    return info.getMacAddress();
}
 
Example 20
Source File: WifiUtils.java    From DevUtils with Apache License 2.0 2 votes vote down vote up
/**
 * 获取 MAC 地址
 * @param wifiInfo {@link WifiInfo}
 * @return MAC 地址
 */
public static String getMacAddress(final WifiInfo wifiInfo) {
    if (wifiInfo == null) return null;
    return wifiInfo.getMacAddress();
}