AndroidJavaObject GetTelephonyManager() { AndroidJavaClass unityPlayer = PlatformIntegrationUtil.GetAndroidJavaClass("com.unity3d.player.UnityPlayer"); if (unityPlayer == null) { Debug.Log("Unable to get UnityPlayer"); return(null); } AndroidJavaObject activity = PlatformIntegrationUtil.GetStatic <AndroidJavaObject>(unityPlayer, "currentActivity"); if (activity == null) { Debug.Log("Can't find an activity!"); return(null); } AndroidJavaObject context = PlatformIntegrationUtil.Call <AndroidJavaObject>(activity, "getApplicationContext"); if (context == null) { Debug.Log("Can't find an app context!"); return(null); } // Context.TELEPHONY_SERVICE: string CONTEXT_TELEPHONY_SERVICE = context.GetStatic <string>("TELEPHONY_SERVICE"); if (CONTEXT_TELEPHONY_SERVICE == null) { Debug.Log("Can't get Context Telephony Service"); return(null); } AndroidJavaObject telManager = PlatformIntegrationUtil.Call <AndroidJavaObject>(context, "getSystemService", new object[] { CONTEXT_TELEPHONY_SERVICE }); return(telManager); }
public CarrierInfoClass() { sdkVersion = getAndroidSDKVers(); if (sdkVersion < 0) { Debug.Log("Could not get valid sdkVersion: " + sdkVersion); return; } if (sdkVersion >= 17) { cellInfoLte = PlatformIntegrationUtil.GetAndroidJavaObject("android.telephony.CellInfoLte"); cellInfoLteString = cellInfoLte != null?PlatformIntegrationUtil.GetSimpleName(cellInfoLte) : ""; cellInfoGsm = PlatformIntegrationUtil.GetAndroidJavaObject("android.telephony.CellInfoGsm"); cellInfoGsmString = cellInfoGsm != null?PlatformIntegrationUtil.GetSimpleName(cellInfoGsm) : ""; cellInfoCdma = PlatformIntegrationUtil.GetAndroidJavaObject("android.telephony.CellInfoCdma"); cellInfoCdmaString = cellInfoCdma != null?PlatformIntegrationUtil.GetSimpleName(cellInfoCdma) : ""; } if (sdkVersion >= 18) { cellInfoWcdma = PlatformIntegrationUtil.GetAndroidJavaObject("android.telephony.CellInfoWcdma"); cellInfoWcdmaString = cellInfoWcdma != null?PlatformIntegrationUtil.GetSimpleName(cellInfoWcdma) : ""; } if (sdkVersion >= 28) { cellInfoTdscdma = PlatformIntegrationUtil.GetAndroidJavaObject("android.telephony.CellInfoTdscdma"); cellInfoTdscdmaString = cellInfoTdscdma != null?PlatformIntegrationUtil.GetSimpleName(cellInfoTdscdma) : ""; } if (sdkVersion >= 29) { cellInfoNr = PlatformIntegrationUtil.GetAndroidJavaObject("android.telephony.CellInfoNr"); cellInfoNrString = cellInfoNr != null?PlatformIntegrationUtil.GetSimpleName(cellInfoNr) : ""; } }
public List <KeyValuePair <String, uint> > GetCellInfoList() { if (Application.platform != RuntimePlatform.Android) { Debug.Log("Not on android device."); return(null); } AndroidJavaObject telManager = GetTelephonyManager(); if (telManager == null) { Debug.Log("Can't get telephony manager!"); return(null); } if (!Permission.HasUserAuthorizedPermission(Permission.FineLocation)) { Permission.RequestUserPermission(Permission.FineLocation); } AndroidJavaObject cellInfoList = PlatformIntegrationUtil.Call <AndroidJavaObject>(telManager, "getAllCellInfo"); if (cellInfoList == null) { Debug.Log("Can't get list of cellInfo objects."); return(null); } int length = PlatformIntegrationUtil.Call <int>(cellInfoList, "size"); if (length <= 0) { Debug.Log("Unable to get valid length for cellInfoList"); return(null); } List <KeyValuePair <String, uint> > cellIDList = new List <KeyValuePair <string, uint> >(); // KeyValuePair to compare to in case GetCidKeyValuePair returns nothing KeyValuePair <string, uint> empty = new KeyValuePair <string, uint>(null, 0); for (int i = 0; i < length; i++) { AndroidJavaObject cellInfo = PlatformIntegrationUtil.Call <AndroidJavaObject>(cellInfoList, "get", new object[] { i }); if (cellInfo == null) { continue; } bool isRegistered = PlatformIntegrationUtil.Call <bool>(cellInfo, "isRegistered"); if (isRegistered) { KeyValuePair <string, uint> pair = GetCidKeyValuePair(cellInfo); if (!pair.Equals(empty)) { cellIDList.Add(pair); } } } return(cellIDList); }
KeyValuePair <string, uint> GetCidKeyValuePair(AndroidJavaObject cellInfo) { KeyValuePair <string, uint> pair = new KeyValuePair <string, uint>(null, 0); string simpleName = PlatformIntegrationUtil.GetSimpleName(cellInfo); AndroidJavaObject cellIdentity = PlatformIntegrationUtil.Call <AndroidJavaObject>(cellInfo, "getCellIdentity"); if (cellIdentity == null) { Debug.Log("Unable to get cellIdentity"); return(pair); } if (simpleName.Equals(cellInfoTdscdmaString)) { int cid = PlatformIntegrationUtil.Call <int>(cellIdentity, "getCid"); if (cid > 0) { pair = new KeyValuePair <string, uint>(simpleName, (uint)cid); } } else if (simpleName.Equals(cellInfoNrString)) { int nci = PlatformIntegrationUtil.Call <int>(cellIdentity, "getNci"); if (nci > 0) { pair = new KeyValuePair <string, uint>(simpleName, (uint)nci); } } else if (simpleName.Equals(cellInfoLteString)) { int ci = PlatformIntegrationUtil.Call <int>(cellIdentity, "getCi"); if (ci > 0) { pair = new KeyValuePair <string, uint>(simpleName, (uint)ci); } } else if (simpleName.Equals(cellInfoGsmString)) { int cid = PlatformIntegrationUtil.Call <int>(cellIdentity, "getCid"); if (cid > 0) { pair = new KeyValuePair <string, uint>(simpleName, (uint)cid); } } else if (simpleName.Equals(cellInfoWcdmaString)) { int cid = PlatformIntegrationUtil.Call <int>(cellIdentity, "getCid"); if (cid > 0) { pair = new KeyValuePair <string, uint>(simpleName, (uint)cid); } } else if (simpleName.Equals(cellInfoCdmaString)) { int baseStationId = PlatformIntegrationUtil.Call <int>(cellIdentity, "getBaseStationId"); if (baseStationId > 0) { pair = new KeyValuePair <string, uint>(simpleName, (uint)baseStationId); } } else { Debug.Log("Object is not an instance of a CellInfo class"); } return(pair); }