1

我正在尝试获取 Wifi 的 MAC 地址、BSSID 和 LINK_speed。我注意到即使 Wifi 关闭,所有三个都显示有值。我关闭手机然后再打开,但没有打开wifi。该应用程序在对这三个项目进行任何询问时崩溃,我无法尝试/捕获错误。如果我打开wifi然后关闭我可以询问项目并且应用程序运行良好。它就像 BSSID 和 LINKSPEED 真的是'LAST'BSSID 和'LAST'LINKSPEED。开机后对MAC地址视而不见,但没有打开wifi正常行为?有没有办法捕获错误以防止应用程序强制关闭?

    WifiManager wifi;
    wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifi.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    ipAddress = wifiInfo.getIpAddress();
    //above work no matter whether wifi on or off before

    String BSSID=wifiInfo.getBSSID();  //<<<<< ERRORS FORCE CLOSE IF NOT EVER CONNECTED
    int WifiLinkSpeed=wifiInfo.getLinkSpeed(); printi("link speed",WifiLinkSpeed);  
    String MacAddress=wifiInfo.getMacAddress(); Log.e("MAC address",MacAddress);
    //All three of these FORCE close if WIFI has never been turn on
4

2 回答 2

1

试试这个,

WifiManager wifiManager = (WifiManager) Settings.cntxt.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
strMacAddr = wifiInfo.getMacAddress();
if(strMacAddr==null) {
    strMacAddr = "Unknown MAC";
}
于 2012-06-30T07:59:44.713 回答
0

尽管您确实说过您尝试过 try/catch ...您正在捕获哪些错误/异常?显然不是正确的。

自从我进行任何 Android 编程以来已经有一段时间了,但这就是问题的症结所在……弄清楚当您尝试这些功能时会触发哪个异常。

try {
    // don't declare variables inside block, you'll limit scope
    BSSID=wifiInfo.getBSSID();  //<<<<< ERRORS FORCE CLOSE IF NOT EVER CONNECTED
    WifiLinkSpeed=wifiInfo.getLinkSpeed(); printi("link speed",WifiLinkSpeed);  
    MacAddress=wifiInfo.getMacAddress(); Log.e("MAC address",MacAddress);
    //All three of these FORCE close if WIFI has never been turn on
}
catch { /* something bad happened, handle it */ }
// continue on since all is good in the world today...
于 2012-06-30T07:44:01.780 回答