Thursday 10 November 2016

Device Information in Android

If you are working with any app that needs android device information, then don't worry. Getting device information is quite easy. Although there are several solutions available, but may be in pieces. I am developing a Mobile Device Management (MDM) app for which I've to gather device information. So, I decided to put everything at one place which may be useful for my fellow developers. Here is implementation of explicit information about any Android Device i.e. Phone, Tablet, Phone / Tablet with or without SIM.

private String sim_serial = "", sim_carrier = "", sim_country = "", device_manufacturer = "", device_model = "";
private String device_imei = "", device_serial = "", device_manufacturer_serial = "", device_OS_version = "";


//Get Device Info
 private void getDeviceInformation(){

        try{

            TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            boolean isSim = checkSIMCard();
            if(isSim){

                sim_serial =tm.getSimSerialNumber();
                sim_carrier=tm.getNetworkOperatorName();
                sim_country = tm.getSimCountryIso();
            }
            device_model = Build.MODEL;
            device_serial = Build.SERIAL;
            device_imei =tm.getDeviceId();
            device_manufacturer_serial = getManufacturerSerial();
            device_manufacturer = Build.MANUFACTURER;
            device_OS_version = android.os.Build.VERSION.RELEASE;
        }
        catch (Exception e){

            e.printStackTrace();
        }
    }

    //To check availability of SIM Card
    private boolean checkSIMCard(){

        TelephonyManager tm= (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        int simState = tm.getSimState();
        boolean isSIM = false;

        switch (simState) {

            case TelephonyManager.SIM_STATE_ABSENT:
                isSIM = false;
                break;

            case TelephonyManager.SIM_STATE_READY:
                isSIM = true;
                break;
        }
        return isSIM;
    }

    //Get manufacturer serial number, need in case of a non GSM Tablet.
    private String getManufacturerSerial() {

        String serial = null;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            serial = (String) get.invoke(c, "ril.serialnumber", "unknown");
        } catch (Exception e) {

            e.printStackTrace();
        }
        return serial;
    }

Thank you for visiting my blog.
Cheers!

No comments:

Post a Comment