0

guys I have an activity that gets the ip address of android, but is giving me the wrong ip. I'm testing this in Genymotion as it runs on Virtualbox and gets an ip. http://pctechtips.org/data/ipAddressAndroid.png I'm working on a class that would display tcp/ip configuration for android. but the ip portion I think is wrong. any help appreciated tcpConfigActivity

package com.example.george.droidnet;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;


/**
 * Created by george on 2/7/17.
 */

public class TcpConfigActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.output);

        //reference textView text config
        TextView tcpText = (TextView) findViewById(R.id.text_output);
        tcpText.setText("TCP/IP Configuration: ");

        TextView tcpOutput = (TextView) findViewById(R.id.config_output);
        tcpOutput.setText(getIpConfig());

    }

    public String getIpConfig() {
        WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        int ip = wifiInfo.getIpAddress();
        return Integer.toString(ip);
    }
}

this is the output.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primary_light"
    android:divider="@drawable/vertical_divider1"
    android:orientation="vertical"
    android:showDividers="middle"
    tools:context="com.example.george.droidnet.MainActivity"
    android:weightSum="1">

    <TextView
        android:id="@+id/hostname"
        android:layout_height="100dp"
        android:layout_width="match_parent"
        android:background="@color/primary"
        android:textSize="24sp"
        />

    <TextView
        android:id="@+id/text_output"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TextStyle"
        android:text="output1" />

    <TextView
        android:id="@+id/config_output"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/txtOutStyle"
        android:text="output2"/>

</LinearLayout>
miatech
  • 2,150
  • 8
  • 41
  • 78
  • 1
    Might be what you are looking for: http://stackoverflow.com/a/18638588/291700 – Charles Caldwell Feb 07 '17 at 21:42
  • thanks. looking at the method on your link. why is context passed as parameter in the method protected String wifiIpAddress(Context context) – miatech Feb 09 '17 at 18:15
  • Because the first line of code in the method gets the wifi service. `WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);` If you are using this in an object that already contains context (like an Activity), then you could probably call `getSystemService` without `context`. – Charles Caldwell Feb 09 '17 at 18:17

0 Answers0