Android 5G判断

之前需要用到5G网络信息,花了很长时间才去研究怎么判断是否是5G信号,但无论是官方说明的CellInfoNr,CellIdentityNr,CellSignalStrengthNr,最后连一个网络连接callback都用过了,在华为mate30 5G版上拿到的都是LTE(13),真的是心态爆炸。

一次偶然的机会看到ServiceState类,发现它有一个getNrState的方法,查了一下,返回为3的时候就是5G网络,详细代码如下
 

   /**
     * Get the NR 5G state of the mobile data network.
     * @return the NR 5G state.
     * @hide
     */
    public @NRState int getNrState() {
        final NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
                NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
        if (regInfo == null) return NetworkRegistrationInfo.NR_STATE_NONE;
        return regInfo.getNrState();
    }

是一个隐藏的方法,但还是迫不及待的用反射试了一下
 

private boolean is5GConnected(){
       TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        if(Build.VERSION.SDK_INT >= 26){
            ServiceState serviceState = telephonyManager.getServiceState();
            try{
                Method getNrStateMethod = ServiceState.class.getMethod("getNrState");
                getNrStateMethod.setAccessible(true);
                int result = (int)getNrStateMethod.invoke(serviceState);
                MyLog.i("ALeeObj", "值为:" + result);
                if(result == 3){
                    return true;
                }else{
                    return false;
                }
            }catch (Exception e){
                MyLog.i("ALeeObj", e.toString());
            }
        }
        return false;
    }

结果就是那么忧伤,给我整个这个
 

java.lang.NoSuchMethodException: android.telephony.ServiceState.getNrState []

说实话,当时脾气都有点上来了,直接破罐子破摔,我就看看ServiceState这都整一些什么方法,直接就写了
 

Method []methods = ServiceState.class.getDeclaredMethods();
                for(Method method: methods){
                    MyLog.i("ALeeObj", method.getName());
                }

没错,就是把所有的方法都打印出来,结果给我看到了这玩意

看到这个我真的很激动,不会是华为自己弄得一个吧,赶紧把返回类型和传参类型都打印一下,发现没有传参,返回类型就是一个int,最后试了一下,果然,成了,最终代码如下:

private boolean is5GConnected(){
       TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        if(Build.VERSION.SDK_INT >= 29){
            ServiceState serviceState = telephonyManager.getServiceState();
            try{
                Method hwOwnMethod = ServiceState.class.getMethod("getHwNetworkType");
                hwOwnMethod.setAccessible(true);
                int result = (int)hwOwnMethod.invoke(serviceState);
                MyLog.i("ALeeObj", "值为:" + result);
                if(result == 20){
                    return true;
                }else{
                    return false;
                }
            }catch (Exception e){
                MyLog.i("ALeeObj", e.toString());
            }
        }
        return false;
    }

试了一下,值就是官方NetworkType的值,比如nr就是20,lte就是13等等。
说明一下,这个方法只是华为mate30 5G的有效,其他有没效果,我也不清楚,手上只有这一台测试机,而且我怀疑,自己怀疑啊,是华为手机特有的一个方法,所以什么时候失效也不清楚,大家如果要用,最好做一下心理准备。。。
 

发布了24 篇原创文章 · 获赞 2 · 访问量 3665

猜你喜欢

转载自blog.csdn.net/ALee_130158/article/details/103200027
5G