コード例 #1
0
ファイル: Util.java プロジェクト: gubo/slipwire
 /**
  * http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
  * http://developer.android.com/training/basics/network-ops/managing.html
  *
  * @param context
  * @return
  */
 public static boolean isNetworkAvailable(final Context context, final boolean mobile) {
   boolean available = false;
   try {
     final ConnectivityManager connectivitymanager =
         (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     final NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
     if ((networkinfo != null) && networkinfo.isConnectedOrConnecting()) {
       switch (networkinfo.getType()) {
         case ConnectivityManager.TYPE_WIFI:
         case ConnectivityManager.TYPE_WIMAX:
         case ConnectivityManager.TYPE_ETHERNET:
           available = true;
           break;
         case ConnectivityManager.TYPE_MOBILE:
           if (mobile) {
             available = true;
           }
           break;
       }
     }
   } catch (Throwable x) {
     DBG.m(x);
   }
   return available;
 }
  @Override
  public LinkQualityInfo getLinkQualityInfo() {
    if (mNetworkInfo == null || mNetworkInfo.getType() == ConnectivityManager.TYPE_NONE) {
      // no data available yet; just return
      return null;
    }

    MobileLinkQualityInfo li = new MobileLinkQualityInfo();

    li.setNetworkType(mNetworkInfo.getType());

    mSamplingDataTracker.setCommonLinkQualityInfoFields(li);

    if (mNetworkInfo.getSubtype() != TelephonyManager.NETWORK_TYPE_UNKNOWN) {
      li.setMobileNetworkType(mNetworkInfo.getSubtype());

      NetworkDataEntry entry = getNetworkDataEntry(mNetworkInfo.getSubtype());
      if (entry != null) {
        li.setTheoreticalRxBandwidth(entry.downloadBandwidth);
        li.setTheoreticalRxBandwidth(entry.uploadBandwidth);
        li.setTheoreticalLatency(entry.latency);
      }

      if (mSignalStrength != null) {
        li.setNormalizedSignalStrength(
            getNormalizedSignalStrength(li.getMobileNetworkType(), mSignalStrength));
      }
    }

    SignalStrength ss = mSignalStrength;
    if (ss != null) {

      li.setRssi(ss.getGsmSignalStrength());
      li.setGsmErrorRate(ss.getGsmBitErrorRate());
      li.setCdmaDbm(ss.getCdmaDbm());
      li.setCdmaEcio(ss.getCdmaEcio());
      li.setEvdoDbm(ss.getEvdoDbm());
      li.setEvdoEcio(ss.getEvdoEcio());
      li.setEvdoSnr(ss.getEvdoSnr());
      li.setLteSignalStrength(ss.getLteSignalStrength());
      li.setLteRsrp(ss.getLteRsrp());
      li.setLteRsrq(ss.getLteRsrq());
      li.setLteRssnr(ss.getLteRssnr());
      li.setLteCqi(ss.getLteCqi());
    }

    if (VDBG) {
      Slog.d(
          TAG,
          "Returning LinkQualityInfo with"
              + " MobileNetworkType = "
              + String.valueOf(li.getMobileNetworkType())
              + " Theoretical Rx BW = "
              + String.valueOf(li.getTheoreticalRxBandwidth())
              + " gsm Signal Strength = "
              + String.valueOf(li.getRssi())
              + " cdma Signal Strength = "
              + String.valueOf(li.getCdmaDbm())
              + " evdo Signal Strength = "
              + String.valueOf(li.getEvdoDbm())
              + " Lte Signal Strength = "
              + String.valueOf(li.getLteSignalStrength()));
    }

    return li;
  }