我正在尝试获取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
答案 0 :(得分:1)
试试这个,
WifiManager wifiManager = (WifiManager) Settings.cntxt.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
strMacAddr = wifiInfo.getMacAddress();
if(strMacAddr==null) {
strMacAddr = "Unknown MAC";
}
答案 1 :(得分: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...