我正在使用以下代码来获取移动设备的mac地址,并且它在android设备上可以正常工作。从stackoverflow获取我的mac地址的代码。但是在MI或Xiaomi手机上,获取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 "";
}