I am writing this tutorial as lot of people are asking me about how to get System properties like what is the version,Model,Hardware Manufacturer,Serial and FingerPrint of a device.As well as many of the visitors also request me to post on how to get ip4/ip6 and MAC Address of Device.
So Today I am creating a simple ListView to show all the list of system properties i mentioned above and a Utility class that will return an Array list of strings to load listView adapter.
OutPut:
Create new Android Project
Project Name: DeviceInformation
//tested from 2.3.3 to current android sdk
Build Target: Android 2.3.3 //or greater than that
Application Name: DeviceInformation
Package Name: com.shaikhhamadali.blogspot.deviceinformation
Create Layout file: activity_device_info
1. code of Layout:
1. code of Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DeviceInfo" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
2. code of activity:
package com.shaikhhamadali.blogspot.deviceinformation;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class DeviceInfo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_info);
//Create instance of Listview and assign reference of control we declared in layout
ListView lv=(ListView)findViewById(R.id.listView1);
//create an instance of ArrayAdapter and pass context,layout,list of items
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
Utility.getlist() );
//assign adapter to listview
lv.setAdapter(arrayAdapter);
}
}
code of UtilityClass:
package com.shaikhhamadali.blogspot.deviceinformation;
import java.util.ArrayList;
import java.util.List;
import java.net.*;
import java.util.*;
import org.apache.http.conn.util.InetAddressUtils;
public class Utility {
public Utility() {
}
public static ArrayList<String> getlist(){
//Create an instance of ArrayList of String.
ArrayList<String> str=new ArrayList<String>();
//add information to list
str.add("Version : "+System.getProperty("os.version"));
str.add("Version Release : "+android.os.Build.VERSION.RELEASE);
str.add("Device : "+android.os.Build.DEVICE);
str.add("Model : "+android.os.Build.MODEL);
str.add("Product : "+android.os.Build.PRODUCT);
str.add("Brand : "+android.os.Build.BRAND);
str.add("Display : "+android.os.Build.DISPLAY);
str.add("CPU_ABI : "+android.os.Build.CPU_ABI);
str.add("CPU_ABI2 : "+android.os.Build.CPU_ABI2);
str.add("Unknown :"+android.os.Build.UNKNOWN);
str.add("HARDWARE : "+android.os.Build.HARDWARE);
str.add("ID : "+android.os.Build.ID);
str.add("Manufecturer : "+android.os.Build.MANUFACTURER);
str.add("Serial : "+android.os.Build.SERIAL);
str.add("Host : "+android.os.Build.HOST);
str.add("FingerPrint : "+android.os.Build.FINGERPRINT);
str.add("User : "+android.os.Build.USER);
str.add("Lan Mac Add : "+getMACAddress("wlan0"));
str.add("ether Add : "+getMACAddress("eth0"));
str.add("ipv4 : "+getIPAddress(true));
str.add("ipv6 : "+getIPAddress(false));
//return list
return str;
}
/**
Returns MAC address of the given interface name like wlan0 or eth0.
@param interfaceName eth0, wlan0 or NULL=use first interface
@return mac address or empty string
*/
public static String getMACAddress(String interfaceName) {
try {
//create instance of List to store List of NetworkInterfaces
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
//iterate to every item of interfaces
for (NetworkInterface intf : interfaces) {
//check only not null interfaces
if (interfaceName != null) {
//if interface matches to the required or not
if (!intf.getName().equalsIgnoreCase(interfaceName)) continue;
}
//get hardware address that is MAC address
byte[] mac = intf.getHardwareAddress();
if (mac==null) return "";
//create instance of StringBuilder
StringBuilder buf = new StringBuilder();
//iterate though every byte to format the address in Particular MAC address format.
for (int idx=0; idx<mac.length; idx++)
buf.append(String.format("%02X:", mac[idx]));
if (buf.length()>0) buf.deleteCharAt(buf.length()-1);
return buf.toString();
}
} catch (Exception ex) { } // for now ignore exceptions
return "";
}
/**
Get IP address from first non-localhost interface
@param ipv4 true=return ipv4, false=return ipv6
@return address or empty string
*/
public static String getIPAddress(boolean useIPv4) {
try {
//create instance of List to store List of NetworkInterfaces
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
//iterate to every item of interfaces
for (NetworkInterface intf : interfaces) {
//create instance of List to store List of InetAddress
List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
//iterate to every item of InetAddress list
for (InetAddress addr : addrs) {
//check that is loop back address
/*Valid IPv4 loopback addresses have the prefix 127/8.
The only valid IPv6 loopback address is ::1.*/
if (!addr.isLoopbackAddress()) {
//get HostAddress
String sAddr = addr.getHostAddress().toUpperCase();
//check that is ipv4
boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
//if ipv4 return address
if (useIPv4) {
if (isIPv4)
return sAddr;
} else {
//or ipv6
if (!isIPv4) {
int delim = sAddr.indexOf('%'); // drop ip6 port suffix
//return ipv6
return delim<0 ? sAddr : sAddr.substring(0, delim);
}
}
}
}
}
} catch (Exception ex) { } // for now ignore Exceptions
return "";
}
}
3. note that:
-
As I ignored exceptions above but Good practice is to always check that or notify that to users regarding the problem.
- Above I have used NetworkInterface that represent a network interface of the local device. An interface is defined by its address and a platform dependent name. The class provides methods to get all information about the available interfaces of the system or to identify the local interface of a joined multicast group.
- Also InetAddress used to get the IPV4 or IPV6 of a device.
- You may be interested in getting paired devices list, get list of sensors, ListView from array and array strings.
As I ignored exceptions above but Good practice is to always check that or notify that to users regarding the problem.
4. conclusion:
- Some information about how to device information.
- Some information about how to get IP and MAC address.
- know how to create an Array Adapter.
- Know how to us NetworkInterface class and InetAddress class.
- Some information about how to device information.
- Some information about how to get IP and MAC address.
- know how to create an Array Adapter.
- Know how to us NetworkInterface class and InetAddress class.
5. About the post:
5. About the post:
- The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
- Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
- Hope you enjoy it!
6. Source Code:
you can download the source code from: GoogleDrive, Github
Cheers,
Hamad Ali Shaikh
- The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
- Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
- Hope you enjoy it!
6. Source Code:
you can download the source code from: GoogleDrive, Github
Cheers,
Hamad Ali Shaikh