How to detect Samsung S10 5G is running on 5G network?

前端 未结 5 753
一整个雨季
一整个雨季 2021-01-07 01:24

Android Q added a new network type, NETWORK_TYPE_NR, for 5G, it is not available for Android Pie. Yet the just released Samsung S10 5G has fully supported 5G function, it ca

相关标签:
5条回答
  • 2021-01-07 01:46

    I believe they backported the code from Q to Pie as the logic for 5G was implemented at the end of last year in Q (alpha). So when using TelephonyManager.getNetworkType()

    you will likely get

    20 (5G)

    EDIT

    As per comment below: The network type will be 13 so it doesn't solve the thing.

    EDIT

    Try using reflection

    static boolean isNRConnected(TelephonyManager telephonyManager) {
        try {
            Object obj = Class.forName(telephonyManager.getClass().getName())
                    .getDeclaredMethod("getServiceState", new Class[0]).invoke(telephonyManager, new Object[0]);
    
            Method[] methods = Class.forName(obj.getClass().getName()).getDeclaredMethods();
    
            for (Method method : methods) {
                if (method.getName().equals("getNrStatus") || method.getName().equals("getNrState")) {
                    method.setAccessible(true);
                    return ((Integer) method.invoke(obj, new Object[0])).intValue() == 3;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    0 讨论(0)
  • 2021-01-07 01:46

    as @Hunter suggested you need to use "listen" from telephony manager but if it's api 30+ you need to have READ_PHONE permission granted and listen for LISTEN_DISPLAY_INFO_CHANGED and override onDisplayInfoChanged from PhoneStateListener

    (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).listen(customPhoneStateListener,PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
    

    The listener should be something like this:

    private  class CustomPhoneStateListener : PhoneStateListener() {
        override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
                   
                    super.onDisplayInfoChanged(telephonyDisplayInfo)
                  
                        when (telephonyDisplayInfo.overrideNetworkType) {
                             //5G
                            OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO,
                            OVERRIDE_NETWORK_TYPE_NR_NSA,
                            OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE -> setNetworkChange(NETWORK_TYPE_NR)
                            OVERRIDE_NETWORK_TYPE_LTE_CA -> {
                                setNetworkChange(19) //LTE+
                            }
                            else -> setNetworkChange(telephonyDisplayInfo.networkType)
                        }
                    } else {
                        setNetworkChange(telephonyDisplayInfo.networkType)
                    }
                }
    
       }
    

    Link: https://developer.android.com/about/versions/11/features/5g#detection

    0 讨论(0)
  • 2021-01-07 02:01

    There is an official documentation to detect 5G on Android 11. https://developer.android.com/about/versions/11/features/5g#detection

    Call TelephonyManager.listen(), passing in LISTEN_DISPLAY_INFO_CHANGED, to determine if the user has a 5G network connection.

    0 讨论(0)
  • 2021-01-07 02:06

    I have extracted the ServiceState.java from SM-G977N firmware and it confirms that they have added ServiceState.getNrStatus()

    5G(NR) is active is if NetworkRegistrationState.NR_STATUS_CONNECTED = 3;

    0 讨论(0)
  • Couldn't add this as a comment, but as @Pavel Machala said, looking at the ServiceState class in the AOSP yields the following:

    /**
     * Get the NR 5G status of the mobile data network.
     * @return the NR 5G status.
     * @hide
     */
    public @NRStatus int getNrStatus() {
        final NetworkRegistrationState regState = getNetworkRegistrationState(
                NetworkRegistrationState.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
        if (regState == null) return NetworkRegistrationState.NR_STATUS_NONE;
        return regState.getNrStatus();
    }
    
    0 讨论(0)
提交回复
热议问题