Method for obtaining equipment status information Mac address and IP address by Android
- 2021-10-27 08:48:47
- OfStack
Preface
During the development of APP, we often encounter scenarios to obtain mobile phone status information, such as obtaining version number during upgrade, collecting mobile phone information when exception occurs, and so on. Some software also determines whether the current user has logged in before according to the Mac address. The method of obtaining the status information of these mobile phones will be described in 11 below.
1 Obtain mobile phone hardware information through build
Use reflection to get Build information, and then get the corresponding field value from build. This applies to getting all build information. Or call the Build class directly to get the field name inside, such as: android. os. Build. MODEL; //Cell phone model. This is a method to obtain the information of a single mobile phone. It is simple and quick to directly call the field of Build to get the corresponding information. Don't forget to add permissions
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
The following is the information corresponding to the fields of Build class
String BOARD The name of the underlying board, like "goldfish". Substrate name
String BOOTLOADER The system bootloader version number.
String BRAND The brand (e.g., carrier) the software is customized for, if any. Brand name
String CPU_ABI The name of the instruction set (CPU type + ABI convention) of native code.
String CPU_ABI2 The name of the second instruction set (CPU type + ABI convention) of native code.
String DEVICE The name of the industrial design. Brand model name, such as millet 4 Correspondence cancro
String DISPLAY A build ID string meant for displaying to the user
String FINGERPRINT A string that uniquely identifies this build. Including manufacturer, device name, system version and many other information
String HARDWARE The name of the hardware (from the kernel command line or /proc).
String HOST
String ID Either a changelist number, or a label like "M4-rc20".
String MANUFACTURER The manufacturer of the product/hardware.
String MODEL The end-user-visible name for the end product.
String PRODUCT The name of the overall product.
String RADIO The radio firmware version number.
String SERIAL A hardware serial number, if available.
String TAGS Comma-separated tags describing the build, like "unsigned,debug".
long TIME Current time, millisecond value
String TYPE The type of build, like "user" or "eng".
String UNKNOWN Value used for when a build property is unknown.
String USER
// Obtain by using reflection build Fields in the class
Field[] fields = Build.class.getDeclaredFields();
// Traversing an array of field names
for (Field field : fields) {
try {
// Set all fields to public Available
field.setAccessible(true);
//filed.get(null) What you get is the device information
haspmap.put(field.getName(), field.get(null).toString());
Log.d("CrashHandler", field.getName() + " : " + field.get(null));
} catch (Exception e) {
}
}
The following is the equipment information corresponding to Xiaomi 4
D/CrashHandler: BOARD : MSM8974
D/CrashHandler: BOOTLOADER : unknown
D/CrashHandler: BRAND : Xiaomi
D/CrashHandler: CPU_ABI : armeabi-v7a
D/CrashHandler: CPU_ABI2 : armeabi
D/CrashHandler: DEVICE : cancro
D/CrashHandler: DISPLAY : MMB29M
D/CrashHandler: FINGERPRINT : Xiaomi/cancro_wc_lte/cancro:6.0.1/MMB29M/V8.1.3.0.MXDCNDI:user/release-keys
D/CrashHandler: HARDWARE : qcom
D/CrashHandler: HOST : c3-miui-ota-bd43
D/CrashHandler: ID : MMB29M
D/CrashHandler: IS_DEBUGGABLE : false
D/CrashHandler: MANUFACTURER : Xiaomi
D/CrashHandler: MODEL : MI 4LTE
D/CrashHandler: PRODUCT : cancro_wc_lte
D/CrashHandler: RADIO : unknown
// Serial number of the device -SERIAL
D/CrashHandler: SERIAL : abcdefgh
D/CrashHandler: SUPPORTED_32_BIT_ABIS : [Ljava.lang.String;@76b6d2b
D/CrashHandler: SUPPORTED_64_BIT_ABIS : [Ljava.lang.String;@e42c588
D/CrashHandler: SUPPORTED_ABIS : [Ljava.lang.String;@9cdbb21
D/CrashHandler: TAG : Build
D/CrashHandler: TAGS : release-keys
D/CrashHandler: TIME : 1478606340000
D/CrashHandler: TYPE : user
D/CrashHandler: UNKNOWN : unknown
D/CrashHandler: USER : builder
2. Get the Ip address through getSystemService ()
Context. getSystemService () This method is a very practical method, only need to input 1 String string constant in the parameters to get the corresponding service management method, which can be used to obtain most of the system information. The corresponding meanings of each constant are as follows.
WINDOW_SERVICE ("window")
The top-level window manager in which you can place custom windows. The returned object is a WindowManager.
LAYOUT_INFLATER_SERVICE ("layout_inflater")
A LayoutInflater for inflating layout resources in this context.
ACTIVITY_SERVICE ("activity")
A ActivityManager for interacting with the global activity state of the system.
POWER_SERVICE ("power")
A PowerManager for controlling power management.
ALARM_SERVICE ("alarm")
A AlarmManager for receiving intents at the time of your choosing.
NOTIFICATION_SERVICE ("notification")
A NotificationManager for informing the user of background events.
KEYGUARD_SERVICE ("keyguard")
A KeyguardManager for controlling keyguard.
LOCATION_SERVICE ("location")
A LocationManager for controlling location (e.g., GPS) updates.
SEARCH_SERVICE ("search")
A SearchManager for handling search.
VIBRATOR_SERVICE ("vibrator")
A Vibrator for interacting with the vibrator hardware.
CONNECTIVITY_SERVICE ("connection")
A ConnectivityManager for handling management of network connections.
WIFI_SERVICE ("wifi")
A WifiManager for management of Wi-Fi connectivity.
WIFI_P2P_SERVICE ("wifip2p")
A WifiP2pManager for management of Wi-Fi Direct connectivity.
INPUT_METHOD_SERVICE ("input_method")
An InputMethodManager for management of input methods.
UI_MODE_SERVICE ("uimode")
An UiModeManager for controlling UI modes.
DOWNLOAD_SERVICE ("download")
A DownloadManager for requesting HTTP downloads
BATTERY_SERVICE ("batterymanager")
A BatteryManager for managing battery state
JOB_SCHEDULER_SERVICE ("taskmanager")
A JobScheduler for managing scheduled tasks
NETWORK_STATS_SERVICE ("netstats")
A NetworkStatsManager for querying network usage statistics.
Note: System services obtained via this API may be closely associated with the Context in which they are obtained from. In general, do not share the service objects between various different contexts (Activities, Applications, Services, Providers, etc.)
Parameters
name
The name of the desired service.
Returns
The service or null if the name does not exist.
To get the IP address, you need to use Context. CONNECTIVITY_SERVICE, the network connection management method corresponding to this constant.
Permissions are required for the following code:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
/** Obtain IP Address, divided into two cases, 1 Yes wifi Under, 2 It is obtained under the mobile network ip Is the address 1 Like */
public static String getIPAddress() {
Context context=MyApp.getContext();
NetworkInfo info = ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {// Current use 2G/3G/4G Network
try {
//Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {// Currently using wireless networks
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
// Calling the method sets the int Convert to address string
String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());// Get IPV4 Address
return ipAddress;
}
} else {
// There is currently no network connection , Please open the network in Settings
}
return null;
}
/**
* What you will get int Type of IP Convert to String Type
* @param ip
* @return
*/
public static String intIP2StringIP(int ip) {
return (ip & 0xFF) + "." +
((ip >> 8) & 0xFF) + "." +
((ip >> 16) & 0xFF) + "." +
(ip >> 24 & 0xFF);
}
3. Get the Mac address
We know that mac address is the only one identification of the network card, through which we can judge how many mobile phones are currently connected to the network. The code is as follows:
public static String getMacAddress(){
/* Get mac The address is 1 The point to note is that android 6.0 Version, the following annotation method is no longer applicable, regardless of any mobile phone will return "02:00:00:00:00:00" This default mac Address, this is googel The official banned it in order to strengthen the authority management getSYstemService(Context.WIFI_SERVICE) Method to get the mac Address. */
// String macAddress= "";
// WifiManager wifiManager = (WifiManager) MyApp.getContext().getSystemService(Context.WIFI_SERVICE);
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
// macAddress = wifiInfo.getMacAddress();
// return macAddress;
String macAddress = null;
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface = null;
try {
networkInterface = NetworkInterface.getByName("eth1");
if (networkInterface == null) {
networkInterface = NetworkInterface.getByName("wlan0");
}
if (networkInterface == null) {
return "02:00:00:00:00:02";
}
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macAddress = buf.toString();
} catch (SocketException e) {
e.printStackTrace();
return "02:00:00:00:00:02";
}
return macAddress;
}
4. Get the mobile phone number, IMEI code
/** Obtain the of the mobile phone IMEI Number */
public static String getPhoneIMEI() {
TelephonyManager mTm = (TelephonyManager) MyApp.getContext().getSystemService(Context.TELEPHONY_SERVICE);
String imei = mTm.getDeviceId();
String imsi = mTm.getSubscriberId();
String mtype = android.os.Build.MODEL; // Mobile phone model
String numer = mTm.getLine1Number(); // Mobile phone numbers, some available and some unavailable
return imei;
}
Summarize