Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection Type to include Cellular Network Technology (e.g. 3G, 4G LTE, 5G) #150

Open
sdzhong opened this issue Sep 28, 2023 · 2 comments

Comments

@sdzhong
Copy link
Member

sdzhong commented Sep 28, 2023

Sentry events currently show Connection Type, but only with values wifi, ethernet, cellular, or null. https://github.com/getsentry/sentry-java/blob/5b8a9a6b2d40e03d1e5d0a38237db370c0f6cfd0/sentry-android-core/src/main/java/io/sentry/android/core/internal/util/ConnectivityChecker.java#L195

Extend the connection_type field to include cellular network technology (3G, 4G LTE, and 5G etc.).
image

@brustolin
Copy link

Swift snippet

import Network
import CoreTelephony

func checkNetworkType() {
    let monitor = NWPathMonitor()
    let queue = DispatchQueue(label: "NetworkMonitor")

    monitor.pathUpdateHandler = { path in
        if path.status == .satisfied {
            // Device is connected to the internet
            if path.usesInterfaceType(.cellular) {
                // Cellular connection
                let networkInfo = CTTelephonyNetworkInfo()

                if let carrierType = networkInfo.serviceCurrentRadioAccessTechnology?.values.first {
                    switch carrierType {
                    case CTRadioAccessTechnologyLTE:
                        print("Connected via 4G (LTE)")
                    case CTRadioAccessTechnologyNRNSA, CTRadioAccessTechnologyNR:
                        print("Connected via 5G")
                    default:
                        print("Other cellular network (3G, etc.)")
                    }
                }
            } else if path.usesInterfaceType(.wifi) {
                print("Connected via WiFi")
            }
        } else {
            print("No internet connection")
        }
    }

    monitor.start(queue: queue)
}

@stefanosiano
Copy link
Member

Java code snippet, to use inside our PhoneStateBreadcrumbsIntegration (we can adapt it to other classes)

  @RequiresApi(api = Build.VERSION_CODES.S)
  abstract class TelephonyDisplayInfoListener extends TelephonyCallback implements TelephonyCallback.DisplayInfoListener {}

  @SuppressLint({"MissingPermission", "NewApi"})
  private String checkNetworkType(final @NotNull BuildInfoProvider buildInfoProvider) {
    if (telephonyManager != null) {
      try {
        // We can listen for changes in real time, if needed, only for Android S and above, but no permission required
        if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.S) {
          telephonyManager.registerTelephonyCallback(Executors.newSingleThreadExecutor(), new TelephonyDisplayInfoListener() {
            @Override
            public void onDisplayInfoChanged(@NonNull TelephonyDisplayInfo telephonyDisplayInfo) {
              networkTypeToText(telephonyDisplayInfo.getNetworkType());
            }
          });
        }
        // We can listen for changes in real time, if needed
        if (buildInfoProvider.getSdkInfoVersion() == Build.VERSION_CODES.R) {
          if (Permissions.hasPermission(context, Manifest.permission.READ_PHONE_STATE) || telephonyManager.hasCarrierPrivileges()) {
            telephonyManager.listen(new PhoneStateListener() {
              @Override
              public void onDisplayInfoChanged(@NonNull TelephonyDisplayInfo telephonyDisplayInfo) {
                networkTypeToText(telephonyDisplayInfo.getNetworkType());
              }
            }, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED);
          }
        }

        if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.N && buildInfoProvider.getSdkInfoVersion() < Build.VERSION_CODES.R) {
          if (Permissions.hasPermission(context, Manifest.permission.READ_PHONE_STATE) || telephonyManager.hasCarrierPrivileges()) {
            return networkTypeToText(telephonyManager.getDataNetworkType());
          }
        }

      } catch (Throwable e) {
        options
          .getLogger()
          .log(SentryLevel.INFO, e, "TelephonyManager is not available or ready to use.");
      }
    }
    return "UNKNOWN";
  }

  private String networkTypeToText(int type) {
    switch (type) {
      case NETWORK_TYPE_GSM:
      case NETWORK_TYPE_IDEN:
      case NETWORK_TYPE_GPRS:
      case NETWORK_TYPE_EDGE:
      case NETWORK_TYPE_1xRTT:
        return "2G";
      case NETWORK_TYPE_UMTS:
      case NETWORK_TYPE_CDMA:
      case NETWORK_TYPE_TD_SCDMA:
      case NETWORK_TYPE_EVDO_0:
      case NETWORK_TYPE_EVDO_A:
      case NETWORK_TYPE_EVDO_B:
      case NETWORK_TYPE_EHRPD:
      case NETWORK_TYPE_IWLAN:
        return "3G";
      case NETWORK_TYPE_HSDPA:
      case NETWORK_TYPE_HSUPA:
      case NETWORK_TYPE_HSPA:
      case NETWORK_TYPE_HSPAP:
        return "3.5G";
      case NETWORK_TYPE_LTE:
        return "4G";
      case NETWORK_TYPE_NR:
        return "5G";
      case NETWORK_TYPE_UNKNOWN:
      default:
        return "UNKNOWN";
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs Investigation
Development

No branches or pull requests

6 participants