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.example.android.classical.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    GlobalVar.currentDeviceMacAddress = wm.getConnectionInfo().getMacAddress();
    IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    registerReceiver(mReceiver, intentFilter);
    intentFilter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    registerReceiver(mReceiver, intentFilter);
    intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, intentFilter);

    if (savedInstanceState == null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        BluetoothChatFragment fragment = new BluetoothChatFragment();
        transaction.replace(R.id.sample_content_fragment, fragment);
        transaction.commit();/* w  w  w . j  a  v  a2s .  c o  m*/
    }

    //new Thread(){
    //    @Override public void run() {
    //        while(true) {
    //            Intent intentService = new Intent(MainActivity.this, TaskIntentService.class);
    //            intentService.putExtra(TaskIntentService.MESSAGE_TYPE, MessageType.toInt(MessageType.BALL_EVENT));
    //            intentService.putExtra(TaskIntentService.EVENT_TYPE, EventType.BALL_CONNECTED);
    //            startService(intentService);
    //            SystemClock.sleep(2000);
    //        }
    //    }
    //}.start();

}

From source file:com.tbay.android.tcpclient.MainActivity.java

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

    // Initialize text fragment that displays intro text.
    SimpleTextFragment introFragment = (SimpleTextFragment) getSupportFragmentManager()
            .findFragmentById(R.id.intro_fragment);
    introFragment.setText(R.string.intro_message);
    introFragment.getTextView().setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16.0f);

    // Initialize the logging framework.
    initializeLogging();/*w w w. j a  v a  2  s .  c o m*/

    // Initialize the source IP string

    WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);

    try {
        int ip = wm.getConnectionInfo().getIpAddress();

        TextView SrcIp = (TextView) findViewById(R.id.textView6);

        String ip2 = String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff),
                (ip >> 24 & 0xff));
        SrcIp.setText(ip2.toCharArray(), 0, ip2.length());
    } catch (Exception e) {
        String S = e.toString(); // Just for debug
    }

}

From source file:com.example.android.nfcprovisioning.ProvisioningValuesLoader.java

private void loadSystemValues(HashMap<String, String> values) {
    Context context = getContext();
    putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME,
            "com.example.android.deviceowner");
    putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_LOCALE,
            context.getResources().getConfiguration().locale.toString());
    putIfMissing(values, DevicePolicyManager.EXTRA_PROVISIONING_TIME_ZONE, TimeZone.getDefault().getID());
    if (!values.containsKey(DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID)) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Activity.WIFI_SERVICE);
        WifiInfo info = wifiManager.getConnectionInfo();
        values.put(DevicePolicyManager.EXTRA_PROVISIONING_WIFI_SSID, trimSsid(info.getSSID()));
    }/*from w  w w . j  av  a  2  s.com*/
}

From source file:com.google.zxing.client.android.result.ClickVURIResultHandler.java

public String getMacAddress() {
    WifiManager wifiManager = (WifiManager) getActivity().getSystemService(getActivity().WIFI_SERVICE);
    return wifiManager.getConnectionInfo().getMacAddress();
}

From source file:de.ribeiro.android.gso.core.MyContext.java

/**
 * @param context//  ww  w . j a v  a 2s .  co m
 * @return
 * @author Tobias Janssen
 * <p/>
 * Prft, ob eine Wlan verbindung besteht, und liefert das Ergebnis
 */
public Boolean isWifiConnected() {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiinfo = wifi.getConnectionInfo();

    return wifiinfo.getNetworkId() != -1;
}

From source file:com.codestation.henkakuserver.MainActivity.java

private String getIpAccess(boolean url) {
    String formatedIpAddress;//w  w  w .  j  a v a  2 s .  c o m

    if (isConnectedInWifi(true)) {
        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
        formatedIpAddress = String.format(Locale.getDefault(), "%d.%d.%d.%d", (ipAddress & 0xff),
                (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
    } else {
        formatedIpAddress = "192.168.43.1";
    }

    if (url) {
        return "http://" + formatedIpAddress + ":";
    } else {
        return formatedIpAddress;
    }
}

From source file:de.taxilof.UulmLoginAgent.java

/**
 * fetch the ssid of the wifi network/*ww  w . j  a  v  a 2s.  c  om*/
 * 
 * @return ip
 */
private String getSsid() {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String ssid = null;
    if (wifiInfo != null) {
        ssid = wifiInfo.getSSID();
    }
    if (ssid != null) {
        return ssid.replace("\"", ""); // because .getSSID() sucks...
    } else {
        return "";
    }
}

From source file:com.google.android.gms.samples.vision.face.facetracker.MainActivity.java

void checkWifi(final WifiManager wifiManager) {
    Log.d("WIFI CHECK", wifiManager.getConnectionInfo().toString());

    //        Memeriksa jika wifi yang terkoneksi merupakan wifi yang diinginkan
    //        atau bukan
    if (wifiManager.getConnectionInfo().getBSSID() == null
            || wifiManager.getConnectionInfo().getBSSID().toString().compareTo(TARGET_WIFI_BSSID) != 0) {
        //            Menampilkan sebuah alert dialog yang meminta user untuk melakukan koneksi ke wifi
        //            dengan ssid yang diinginkan
        new AlertDialog.Builder(this)
                .setMessage("Please Connect to wifi network named '" + TARGET_WIFI_SSID + "'")
                .setPositiveButton("Connect to wifi", new DialogInterface.OnClickListener() {
                    @Override/*from   w w w  . ja  va2s .c om*/
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        //                            User dialihkan ke setting wifi untuk melakukan koneksi ke wifi tujuan
                        startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                    }
                }).setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                }).setCancelable(false).show();
        return;
    }
}

From source file:de.taxilof.UulmLoginAgent.java

/**
 * fetch the IP of the Device/*w  w w  . ja  va 2s  .c o  m*/
 * 
 * @return ip
 */
private String getIp() {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String ipAddress = null;
    if (wifiInfo != null) {
        long addr = wifiInfo.getIpAddress();
        if (addr != 0) {
            if (addr < 0)
                addr += 0x100000000L; // handle negative values whe first
            // octet > 127
            ipAddress = String.format("%d.%d.%d.%d", addr & 0xFF, (addr >> 8) & 0xFF, (addr >> 16) & 0xFF,
                    (addr >> 24) & 0xFF);
        }
    }
    return ipAddress;
}

From source file:github.daneren2005.serverproxy.ServerProxy.java

public String getPublicAddress(String request) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int ipAddress = wifiManager.getConnectionInfo().getIpAddress();

    if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
        ipAddress = Integer.reverseBytes(ipAddress);
    }/*from w ww  . j  a  v  a2  s  .  c  o  m*/

    byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
    String ipAddressString = null;
    try {
        ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
    } catch (UnknownHostException ex) {
        Log.e(TAG, "Unable to get host address.");
    }

    return getAddress(ipAddressString, request);
}