我正在使用以下代码获取移动设备的 mac 地址,并且它在 android 设备上运行良好。我从 stackoverflow 获得的获取 mac 地址的代码。但是在 MI 或小米手机上获取 mac 地址不起作用..请指导。下面是我的代码。用户通过输入他的手机号码来访问该应用程序,并将在 url 中发送 mac 地址
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void onClick(View view) {
ConnectivityManager ConnectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = ConnectionManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected() == true) {
//get data from screen
getMacAddr();
mobile = mobile_no.getText().toString();
String url = "https://sugarapp.web4rest.com/verify_user.php?mobile="+mobile+"&mac="+mac_addr;
if(mobile.isEmpty() || !mobile.matches(MOBILE_PATTERN)) {
builder.setTitle("Something went wrong!!");
displayAlert("You have not entered Mobile Number");
}else {
final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please Wait...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
progressDialog.dismiss();
JSONObject jobject = new JSONObject(response);
if (Integer.parseInt(jobject.getString("status")) == 200) {
Toast.makeText(getApplicationContext(), "Login Success", Toast.LENGTH_LONG).show();
mobile_no.setText("");
port_no.setText("");
session.setLoggedin(true);
getData(); //Shared p call
Intent i = new Intent(MainActivity.this, ReportActivity.class);
startActivity(i);
finish();
} else {
Toast.makeText(getApplicationContext(), "Login Unsuccessfull", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
progressDialog.dismiss();
Log.e("Error", "Login Failed" + e.toString());
e.printStackTrace();
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT);
error.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parmams = new HashMap<String, String>();
parmams.put("mobile_no", mobile);
parmams.put("mac", mac_addr);
return parmams;
}
};
MySingleton.getInstance(MainActivity.this).addToRequestque(stringRequest);
}
}else {
Toast.makeText(MainActivity.this, "Network Unavailable", Toast.LENGTH_LONG).show();
}
}
});
}
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) + ":");
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
mac_addr = res1.toString();
return mac_addr;
}
} catch (Exception ex) {
//handle exception
}
return "";
}