Example usage for android.net.wifi WifiManager getConnectionInfo

List of usage examples for android.net.wifi WifiManager getConnectionInfo

Introduction

In this page you can find the example usage for android.net.wifi WifiManager getConnectionInfo.

Prototype

public WifiInfo getConnectionInfo() 

Source Link

Document

Return dynamic information about the current Wi-Fi connection, if any is active.

Usage

From source file:com.iiitd.networking.UDPMessenger.java

/**
 * Sends a broadcast message (TAG EPOCH_TIME message). Opens a new socket in case it's closed.
 * @param message the message to send (multicast). It can't be null or 0-characters long.
 * @return/*  ww w.  j a v a  2 s . c om*/
 * @throws IllegalArgumentException
 */
public boolean sendMessage(String message) throws IllegalArgumentException {
    if (message == null || message.length() == 0)
        throw new IllegalArgumentException();

    // Check for WiFi connectivity
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (mWifi == null || !mWifi.isConnected()) {
        Log.d(DEBUG_TAG,
                "Sorry! You need to be in a WiFi network in order to send UDP multicast packets. Aborting.");
        return false;
    }

    // Check for IP address
    WifiManager wim = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int ip = wim.getConnectionInfo().getIpAddress();

    // Create the send socket
    if (socket == null) {
        try {
            socket = new DatagramSocket();
        } catch (SocketException e) {
            Log.d(DEBUG_TAG, "There was a problem creating the sending socket. Aborting.");
            e.printStackTrace();
            return false;
        }
    }

    // Build the packet
    DatagramPacket packet;
    Message msg = new Message(TAG, message);
    byte data[] = msg.toString().getBytes();

    WifiInfo wifiInfo = wim.getConnectionInfo();
    int ipa = wifiInfo.getIpAddress();
    String ipAddress = Formatter.formatIpAddress(ipa);

    try {
        //         packet = new DatagramPacket(data, data.length, InetAddress.getByName(ipToString(ip, true)), MULTICAST_PORT);
        packet = new DatagramPacket(data, data.length, InetAddress.getByName(ipAddress), MULTICAST_PORT);
    } catch (UnknownHostException e) {
        Log.d(DEBUG_TAG, "It seems that " + ipToString(ip, true) + " is not a valid ip! Aborting.");
        e.printStackTrace();
        return false;
    }

    try {
        socket.send(packet);
    } catch (IOException e) {
        Log.d(DEBUG_TAG, "There was an error sending the UDP packet. Aborted.");
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:com.lge.helloFriendsCamera.MainActivity.java

public boolean checkIsConnectedToDevice() {
    WifiManager wifimanager;
    wifimanager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    WifiInfo info = wifimanager.getConnectionInfo();

    String ssid = info.getSSID();
    Log.d("HERE", " ssid = " + ssid);

    if (ssid.contains(".OSC")) {
        return true;
    } else {//from   ww w  .j  av  a 2s  .c  o m
        return false;
    }
}

From source file:org.deviceconnect.android.deviceplugin.host.setting.HostSettingFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        final Bundle savedInstanceState) {

    // Activity?/*from  w w  w .jav a2 s.  c  o  m*/
    mActivity = this.getActivity();
    mActivity.bindService(new Intent(this.getActivity(), HostDeviceService.class), mServiceConnection,
            Context.BIND_AUTO_CREATE);

    // Position?
    Bundle mBundle = getArguments();
    int mPagePosition = mBundle.getInt("position", 0);

    int mPageLayoutId = this.getResources().getIdentifier("host_setting_" + mPagePosition, "layout",
            getActivity().getPackageName());

    View mView = inflater.inflate(mPageLayoutId, container, false);

    if (mPagePosition == 0) {
        WifiManager wifiManager = (WifiManager) this.getActivity().getSystemService(WIFI_SERVICE);
        int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
        final String formatedIpAddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff),
                (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));

        // Host IP
        mDeviceHostIpTextView = (TextView) mView.findViewById(R.id.host_ipaddress);
        mDeviceHostIpTextView.setText("Your IP:" + formatedIpAddress);
    }

    return mView;
}

From source file:com.wso2.mobile.mdm.api.DeviceInfo.java

/**
*Returns the device WiFi MAC//ww w . j  a  v  a2 s  . co m
*/
public String getMACAddress() {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wInfo = wifiManager.getConnectionInfo();
    mac = wInfo.getMacAddress();
    return mac;
}

From source file:com.fastbootmobile.encore.cast.CastModule.java

private String getWiFiIpAddress() {
    WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    return Formatter.formatIpAddress(ip);
}

From source file:github.daneren2005.dsub.util.Util.java

public static String getSSID(Context context) {
    if (isWifiConnected(context)) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifiManager.getConnectionInfo() != null && wifiManager.getConnectionInfo().getSSID() != null) {
            return wifiManager.getConnectionInfo().getSSID().replace("\"", "");
        }//from  w  ww  .  ja  va2s.  c o m
        return null;
    }
    return null;
}

From source file:com.wifi.brainbreaker.mydemo.spydroid.ui.HandsetFragment.java

private void displayIpAddress() {
    WifiManager wifiManager = (WifiManager) mApplication.getApplicationContext()
            .getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifiManager.getConnectionInfo();
    String ipaddress = null;/*from  ww  w  .j  a v  a  2s  .c om*/
    if (info != null && info.getNetworkId() > -1) {
        int i = info.getIpAddress();
        String ip = String.format(Locale.ENGLISH, "%d.%d.%d.%d", i & 0xff, i >> 8 & 0xff, i >> 16 & 0xff,
                i >> 24 & 0xff);
        mLine1.setText(mHttpServer.isHttpsEnabled() ? "https://" : "http://");
        mLine1.append(ip);
        mLine1.append(":" + mHttpServer.getHttpPort());
        mLine2.setText("rtsp://");
        mLine2.append(ip);
        mLine2.append(":" + mRtspServer.getPort());
        streamingState(0);
    } else if ((ipaddress = Utilities.getLocalIpAddress(true)) != null) {
        mLine1.setText(mHttpServer.isHttpsEnabled() ? "https://" : "http://");
        mLine1.append(ipaddress);
        mLine1.append(":" + mHttpServer.getHttpPort());
        mLine2.setText("rtsp://");
        mLine2.append(ipaddress);
        mLine2.append(":" + mRtspServer.getPort());
        streamingState(0);
    } else {
        streamingState(2);
    }

}

From source file:org.gdg.frisbee.android.widget.FeedbackFragment.java

private void buildProperties() {
    addProperty(PROPERTY_MODEL, Build.MODEL);
    addProperty(PROPERTY_ANDROID_VERSION, VERSION.RELEASE);

    try {//from ww w .  j a v a  2  s.  c o m
        WifiManager cm = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        WifiInfo e = cm.getConnectionInfo();
        SupplicantState mobileDataEnabled = e.getSupplicantState();
        addProperty(PROPERTY_WI_FI_ENABLED, mobileDataEnabled);
    } catch (Exception e) {
        Timber.d(e, "Wifi Manager problem.");
    }

    boolean mobileDataEnabled1 = false;
    ConnectivityManager cm1 = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    try {
        Class e1 = Class.forName(cm1.getClass().getName());
        Method resolution = e1.getDeclaredMethod("getMobileDataEnabled");
        resolution.setAccessible(true);
        mobileDataEnabled1 = (Boolean) resolution.invoke(cm1);
    } catch (Exception e) {
        Timber.d(e, "Mobil data problem.");
    }

    addProperty(PROPERTY_MOBILE_DATA_ENABLED, mobileDataEnabled1);

    try {
        LocationManager e2 = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        boolean resolution1 = e2.isProviderEnabled("gps");
        addProperty(PROPERTY_GPS_ENABLED, resolution1);
    } catch (Exception e) {
        Timber.d(e, "GPS problem.");
    }

    try {
        DisplayMetrics e3 = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(e3);
        String resolution2 = Integer.toString(e3.widthPixels) + "x" + Integer.toString(e3.heightPixels);
        addProperty(PROPERTY_SCREEN_RESOLUTION, resolution2);
    } catch (Exception e) {
        Timber.d(e, "Screen density problem.");
    }

    try {
        String e4 = getActivity().getClass().getSimpleName();
        addProperty(PROPERTY_ACTIVITY, e4);
    } catch (Exception e) {
        Timber.d(e, "Activity name problem.");
    }

    PackageManager manager = getActivity().getPackageManager();
    try {
        PackageInfo e = manager.getPackageInfo(getActivity().getPackageName(), 0);
        addProperty(PROPERTY_APP_VERSION_NAME, e.versionName);
        addProperty(PROPERTY_APP_VERSION_CODE, e.versionCode);
    } catch (PackageManager.NameNotFoundException var7) {
        Timber.d("Problem with PackageManager");
    }
}

From source file:fr.enseirb.odroidx.remote_client.MainActivity.java

public String getLocalIpAddress() {
    WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    String ipBinary = null;/*from  w  w  w  .java  2s  .co  m*/
    try {
        ipBinary = Integer.toBinaryString(wm.getConnectionInfo().getIpAddress());
    } catch (Exception e) {
    }
    if (ipBinary != null) {
        while (ipBinary.length() < 32) {
            ipBinary = "0" + ipBinary;
        }
        String a = ipBinary.substring(0, 8);
        String b = ipBinary.substring(8, 16);
        String c = ipBinary.substring(16, 24);
        String d = ipBinary.substring(24, 32);
        String actualIpAddress = Integer.parseInt(d, 2) + "." + Integer.parseInt(c, 2) + "."
                + Integer.parseInt(b, 2) + "." + Integer.parseInt(a, 2);
        return actualIpAddress;
    } else {
        return null;
    }
}

From source file:org.sociotech.fishification.ui.MainActivity.java

/**
 * Checks for WiFi connection and if SSID is allowed.
 *
 * @return If Wifi connection check was successful.
 */// w  w  w  . j av a  2s . c  o m
private boolean checkWifiConnection() {
    if (!m_wifiCheck) {
        return true;
    }

    ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo.State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    if (wifi == NetworkInfo.State.CONNECTED) {
        if (m_wifiSSIDs.length == 0) {
            return true;
        }

        WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

        String ssid = wifiInfo.getSSID().replace("\"", "").trim();
        for (String s : m_wifiSSIDs) {
            if (ssid.equalsIgnoreCase(s.trim())) {
                return true;
            }
        }

        Toast.makeText(this, "Not connected to allowed WiFi zones.", Toast.LENGTH_LONG);
        return false;
    }

    Toast.makeText(this, "No WiFi connection available.", Toast.LENGTH_LONG);
    return false;
}