我有两个类:其中一个是活动类,另一个是非活动类.我调用一个非活动类中的方法来返回mac Adress.
活动类:
public class Control extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
deneme d = new deneme(this); // i has tried (getApplicatonContext)
String x = d.macadress();
Toast.makeText(getApplicationContext(),x,Toast.LENGTH_LONG).show();
}}
和非活动类:
public class deneme {
Context mcontext ;
WifiManager wm;
public deneme(Context mcontext){
this.mcontext = mcontext;
}
public String macadress(){
wm = (WifiManager)mcontext.getSystemService(Context.WIFI_SERVICE);
String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
return m_szWLANMAC;
}}
但该方法返回null.我有权限ACCESS_WIFI_STATE.
最佳答案
如果你的wifi没有在设备上启用,它将返回null作为你的情况,检查是否启用了wifi然后如果它启用返回mac地址否则通知用户启用wifi.
原文链接:https://www.f2er.com/android/430682.htmlpackage com.example.wifitest;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.widget.Toast;
public class TEST {
Context mcontext;
WifiManager wm;
public TEST(Context mcontext) {
this.mcontext = mcontext;
}
public String macadress() {
wm = (WifiManager) mcontext.getSystemService(Context.WIFI_SERVICE);
if (wm.isWifiEnabled()) {
String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
return m_szWLANMAC;
}
else{
Toast.makeText(mcontext,"Please enbale your wifi",Toast.LENGTH_SHORT).show();
return null;
}
}
}