我无法获取mac地址
请有人帮帮我
我是android开发新手请帮帮我
manifest.xml
<receiver
android:name="com.example.v3rlu.authentification.DeviceAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/xml" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
启动活动检查
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check);
mac=(TextView) findViewById(R.id.textView) ;
DeviceAdminReceiver admin = new DeviceAdminReceiver();
DevicePolicyManager devicepolicymanager =
admin.getManager(getApplicationContext());
ComponentName name1 = admin.getWho(getApplicationContext());
if (devicepolicymanager.isAdminActive(name1)){
mac_address = devicepolicymanager.getWifiMacAddress(name1);}
mac.setText(mac_address);
}}
receiverclass 我从android site
获取代码答案 0 :(得分:2)
试试这个。
public String getMacAddr() {
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
//res1.append(Integer.toHexString(b & 0xFF) + ":");
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
}
return "";
}
在清单
中添加互联网权限<uses-permission android:name="android.permission.INTERNET"/>
答案 1 :(得分:0)
尝试使用WifiManager获取mac地址。请尝试以下代码。
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();
在清单中添加权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>