saumilsdk
saumilsdk

Reputation: 847

How to get mobile network is 4g+ using android programmatically?

I have a galaxy phone with Airtel SIM card (India) and it is showing I am getting 4G+ network in the notification top window. I want to get the same "4G+" value using android somehow. I tried with TelephonyManager and ConnectivityManager but unable to find any methods that returns "4G+". Any help is really really appreciated.

Upvotes: 1

Views: 1507

Answers (3)

iFarbod
iFarbod

Reputation: 639

I have been looking around for a solution as well. Based on my research, it appears that 4G+/LTE+ is not so different from 4G and the plus icon only indicates if it's using carrier aggregation or not.

Due to this, some vendors/carriers do indeed disable this feature in their phones with a special flag called hideLtePlus in Android's code.

Until Android 11's release, there was no way to detect 4G+ on a non-system app (or with some reflections). Now you can use TelephonyDisplayInfo.getOverrideNetworkType. If it returns TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA it means you're on 4G+/LTE+.

You can also do something like this (API 26+):

TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
    ServiceState serviceState = telephony.getServiceState();
    String serviceStateString = serviceState.toString();

    if (serviceStateString.matches("[mI|i]sUsingCarrierAggregation ?= ?true"))
    {
        // You're on 4G+.
    }
}
else
{
    // There's no standard way to detect 4G+ exposed by Android API
}

Do note that you'll need both of ACCESS_COARSE_LOCATION and READ_PHONE_STATE permissions.

Upvotes: 0

noric
noric

Reputation: 56

From my understanding on my Pixel 4A runnging stock A10, the boolean config_hideLtePlus isn't available anymore on A10 (and I guess, A11).

However, I've been able to readd support for 4G+ icon in the status bar in a sort of hackish way, i.e. editing systemuigoogle.apk/smali/com/android/systemui/statusbar/policy/MobileSignalController.smali.

More in-depth, below the line

iget-boolean v0, v0, Lcom/android/systemui/statusbar/policy/NetworkControllerImpl$Config;->hideLtePlus:Z

I've replaced

if-eqz v0, :cond_4

with

if-nez v0, :cond_4

YMMV

EDIT 25/8/2021:

Here is the source code: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android10-release/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java#227

I'm quoting the relevant part:

        if (mConfig.show4gForLte) {
        mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE, TelephonyIcons.FOUR_G);
        if (mConfig.hideLtePlus) {
            mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE_CA,
                    TelephonyIcons.FOUR_G);
        } else {
            mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE_CA,
                    TelephonyIcons.FOUR_G_PLUS);
        }
    } else {
        mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE, TelephonyIcons.LTE);
        if (mConfig.hideLtePlus) {
            mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE_CA,
                    TelephonyIcons.LTE);
        } else {
            mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE_CA,
                    TelephonyIcons.LTE_PLUS);
        }
    }

Basically, what I have achieved is triggering the application of lines 7-8, instead of 4-5.

Upvotes: 1

Mohit Ajwani
Mohit Ajwani

Reputation: 1338

The type is not stored that way, we have standards like 2g, HSDPA, LTE, etc.

        switch(subType){
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false; // ~ 14-64 kbps
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return true; // ~ 400-1000 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return true; // ~ 600-1400 kbps
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false; // ~ 100 kbps
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return true; // ~ 2-14 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPA:
                return true; // ~ 700-1700 kbps
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                return true; // ~ 1-23 Mbps
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true; // ~ 400-7000 kbps
            /*
             * Above API level 7, make sure to set android:targetSdkVersion 
             * to appropriate level to use these
             */
            case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11 
                return true; // ~ 1-2 Mbps
            case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
                return true; // ~ 5 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
                return true; // ~ 10-20 Mbps
            case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
                return false; // ~25 kbps 
            case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
                return true; // ~ 10+ Mbps
            // Unknown
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            default:
                return false;
         }

Please check this answer as it elaborates all the condition scenarios.

https://stackoverflow.com/a/8548926/4491971

Upvotes: 1

Related Questions