Example usage for android.net.wifi WifiInfo getMacAddress

List of usage examples for android.net.wifi WifiInfo getMacAddress

Introduction

In this page you can find the example usage for android.net.wifi WifiInfo getMacAddress.

Prototype

public String getMacAddress() 

Source Link

Usage

From source file:org.cocos2dx.lib.CCUtils.java

public static String getMacAddress() {
    String mac = "";
    Context ctx = Cocos2dxActivity.getContext();

    // first, try to get mac from wifi manager
    if (ctx.checkCallingPermission(permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED) {
        WifiManager wifi = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        mac = info.getMacAddress();
    }/*  w  w  w .ja  va 2  s.  c o m*/

    // if failed, try from network interface api
    if (TextUtils.isEmpty(mac)) {
        if (Build.VERSION.SDK_INT >= 9) {
            try {
                NetworkInterface ne = NetworkInterface
                        .getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
                byte[] b = ne.getHardwareAddress();
                mac = byte2Hex(b);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // if failed, use fake
    if (TextUtils.isEmpty(mac)) {
        mac = "00:00:00:00:00:00";
    }

    // return
    return mac;
}

From source file:com.tencent.wetest.common.util.DeviceUtil.java

/**
 * ?MAC?/*ww w  .j a v a  2 s.  co m*/
 * @param ctx
 * @return
 */
public static String getMacAddr(Context ctx) {

    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Service.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();

    return wifiInfo.getMacAddress();

}

From source file:com.eurotong.orderhelperandroid.Common.java

public static String GetWifiMacAddress() {
    String id = "UNKNOWN";
    Object idObject = null;/*ww  w  .j a va  2 s. c  o  m*/
    WifiManager wifiMan = (WifiManager) MyApplication.getAppContext().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInf = wifiMan.getConnectionInfo();
    String macAddr = wifiInf.getMacAddress();
    id = macAddr;
    return id;
}

From source file:com.oo58.game.texaspoker.AppActivity.java

public static String getLocalMacAddress() {

    WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    return info.getMacAddress();

}

From source file:com.googlecode.android_scripting.jsonrpc.JsonBuilder.java

private static JSONObject buildJsonWifiInfo(WifiInfo data) throws JSONException {
    JSONObject result = new JSONObject();
    result.put("hidden_ssid", data.getHiddenSSID());
    result.put("ip_address", data.getIpAddress());
    result.put("link_speed", data.getLinkSpeed());
    result.put("network_id", data.getNetworkId());
    result.put("rssi", data.getRssi());
    result.put("bssid", data.getBSSID());
    result.put("mac_address", data.getMacAddress());
    result.put("ssid", data.getSSID());
    String supplicantState = "";
    switch (data.getSupplicantState()) {
    case ASSOCIATED:
        supplicantState = "associated";
        break;//from  w  w  w .ja v a  2s  .c  o  m
    case ASSOCIATING:
        supplicantState = "associating";
        break;
    case COMPLETED:
        supplicantState = "completed";
        break;
    case DISCONNECTED:
        supplicantState = "disconnected";
        break;
    case DORMANT:
        supplicantState = "dormant";
        break;
    case FOUR_WAY_HANDSHAKE:
        supplicantState = "four_way_handshake";
        break;
    case GROUP_HANDSHAKE:
        supplicantState = "group_handshake";
        break;
    case INACTIVE:
        supplicantState = "inactive";
        break;
    case INVALID:
        supplicantState = "invalid";
        break;
    case SCANNING:
        supplicantState = "scanning";
        break;
    case UNINITIALIZED:
        supplicantState = "uninitialized";
        break;
    default:
        supplicantState = null;
    }
    result.put("supplicant_state", build(supplicantState));
    return result;
}

From source file:com.mozilla.SUTAgentAndroid.SUTAgentAndroid.java

public static String getHWID(Context cx) {
    if (sHWID != null)
        return sHWID;

    // If we're on SDK version > 8, use Build.SERIAL
    if (android.os.Build.VERSION.SDK_INT > 8) {
        sHWID = android.os.Build.SERIAL;
    }//from  w ww  . j a v a 2 s  .c  o m

    if (sHWID != null)
        return sHWID;

    // Otherwise, try from the telephony manager
    TelephonyManager mTelephonyMgr = (TelephonyManager) cx.getSystemService(TELEPHONY_SERVICE);
    if (mTelephonyMgr != null) {
        sHWID = mTelephonyMgr.getDeviceId();
    }

    if (sHWID != null)
        return sHWID;

    // Otherwise, try WIFI_SERVICE and use the wifi manager
    WifiManager wifiMan = (WifiManager) cx.getSystemService(Context.WIFI_SERVICE);
    if (wifiMan != null) {
        WifiInfo wifi = wifiMan.getConnectionInfo();
        if (wifi != null) {
            sHWID = "wifimac" + wifi.getMacAddress();
        }
    }

    if (sHWID != null)
        return sHWID;

    sHWID = "0011223344556677";

    return sHWID;
}

From source file:com.xdyou.sanguo.GameSanGuo.java

public static String getMacID() {
    String str = "mac_address";

    WifiManager wifi = (WifiManager) mActivity.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    str = info.getMacAddress();

    // ??imei?/* w w w  .  j a v a 2  s .  c om*/
    if (str == null) {
        TelephonyManager tm = (TelephonyManager) mActivity.getSystemService(TELEPHONY_SERVICE);
        str = tm.getDeviceId();
    }
    Log.e("mac_address", str);
    return str;
}

From source file:io.v.positioning.BluetoothPositionActivity.java

private void addPositionRecord(String deviceName, String deviceAddress, String rssi) {
    JSONObject data = new JSONObject();
    // If user didn't set a device name
    if (deviceName == null) {
        deviceName = "name missing";
    }/*from   w w  w  .j  ava 2 s  .  c  o  m*/
    try {
        data.put("remoteName", deviceName);
        data.put("remoteAddress", deviceAddress);
        data.put("remoteRssi", rssi);
        String androidId = Settings.Secure.getString(this.getBaseContext().getContentResolver(),
                Settings.Secure.ANDROID_ID);
        data.put("androidId", androidId);
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo wInfo = wifiManager.getConnectionInfo();
        data.put("myMacAddress", wInfo.getMacAddress());
        data.put("deviceTime", System.currentTimeMillis());
        new ServletPostAsyncTask("bluetooth", data).execute(this);
        Log.d(TAG, "Added " + androidId);
    } catch (MalformedURLException e) {
        Log.e(TAG, "Failed to create ServletPostAsyncTask." + e);
    } catch (Exception e) {
        Log.e(TAG, "Failed to add a record to GAE. " + e);
    }
}

From source file:org.hygieiasoft.cordova.uid.UID.java

/**
 * Get the Media Access Control address (MAC).
 *
 * @param context The context of the main Activity.
 * @return//from   w ww.  j  a  v  a  2s  . c  o m
 */
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;
}

From source file:org.zywx.wbpalmstar.platform.push.mqttpush.PushGetData2.java

private String getMacAddress() {
    WifiManager wifi = (WifiManager) mCtx.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    return info.getMacAddress().replaceAll(":", "");
}