@Override
  public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
      throws JSONException {
    WifiManager wifiMgr =
        (WifiManager) cordova.getActivity().getSystemService(Context.WIFI_SERVICE);
    DhcpInfo ipConfig = wifiMgr.getDhcpInfo();

    if (IpUtils.toIpv4(ipConfig.ipAddress).compareTo("0.0.0.0") != 0) {
      netConfig.put("ipAddress", IpUtils.toIpv4(ipConfig.ipAddress));
    }
    netConfig.put("ipAddress", IpUtils.toIpv4(ipConfig.ipAddress));
    netConfig.put("macAddress", wifiMgr.getConnectionInfo().getMacAddress());
    netConfig.put("netmask", IpUtils.toIpv4(ipConfig.netmask));
    netConfig.put("gateway", IpUtils.toIpv4(ipConfig.gateway));
    netConfig.put("dns1", IpUtils.toIpv4(ipConfig.dns1));
    netConfig.put("dns2", IpUtils.toIpv4(ipConfig.dns1));

    if (netConfig != null) {
      callbackContext.success(netConfig);
      return true;
    } else {
      callbackContext.error("Operation failed");

      return false;
    }
  }
  private void autoSetWifi() {
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    String ip = Formatter.formatIpAddress(wifi.getDhcpInfo().ipAddress);

    Process process;
    String targetStr = "";
    try {
      process = Runtime.getRuntime().exec("su");
      DataOutputStream os = new DataOutputStream(process.getOutputStream());
      os.writeBytes("netcfg\n");
      os.writeBytes("exit\n");
      os.flush();
      BufferedReader mReader =
          new BufferedReader(new InputStreamReader(process.getInputStream()), 1024);

      String line;
      while ((line = mReader.readLine()) != null) {
        if (line.length() == 0) {
          continue;
        }
        if (line.contains(ip)) {
          targetStr = line;
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

    targetStr = targetStr.split(" ")[0];
    targetStr = targetStr.split("\t")[0];
    Logging.updateDebug(targetStr);
    EditText et = (EditText) findViewById(R.id.wifi_edittext);
    et.setText(targetStr);
  }
    public static void populateLanMasks(Context context, String[] names, InterfaceDetails ret) {
      WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
      DhcpInfo dhcp = wifi.getDhcpInfo();

      if (dhcp != null) {
        ret.lanMaskV4 = intToDottedQuad(dhcp.ipAddress) + "/" + intToDottedQuad(dhcp.netmask);
        ret.wifiName = "UNKNOWN";
      }
    }
  // Returns the broadcast address
  InetAddress getBroadcastAddress() throws IOException {
    WifiManager wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();

    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++) quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    return InetAddress.getByAddress(quads);
  }
  private int updateNetworkIDText() {
    //noinspection ConstantConditions
    WifiManager wifi = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
    int gateway = (wifi != null) ? wifi.getDhcpInfo().gateway : 0;
    String networkID = (gateway != 0) ? String.valueOf(gateway) : "n/a";

    //noinspection ConstantConditions
    networkIDText.setText(getActivity().getString(R.string.neighbours_hint_activated, networkID));
    return gateway;
  }
  public static byte[] getBroadcastIPAddressRaw(final Context pContext) throws WifiUtilsException {
    final WifiManager wifiManager = WifiUtils.getWifiManager(pContext);
    final DhcpInfo dhcp = wifiManager.getDhcpInfo();
    // TODO handle null somehow...

    final int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    final byte[] broadcastIP = new byte[IPUtils.IPV4_LENGTH];
    for (int k = 0; k < IPUtils.IPV4_LENGTH; k++) {
      broadcastIP[k] = (byte) ((broadcast >> (k * 8)) & 0xFF);
    }
    return broadcastIP;
  }
 public String getGatewayIP() {
   try {
     WifiManager wifii;
     wifii = (WifiManager) getSystemService(Context.WIFI_SERVICE);
     int gt = wifii.getDhcpInfo().gateway;
     return intToIp(gt);
   } catch (Exception e) {
     Toast.makeText(this, "CS Pinger internal error.", Toast.LENGTH_LONG).show();
     StringWriter errors = new StringWriter();
     e.printStackTrace(new PrintWriter(errors));
     Log.d(LOG_TAG, "Error in isWifiConnected:\n" + errors.toString());
     return "???";
   }
 }
  private static int wifiDhcpGatewayAddress(Context context) {
    if (context == null) {
      return 0;
    }

    try {
      WifiManager mgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
      DhcpInfo d = mgr.getDhcpInfo();
      if (d == null) {
        return 0;
      }

      return d.gateway;

    } catch (Exception ex) {
      // getDhcpInfo() is not documented to require any permissions, but on some devices
      // requires android.permission.ACCESS_WIFI_STATE. Just catch the generic exception
      // here and returning 0. Not logging because this could be noisy.
      return 0;
    }
  }
Exemple #9
0
  public String getIpAddress() {
    Log.d(TAG, "get ip ");
    String ipAddress = null;
    if (getNetworkTypeName().equals("WIFI")) {
      WifiManager wifiManager = (WifiManager) mContext.getSystemService(mContext.WIFI_SERVICE);
      DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
      ipAddress = Formatter.formatIpAddress(dhcpInfo.ipAddress);
      Log.i(TAG, "WIFI_IP_Address: " + ipAddress);
      // } else if (getNetworkTypeName().equals("ETHERNET")) {
    } else if (getNetworkTypeName().equals("Ethernet")) {
      Log.d(TAG, "youxian ip ");
      if (Build.DEVICE.equals("heran")
          || Build.DEVICE.equals("scifly_m202_1G")
          || Build.DEVICE.equalsIgnoreCase("Leader")
          || Build.DEVICE.equalsIgnoreCase("soniq")) {
        Log.d(TAG, "qita  ip");
        LinkProperties linkProperties =
            mConnManager.getLinkProperties(ConnectivityManager.TYPE_ETHERNET);
        if (null == linkProperties) {
          return "0.0.0.0";
        }
        String ipStr = linkProperties.getAddresses().toString();
        ipAddress = ipStr.substring(2, ipStr.length() - 2);
        Log.i(TAG, "ETHERNET_IP_Address: " + ipAddress);

      } else {
        Log.d(TAG, "828ip");
        SharedPreferences settings = mContext.getSharedPreferences("test", mContext.MODE_PRIVATE);
        ipAddress = settings.getString("ip", "0.0.0.0");
        Log.i(TAG, ">>>>>828ipAddress = " + ipAddress);
        // }
      }
    }
    Log.i(TAG, ">>>>>ipAddress = " + ipAddress);
    Log.i(TAG, ">>>>>ipAddress = " + ipAddress);
    return ipAddress;
  }
Exemple #10
0
  public String getGateWay() {
    String gwAddress = null;
    if (getNetworkTypeName().equals("WIFI")) {
      WifiManager wm = (WifiManager) mContext.getSystemService(mContext.WIFI_SERVICE);
      DhcpInfo dhcpInfo = wm.getDhcpInfo();

      gwAddress = Formatter.formatIpAddress(dhcpInfo.gateway);
      Log.i(TAG, "WIFI_GATEWAY_Address: " + gwAddress);
      // } else if (getNetworkTypeName().equals("ETHERNET")) {
    } else if (getNetworkTypeName().equals("Ethernet")) {
      Log.d(TAG, "youxian gateway ");
      if (Build.DEVICE.equals("heran")
          || Build.DEVICE.equals("scifly_m202_1G")
          || Build.DEVICE.equalsIgnoreCase("Leader")
          || Build.DEVICE.equalsIgnoreCase("soniq")) {
        Log.d(TAG, "qita  wangguan");
        LinkProperties linkProperties =
            mConnManager.getLinkProperties(ConnectivityManager.TYPE_ETHERNET);
        if (null == linkProperties) {
          return "";
        }
        String gatewayStr = linkProperties.getRoutes().toString();
        gwAddress = gatewayStr.substring(gatewayStr.length() - 14, gatewayStr.length() - 1);
        if (gwAddress.contains(">")) {
          gwAddress = gwAddress.substring(1, gwAddress.length());
        }
      } else {
        Log.d(TAG, "828wangguan");
        SharedPreferences settings = mContext.getSharedPreferences("test", mContext.MODE_PRIVATE);
        gwAddress = settings.getString("gateway", "0.0.0.0");
      }

      Log.i(TAG, "ETHERNET_GATEWAY_Address :  " + gwAddress);
    }
    return gwAddress;
  }
  // update info bar, speedometer, connection info
  @SuppressWarnings("deprecation")
  private void update() {
    WifiInfo wifiInfo = mWifiManager.getConnectionInfo();

    String ssid = wifiInfo.getSSID();
    if (wifiInfo.getBSSID() == null) {
      ssid = "EXAMPLE";
    }

    double rssi = wifiInfo.getRssi();

    if (rssi < -100) rssi = -100;

    WifiChart wifiChart = new WifiChart();
    wifiChart.init();
    wifiChart.setValues((System.nanoTime() - timeStart) / 1000000000, (int) rssi, ssid);

    viewHolder.mChartChannels.addView(wifiChart.getmChartView(), 0);

    if (wifiInfo.getBSSID() == null) return;

    viewHolder.speedometer.setSpeed(rssi + 100, 1000, 0);
    viewHolder.strengthBarView.setText(
        String.format(
            getResources().getString(R.string.cinf_strength_bar_view), wifiInfo.getRssi()));
    viewHolder.strengthOnProgressBarView.setText(
        String.format(
            getResources().getString(R.string.percent_textView),
            String.valueOf(Utility.convertRssiToQuality(wifiInfo.getRssi()))));
    viewHolder.progressBar.setProgress(Utility.convertRssiToQuality(wifiInfo.getRssi()));
    viewHolder.connectedView.setText(
        String.format(getString(R.string.connected_bar), wifiInfo.getSSID()));

    List<ScanResult> wifiScanList = mWifiManager.getScanResults();

    if (wifiScanList == null) {
      return;
    }

    String cap = "";
    int freq = 1;

    for (int i = 0; i < wifiScanList.size(); i++) {
      if (wifiScanList.get(i).BSSID.equals(wifiInfo.getBSSID())) {
        cap = wifiScanList.get(i).capabilities;
        freq = wifiScanList.get(i).frequency;
      }
    }

    DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo();

    viewHolder.ssidView.setText(wifiInfo.getSSID());
    viewHolder.bssidView.setText(wifiInfo.getBSSID());
    viewHolder.macView.setText(wifiInfo.getMacAddress());
    viewHolder.speedView.setText(
        String.format(getString(R.string.Mbps_textView), wifiInfo.getLinkSpeed()));
    viewHolder.strengthbView.setText(
        String.format(getString(R.string.dBm_textView), wifiInfo.getRssi()));
    viewHolder.encryptionView.setText(Utility.getEncryptionFromCapabilities(cap));
    viewHolder.channelView.setText(String.valueOf(Utility.convertFrequencyToChannel(freq)));
    viewHolder.frequecyView.setText(String.valueOf(freq));
    viewHolder.ipView.setText(Formatter.formatIpAddress(wifiInfo.getIpAddress()));
    viewHolder.netmaskView.setText(Formatter.formatIpAddress(dhcpInfo.netmask));
    viewHolder.gatewayView.setText(Formatter.formatIpAddress(dhcpInfo.gateway));
    viewHolder.dhcpView.setText(Formatter.formatIpAddress(dhcpInfo.serverAddress));
    viewHolder.dns1View.setText(Formatter.formatIpAddress(dhcpInfo.dns1));
    viewHolder.dns2View.setText(Formatter.formatIpAddress(dhcpInfo.dns2));
    viewHolder.dhsp1View.setText(String.valueOf(dhcpInfo.leaseDuration));
  }
  private void refreshWifiInfo() {
    WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
    DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo();

    Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS);
    String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
    wifiMacAddressPref.setSummary(
        !TextUtils.isEmpty(macAddress)
            ? macAddress
            : getActivity().getString(R.string.status_unavailable));

    Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS);
    String ipAddress = Utils.getWifiIpAddresses(getActivity());
    wifiIpAddressPref.setSummary(
        ipAddress == null ? getActivity().getString(R.string.status_unavailable) : ipAddress);
    Preference wifiGateWayPref = findPreference(KEY_GATEWAY_ADDRESS);
    String netGateWayAddress = dhcpInfo.getGateWayValue();
    wifiGateWayPref.setSummary(
        netGateWayAddress == null
            ? getActivity().getString(R.string.status_unavailable)
            : netGateWayAddress);

    Preference wifiNetMaskPref = findPreference(KEY_NETMASK_ADDRESS);
    String netMaskAddress = dhcpInfo.getNetMaskValue();
    wifiNetMaskPref.setSummary(
        netMaskAddress == null
            ? getActivity().getString(R.string.status_unavailable)
            : netMaskAddress);

    Preference wifiDNS1Pref = findPreference(KEY_DNS1_ADDRESS);
    String netDNS1Address = dhcpInfo.getDNS1Value();
    wifiDNS1Pref.setSummary(
        netDNS1Address == null
            ? getActivity().getString(R.string.status_unavailable)
            : netDNS1Address);

    Preference wifiDNS2Pref = findPreference(KEY_DNS2_ADDRESS);
    String netDNS2Address = dhcpInfo.getDNS2Value();
    wifiDNS2Pref.setSummary(
        netDNS2Address == null
            ? getActivity().getString(R.string.status_unavailable)
            : netDNS2Address);

    Preference wifiRssiPref = findPreference(KEY_RSSI);
    // String netRssi = wifiInfo.getRssi()+"";
    String netRssi = mWifiManager.calculateSignalLevel(wifiInfo.getRssi(), 4) + "";
    wifiRssiPref.setSummary(
        netRssi == null ? getActivity().getString(R.string.status_unavailable) : netRssi);

    Preference wifiChNumPref = findPreference(KEY_CHANNELNUM);
    String chNum = wifiInfo.getChannelNumber(wifiInfo.getFrequency()) + "";
    wifiChNumPref.setSummary(
        chNum == null ? getActivity().getString(R.string.status_unavailable) : chNum);

    Preference wifiChFreqPref = findPreference(KEY_CHANNELFREQ);
    String chFreq = wifiInfo.getFrequency() + "";
    wifiChFreqPref.setSummary(
        chFreq == null ? getActivity().getString(R.string.status_unavailable) : chFreq);

    /*Preference wifiBssidPref = findPreference(KEY_BSSID);
          String bssid = wifiInfo.getBSSID();
          wifiBssidPref.setSummary(bssid == null ?
                  getActivity().getString(R.string.status_unavailable) : bssid);
    Preference wifiSsidPref = findPreference(KEY_SSID);
          String ssid = wifiInfo.getSSID();
          wifiSsidPref.setSummary(ssid == null ?
                  getActivity().getString(R.string.status_unavailable) : ssid);
    Preference wifiNetIdPref = findPreference(KEY_NETWORKID);
          String netId = wifiInfo.getNetworkId()+"";
          wifiNetIdPref.setSummary(netId == null ?
                  getActivity().getString(R.string.status_unavailable) : netId);

    Preference wifiPairwisePref = findPreference(KEY_PAIRWISE_CIPHER);
          String pairwise = wifiInfo.getPairwiseCipher();
          wifiPairwisePref.setSummary(pairwise == null ?
                  getActivity().getString(R.string.status_unavailable) : pairwise);
    Preference wifiGroupPref = findPreference(KEY_GROUP_CIPHER);
          String group = wifiInfo.getGroupCipher();
          wifiGroupPref.setSummary(group == null ?
                  getActivity().getString(R.string.status_unavailable) : group);
    Preference wifiMgmtPref = findPreference(KEY_KEY_MGMT);
          String mgmt = wifiInfo.getKeyMgmt();
          wifiMgmtPref.setSummary(mgmt == null ?
                  getActivity().getString(R.string.status_unavailable) : mgmt);
    Preference wifiCurrentRatePref = findPreference(KEY_CURRENT_RATE);
          String currentRate = wifiInfo.getLinkSpeed()+"";
          wifiCurrentRatePref.setSummary(currentRate == null ?
                  getActivity().getString(R.string.status_unavailable) : currentRate);
    Preference wifiCapsPref = findPreference(KEY_PROTOCOL_CAPS);
          String caps = wifiInfo.getGroupCipher();
          wifiCapsPref.setSummary(caps == null ?
                  getActivity().getString(R.string.status_unavailable) : caps);
    Preference wifiWapstatePref = findPreference(KEY_WPA_STATE);
          String wapState = wifiInfo.getWpaState();
          wifiWapstatePref.setSummary(wapState == null ?
                  getActivity().getString(R.string.status_unavailable) : wapState);*/
  }