public static String getMac(Context context){ String mac = ""; if(Build.VERSION.SDK_INT /** * Android 6.0 之前(不包括6.0)获取mac地址 * 必须的权限 * @param context * @return */ private static String getMacDefault(Context context) { String mac = ""; if (cOntext== null) { return mac; } WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); WifiInfo info = null; try { info = wifi.getConnectionInfo(); } catch (Exception e) { e.printStackTrace(); } if (info == null) { return null; } mac = info.getMacAddress(); if (!TextUtils.isEmpty(mac)) { mac = mac.toUpperCase(Locale.ENGLISH); } return mac; } /** * Android 6.0-Android 7.0 获取mac地址 */ private static String getMacAddress() { String macSerial = null; String str = ""; try { Process pp = Runtime.getRuntime().exec("cat/sys/class/net/wlan0/address"); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); while (null != str) { str = input.readLine(); if (str != null) { macSerial = str.trim();//去空格 break; } } } catch (IOException ex) { // 赋予默认值 ex.printStackTrace(); } return macSerial; }/** * Android 7.0之后获取Mac地址 * 遍历循环所有的网络接口,找到接口是 wlan0 * 必须的权限 * @return */ private static String getMacFromHardware() { try { List 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(String.format("%02X:", b)); } if (res1.length() > 0) { res1.deleteCharAt(res1.length() - 1); } return res1.toString(); } } catch (Exception e) { e.printStackTrace(); } return "02:00:00:00:00:00"; } //获取序列号,需要自己去申请权限 if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE,Manifest.permission.READ_PRECISE_PHONE_STATE}, 1);}else { getSerialNumber();} public static String getSerialNumber(){ String serialNumber = ""; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ serialNumber = Build.getSerial(); }else { serialNumber = Build.SERIAL; } return serialNumber;}