获取设备相关的MAC地址时,WiFi是关闭地址、设备、MAC、WiFi

由网友(曾经最丑男团)分享简介:我使用下面的code寻找Android设备的MAC地址:I am finding the MAC address of the Android Device using the following code:WifiManager manager = (WifiManager) getSystemService(C...

我使用下面的code寻找Android设备的MAC地址:

I am finding the MAC address of the Android Device using the following code:

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress());

但在这种情况下,我无法得到MAC地址时,无线已关闭。我怎样才能获得Android设备的MAC地址,即使WIFI处于关闭状态。

But in this case I am unable to get the MAC address when the Wifi is off. How can I get the MAC address of the Android Device even when WIFI is off.

感谢

推荐答案

为什么不启用无线片刻,直到你得到的MAC地址,然后将其禁用一旦你完成获取MAC地址?

Why not enable the Wifi momentarily until you get the MAC address and then disable it once you are done getting the MAC address?

当然,这样做是如果获取MAC地址是绝对重要的。的

UNTESTED code

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

if(wifiManager.isWifiEnabled()) {
    // WIFI ALREADY ENABLED. GRAB THE MAC ADDRESS HERE
    WifiInfo info = wifiManager.getConnectionInfo();
    String address = info.getMacAddress();
} else {
    // ENABLE THE WIFI FIRST
    wifiManager.setWifiEnabled(true);

    // WIFI IS NOW ENABLED. GRAB THE MAC ADDRESS HERE
    WifiInfo info = wifiManager.getConnectionInfo();
    String address = info.getMacAddress();
}

您将需要这些权限设置在清单

You will need these permission setup in the Manifest

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

我不能完全肯定,如果 UPDATE_DEVICE_STATS 许可是必要的,这种情况下。请尝试,决定保留它。

I am not entirely sure if the UPDATE_DEVICE_STATS permission is necessary in this case. Please try it out before deciding to keep it.

阅读全文

相关推荐

最新文章