How do I know if an Android device has a cellular radio module?

How can I know for sure that the device actually has gsm, cdma or other cellular network equipment (and not just WiFi)? I do not want to check the current status of the connected network, because the device may be disconnected at the moment. And I do not want to check the device identifier through ((TelephonyManager) act.getSystemService (Context.TELEPHONY_SERVICE)). GetDeviceId () because some devices simply give you a polymorphic or dummy device identifier.

In fact, I need to check the equipment of the cell precisely to skip TelephonyManager.getDeviceId and make the settings. Check Secure.ANDROID_ID on devices that do not have cellular radio. I have at least one tablet ("Excel 7 scrolling storage options") that returns different IMEIs every time you ask it, although it should return a null value since it does not have cellular radio (same situation here: Android: getDeviceId () returns IMEI, adb shell dumpsys iphonesubinfo returns Device ID = NULL ). But I need to have a reliable device identifier that I ask every time.

I would be happy to hear your thoughts!

+3
source share
2 answers

Just in case, someone needs a complete solution for this: Reflection is used because some things may not be available on some versions of the firmware. MainContext - the main context of the activity.

    static public int getSDKVersion()
{
    Class<?> build_versionClass = null;

    try
    {
        build_versionClass = android.os.Build.VERSION.class;
    }
    catch (Exception e)
    {
    }

    int retval = -1;
    try
    {
        retval = (Integer) build_versionClass.getField("SDK_INT").get(build_versionClass);
    }
    catch (Exception e)
    {
    }

    if (retval == -1)
        retval = 3; //default 1.5

    return retval;
}

static public boolean hasTelephony()
{
    TelephonyManager tm = (TelephonyManager) Hub.MainContext.getSystemService(Context.TELEPHONY_SERVICE);
    if (tm == null)
        return false;

    //devices below are phones only
    if (Utils.getSDKVersion() < 5)
        return true;

    PackageManager pm = MainContext.getPackageManager();

    if (pm == null)
        return false;

    boolean retval = false;
    try
    {
        Class<?> [] parameters = new Class[1];
        parameters[0] = String.class;
        Method method = pm.getClass().getMethod("hasSystemFeature", parameters);
        Object [] parm = new Object[1];
        parm[0] = "android.hardware.telephony";
        Object retValue = method.invoke(pm, parm);
        if (retValue instanceof Boolean)
            retval = ((Boolean) retValue).booleanValue();
        else
            retval = false;
    }
    catch (Exception e)
    {
        retval = false;
    }

    return retval;
}
+1
source

If you publish in the store and want to restrict your application to only visible to real phones, you can add it <uses-feature>to your manifest that it asks for android.hardware.telephony. Check if this works for you from the documentation .

+1
source

Source: https://habr.com/ru/post/1619490/


All Articles