android 10以上版本5G信号TAC、PCI、RSRP、RSRQ、SINR等数据获取

前言

之前有个APP用到了获取TAC、PCI、RSRP、RSRQ、SINR等信息,4G网络已经适配过,但是5G信号的情况下会报错,发现是5G信号下,获取的方式变了,但是查了许多相关资料,也没找到相关方法,最后没办法只能看相关源码了,最后在源码里找到了相关方法,用反射获取到了,在这记录下

一、TAC、PCI数据获取

以下CellIdentityNr 也能获取到RSRP、RSRQ、SINR,但是获取到的数据不对,需要用第二点里面的方式获取

//telephonyManager 自己创建一个哈,就不写了
List<CellInfo> allCellInfo = telephonyManager.getAllCellInfo();
if (allCellInfo != null && allCellInfo.size() > 0) {
	CellInfo info = allCellInfo.get(0);
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
		// 5G
		if (info instanceof CellInfoNr) {
			CellIdentityNr nr = (CellIdentityNr) ((CellInfoNr) info)
					.getCellIdentity();
			CellSignalStrengthNr nrStrength = (CellSignalStrengthNr) ((CellInfoNr) info)
					.getCellSignalStrength();
			
			nr.getTac();
			nr.getPci();
			Log.i("nrgsm", "CellIdentityNr-" + nr.toString());
			
		}
	}
}

二、RSRP、RSRQ、SINR数据获取

//获取TelephonyManager 
TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int mPhoneRatType = mTelephonyManager.getNetworkType();
//创建监听
PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
	private String[] mPara = null;

	@SuppressWarnings("unchecked")
	@Override
	public void onSignalStrengthsChanged(SignalStrength sStrength) {

		if (mPhoneRatType == TelephonyManager.NETWORK_TYPE_NR) {/* 5G状态下获取 */

			try {

				Class<?> obj = Class
						.forName("android.telephony.SignalStrength");
				Method method = obj
						.getDeclaredMethod("getCellSignalStrengths");
				method.setAccessible(true);
				List<CellSignalStrength> cellListStrengths = (List<CellSignalStrength>) method
						.invoke(sStrength);
				if (cellListStrengths != null && cellListStrengths.size() > 0) {
					for (CellSignalStrength o : cellListStrengths) {
						if (o instanceof CellSignalStrengthNr) {
							CellSignalStrengthNr nr = (CellSignalStrengthNr) o;
							nr.getSsRsrp();
							nr.getSsRsrq();
							nr.getSsSinr();
						}

					}
				}

				// System.out.println(field.getName()+":"+field.get(obj.newInstance()));
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		if (mPhoneRatType == TelephonyManager.NETWORK_TYPE_LTE) {/* 4G状态下获取 */
			try {
				(Integer) sStrength.getClass()
						.getMethod("getLteRsrp").invoke(sStrength);//LteRsrp
				(Integer) sStrength.getClass()
						.getMethod("getLteRsrq").invoke(sStrength);//LteRsrq
				(Integer) sStrength.getClass()
						.getMethod("getLteRssnr").invoke(sStrength);//LteRssnr
				// serverCellInfo.cqi = (Integer) sStrength.getClass()
				// .getMethod("getLteCqi").invoke(sStrength);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		/* 其他状态下获取 */
		int mGSMSignal = sStrength.getGsmSignalStrength();
		String[] mPara = sStrength.toString().split(" ");
		 Integer.parseInt(mPara[9]);//RSRP
		try {
			Field f = SignalStrength.class.getDeclaredField("mLteRsrq");
			Field f2 = SignalStrength.class
					.getDeclaredField("mLteRssnr");// mLteSignalStrenth
			f.setAccessible(true);
			f2.setAccessible(true);

			 Integer.parseInt(f.get(sStrength).toString());//RSRQ 

			int rsinr = Integer.parseInt(f2.get(sStrength).toString());//SINR
			//适配华为个别手机
			if (!Build.MODEL.contains("HUAWEI MT7")
					&& !Build.MODEL.contains("HUAWEI P7")
					&& !Build.MODEL.contains("HUAWEI D2")
					&& !Build.MODEL.contains("HUAWEI MT2")) {
				mLTESINR = rsinr % 10 != 0 ? rsinr / 10
						: (rsinr / 10) + 1;
			}
			int tempSinr = (Integer) sStrength.getClass()
					.getMethod("getLteRssnr").invoke(sStrength);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

};
// 开始监听
if (mTelephonyManager != null) {
	mTelephonyManager.listen(mPhoneStateListener,
			PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐