Detect 5G NR (SA/NSA) in my Android Application

4.2k Views Asked by At

I am trying to detect 5G network. I use the telephony manager to get NETWORK_TYPE. Even if I am in 5G network coverage and my phone shows 5G, I do not get NETWORK_TYPE_NR. The NETWORK_TYPE is always 13 i.e. LTE. The Phones Engineering service mode shows NR related data. Is there any way to detect NR (Standalone or Non-Standalone) mode?

I also need to get the Cell Information for NR data. I use telephonyManager.getAllCellinfo(), but I never get an instance of cellinfonr.

Any help is appreciated. Thanks

1

There are 1 best solutions below

5
On

I faced the same problem for a few weeks ago. In my case, I want to detect 5G network on Galaxy S20-5G but the getDataNetworkType() always return 13 NETWORK_TYPE_LTE. Following by netmonster-core strategy, and here is the code that I extract from them to solve my problem.

public boolean isNRConnected(TelephonyManager telephonyManager) {
    try {
        Object obj = Class.forName(telephonyManager.getClass().getName())
                .getDeclaredMethod("getServiceState", new Class[0]).invoke(telephonyManager, new Object[0]);
        // try extracting from string
        String serviceState = obj.toString();
        boolean is5gActive = serviceState.contains("nrState=CONNECTED") ||
                serviceState.contains("nsaState=5") ||
                (serviceState.contains("EnDc=true") &&
                        serviceState.contains("5G Allocated=true"));
        if (is5gActive) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

Here is full detector class from netmonster-core: (DetectorLteAdvancedNrServiceState.kt)