Java 以编程方式获取Android设备的MAC
我需要使用Java获取我的android设备的MAC地址。我在网上搜索过,但没有找到任何有用的东西。正如评论中已经指出的,MAC地址可以通过 另外,不要忘记将适当的权限添加到您的Java 以编程方式获取Android设备的MAC,java,android,mac-address,Java,Android,Mac Address,我需要使用Java获取我的android设备的MAC地址。我在网上搜索过,但没有找到任何有用的东西。正如评论中已经指出的,MAC地址可以通过 另外,不要忘记将适当的权限添加到您的AndroidManifest.xml <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 请参阅 为了向用户提供更好的数据保护,从本版本开始,Android将删除使用Wi-Fi和蓝牙API的应
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
请参阅
为了向用户提供更好的数据保护,从本版本开始,Android将删除使用Wi-Fi和蓝牙API的应用程序对设备本地硬件标识符的编程访问。WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法现在返回的常量值为02:00:00:00:00
要通过蓝牙和Wi-Fi扫描访问附近外部设备的硬件标识符,您的应用程序现在必须具有访问\u FINE\u LOCATION或访问\u rough\u LOCATION权限
公共字符串getMacAddress(上下文){
WifiManager wimanager=(WifiManager)context.getSystemService(context.WIFI_SERVICE);
字符串macAddress=wimanager.getConnectionInfo().getMacAddress();
if(macAddress==null){
macAddress=“设备没有mac地址或wi-fi已禁用”;
}
返回macAddress;
}
从Android资源中获取其他方式。这是在系统设置应用程序中显示您的MAC地址的实际代码
private void refreshWifiInfo() {
WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS);
String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress
: getActivity().getString(R.string.status_unavailable));
Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS);
String ipAddress = Utils.getWifiIpAddresses(getActivity());
wifiIpAddressPref.setSummary(ipAddress == null ?
getActivity().getString(R.string.status_unavailable) : ipAddress);
}
您可以获取mac地址:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String mac = wInfo.getMacAddress();
在Menifest.xml中设置权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
通过获取MAC地址在棉花糖和更高版本上不起作用,它已被禁用并将返回。您无法再获取android设备的硬件MAC地址。WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法将返回02:00:00:00:00。这个限制是在Android 6.0中引入的
但是Rob Anderson找到了一个解决方案,它适用于公共静态字符串getMacAddr(){
试一试{
List all=Collections.List(NetworkInterface.getNetworkInterfaces());
用于(网络接口nif:all){
如果(!nif.getName().equalsIgnoreCase(“wlan0”))继续;
byte[]macBytes=nif.getHardwareAddress();
if(macBytes==null){
返回“”;
}
StringBuilder res1=新的StringBuilder();
用于(字节b:macBytes){
追加(String.format(“%02X:”,b));
}
如果(res1.length()>0){
res1.deleteCharAt(res1.length()-1);
}
返回res1.toString();
}
}捕获(例外情况除外){
}
返回“02:00:00:00:00:00”;
}
我从中创建了此解决方案,它正在为我工作!希望有帮助
public static 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) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1)
hex = "0".concat(hex);
res1.append(hex.concat(":"));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
}
return "";
}
public静态字符串getMacAddr(){
试一试{
List all=Collections.List(NetworkInterface.getNetworkInterfaces());
用于(网络接口nif:all){
如果(!nif.getName().equalsIgnoreCase(“wlan0”))继续;
byte[]macBytes=nif.getHardwareAddress();
if(macBytes==null){
返回“”;
}
StringBuilder res1=新的StringBuilder();
用于(字节b:macBytes){
字符串hex=Integer.toHexString(b&0xFF);
如果(十六进制长度()==1)
hex=“0”。concat(十六进制);
res1.append(十六进制concat(“:”);
}
如果(res1.length()>0){
res1.deleteCharAt(res1.length()-1);
}
返回res1.toString();
}
}捕获(例外情况除外){
}
返回“”;
}
它与棉花糖一起工作
package com.keshav.fetchmacaddress;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("keshav","getMacAddr -> " +getMacAddr());
}
public static 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) + ":");
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
//handle exception
}
return "";
}
}
package com.keshav.fetchmacaddress;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入java.net.InetAddress;
导入java.net.NetworkInterface;
导入java.net.SocketException;
导入java.net.UnknownHostException;
导入java.util.Collections;
导入java.util.List;
公共类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(“keshav”,“getMacAddr->”+getMacAddr());
}
公共静态字符串getMacAddr(){
试一试{
List all=Collections.List(NetworkInterface.getNetworkInterfaces());
用于(网络接口nif:all){
如果(!nif.getName().equalsIgnoreCase(“wlan0”))继续;
byte[]macBytes=nif.getHardwareAddress();
if(macBytes==null){
返回“”;
}
StringBuilder res1=新的StringBuilder();
用于(字节b:macBytes){
res1.append(Integer.toHexString(b&0xFF)+“:”;
}
如果(res1.length()>0){
res1.deleteCharAt(res1.length()-1);
}
返回res1.toString();
}
}捕获(例外情况除外){
//处理异常
}
返回“”;
}
}
我想我刚刚找到了一种在没有位置许可的情况下读取MAC地址的方法:运行ip链接
并解析其输出。(您可以通过查看此二进制文件的源代码来执行类似操作)使用此简单方法
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String WLANMAC = wm.getConnectionInfo().getMacAddress();
这个
iplink | grep-a1wlan0
命令在Android 9上工作,我知道这是一个非常老的问题,但还有一种方法可以做到这一点。下面的代码编译,但我还没有尝试过。
您可以编写一些C代码并使用JNI(Java本机接口)获取MAC地址。
以下是示例主活动代码:
package com.example.getmymac;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.TextView;
导入androidx.appcompat.app.appcompat活动;
公共类GetMyMacActivity扩展了AppCompatActivity{
静止的
public static 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) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1)
hex = "0".concat(hex);
res1.append(hex.concat(":"));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
}
return "";
}
package com.keshav.fetchmacaddress;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("keshav","getMacAddr -> " +getMacAddr());
}
public static 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) + ":");
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
//handle exception
}
return "";
}
}
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String WLANMAC = wm.getConnectionInfo().getMacAddress();
cmake_minimum_required(VERSION 3.4.1)
add_library(net-utils SHARED src/main/cpp/net-utils.c)
target_link_libraries(net-utils android log)