获取设备相关的MAC地址时,WiFi是关闭 [英] Getting the MAC address of the device- when wifi is off

查看:223
本文介绍了获取设备相关的MAC地址时,WiFi是关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的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.

这篇关于获取设备相关的MAC地址时,WiFi是关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆