以编程方式获取Android设备的MAC

我需要使用Java获取我的android设备的MAC地址。我已经在网上搜索过,但没有发现任何有用的信息。


以编程方式获取Android设备的MAC

慕容森
浏览 531回答 4
4回答

哈士奇WWW

正如评论中已经指出的那样,可以通过WifiManager接收MAC地址。WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);WifiInfo info = manager.getConnectionInfo();String address = info.getMacAddress();也不要忘记在您的计算机中添加适当的权限 AndroidManifest.xml<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>请参考Android 6.0更改。为了向用户提供更好的数据保护,从此版本开始,Android删除使用Wi-Fi和Bluetooth API对应用程序对设备本地硬件标识符的编程访问。WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法现在返回常数值02:00:00:00:00:00。要通过蓝牙和Wi-Fi扫描访问附近的外部设备的硬件标识符,您的应用现在必须具有ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限。

繁花不似锦

public static String getMacAddr() {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());&nbsp; &nbsp; &nbsp; &nbsp; for (NetworkInterface nif : all) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!nif.getName().equalsIgnoreCase("wlan0")) continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] macBytes = nif.getHardwareAddress();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (macBytes == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder res1 = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (byte b : macBytes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res1.append(String.format("%02X:",b));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (res1.length() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res1.deleteCharAt(res1.length() - 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return res1.toString();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (Exception ex) {&nbsp; &nbsp; }&nbsp; &nbsp; return "02:00:00:00:00:00";}

守候你守候我

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />public String getMacAddress(Context context) {&nbsp; &nbsp; WifiManager wimanager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);&nbsp; &nbsp; String macAddress = wimanager.getConnectionInfo().getMacAddress();&nbsp; &nbsp; if (macAddress == null) {&nbsp; &nbsp; &nbsp; &nbsp; macAddress = "Device don't have mac address or wi-fi is disabled";&nbsp; &nbsp; }&nbsp; &nbsp; return macAddress;}
打开App,查看更多内容
随时随地看视频慕课网APP