5

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 Answers1

7

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)

Sophoun
  • 81
  • 4
  • @Sophoun Thanks for the answer. This detects if I am on 5G or not, but is there anything from which I can get "cellinfonr" object. GetallCellinfo() api does not return nr details even if I am in 5G coverage. – Akshay Chougule Jul 24 '20 at 22:35
  • 3
    So after a lot of testing, I figured out that we get the "Cellinfonr" object only on NR-SA devices and not on NR-NSA devices. on NR-NSA devices we can detect 5G using the above answer and can get NR signal info from "CellSignalStrengthNr" api. – Akshay Chougule Sep 18 '20 at 15:20
  • @AkshayChougule can you please share some code as to how you get CellSignalStrengthNr, because TelephonyManager returns CellInfoLTE instance when the device is in NR-NSA. – Hashim Ali Oct 06 '20 at 08:33
  • 1
    @HashimAli Use telephonyManager.getSignalStrength(); – Akshay Chougule Oct 21 '20 at 22:45
  • NR state is now masked in Android 12 – Android Jul 23 '21 at 09:51