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:org.hygieiasoft.cordova.uid.UID.java

/**
 * Get the Media Access Control address (MAC).
 *
 * @param context The context of the main Activity.
 * @return/*  w w  w .j  av  a  2  s.  co m*/
 */
public String getMac(Context context) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    final WifiInfo wInfo = wifiManager.getConnectionInfo();
    String mac = wInfo.getMacAddress();
    return mac;
}

From source file:org.zywx.wbpalmstar.platform.push.mqttpush.PushGetData2.java

private String getMacAddress() {
    WifiManager wifi = (WifiManager) mCtx.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    return info.getMacAddress().replaceAll(":", "");
}

From source file:com.bangz.smartmute.WifiEditActivity.java

@Override
public void onSuccessUpdateDatabase(Uri uri) {

    if (bActivited == false)
        return;// ww  w .j a  va 2s .c  o  m

    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo ninfo = cm.getActiveNetworkInfo();

    if (ninfo != null && ninfo.isConnected() && ninfo.getType() == ConnectivityManager.TYPE_WIFI) {
        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        if (wifiManager != null) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            String currssid = wifiInfo.getSSID();
            if (currssid.equals(strSSID)) {
                WifiMuteService.wifiConnected(this, strSSID);
            }
        }
    }

}

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

/**
*Returns the device IP address/*from  w ww .  jav a2 s.  c o m*/
*/
public String getIpAddress() {
    WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    String ip = intToIp(ipAddress);
    return ip;
}

From source file:com.amazon.rvspeedtest.GcmIntentService.java

private String testDownloadSpeed() {
    String downloadSpeed = null;//w w w  . java 2 s . c  om
    //        String  url = "http://upload.wikimedia.org/wikipedia/commons/2/2d/Snake_River_%285mb%29.jpg";
    //        byte[] buf = new byte[1024];
    //        int n = 0;
    //        long BeforeTime = System.nanoTime();
    //        long TotalRxBeforeTest = TrafficStats.getTotalRxBytes();
    //        Log.i(TAG, "Before test bytes :" + TotalRxBeforeTest);
    //        //  long TotalTxBeforeTest = TrafficStats.getTotalRxBytes();
    //        try {
    //            InputStream is = new URL(url).openStream();
    //            int bytesRead;
    //            while ((bytesRead = is.read(buf)) != -1) {
    //                n++;
    //            }
    //            Log.i(TAG, "Value of n " + n);
    //            long TotalRxAfterTest = TrafficStats.getTotalRxBytes();
    //            Log.i(TAG, "After test bytes :" + TotalRxAfterTest);
    //            // long TotalTxAfterTest = TrafficStats.getTotalRxBytes();
    //            long AfterTime = System.nanoTime();
    //
    //            double TimeDifference = AfterTime - BeforeTime;
    //            Log.i(TAG, "Time difference " + TimeDifference);
    //
    //            double rxDiff = TotalRxAfterTest - TotalRxBeforeTest;
    //            //Convert into kb
    //            rxDiff /= 1024;
    ////            double txDiff = TotalTxAfterTest - TotalTxBeforeTest;
    //
    //            if ((rxDiff != 0)) {
    //                double rxBPS = (rxDiff / (TimeDifference)) * Math.pow(10, 9); // total rx bytes per second.
    //                downloadSpeed = Double.toString(rxBPS);
    ////            double txBPS = (txDiff / (TimeDifference/1000)); // total tx bytes per second.
    //                Log.i(TAG, String.valueOf(rxBPS) + "KBps. Total rx = " + rxDiff);
    ////            testing[1] = String.valueOf(txBPS) + "bps. Total tx = " + txDiff;
    //            } else {
    //                Log.e(TAG, "Download speed is 0");
    //            }
    //        } catch (Exception e) {
    //            e.printStackTrace();
    //        }
    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo != null) {
        Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
        downloadSpeed = Integer.toString(linkSpeed);
        Log.i(TAG, "link speed : " + linkSpeed);
    }
    return downloadSpeed;
}

From source file:nacho.tfg.blepresencetracker.MyFirebaseInstanceIDService.java

/**
 * Persist token to third-party servers.
 *
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application./*from ww w .j av  a2s  .co m*/
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.
    Intent intent = new Intent(ACTION_SHOW_TOKEN);
    intent.putExtra("token", token);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

    AsyncTask<String, Void, Boolean> asyncTask = new AsyncTask<String, Void, Boolean>() {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Boolean doInBackground(String... params) {
            String data = params[0];
            String ip = "192.168.4.1";
            int port = 7777;
            try {
                Socket socket = new Socket(ip, port);
                while (!socket.isConnected()) {

                }

                OutputStream out = socket.getOutputStream();
                PrintWriter output = new PrintWriter(out);

                output.println(data);
                output.flush();
                out.close();
                output.close();

                socket.close();
            } catch (SocketException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            if (result) {
                SharedPreferences settings = PreferenceManager
                        .getDefaultSharedPreferences(MyFirebaseInstanceIDService.this);
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean(getString(R.string.registration_id_sent), true);
                editor.commit();
            } else {
                SharedPreferences settings = PreferenceManager
                        .getDefaultSharedPreferences(MyFirebaseInstanceIDService.this);
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean(getString(R.string.registration_id_sent), false);
                editor.commit();
            }
        }
    };

    // Change wifi network to "NodeMCU WiFi"

    String ssid = "";
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()) == NetworkInfo.DetailedState.CONNECTED) {
        ssid = wifiInfo.getSSID();
    }

    if (!ssid.equals("NodeMCU WiFi")) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), getString(R.string.toast_connect_NodeMCU),
                        Toast.LENGTH_LONG).show();

            }
        });

    }
}

From source file:com.example.multi_ndef.frag_wifi.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag_wifi, container, false);

    mSpinner = (Spinner) v.findViewById(R.id.spinner);
    mSpinner.setOnItemSelectedListener(this);

    mEditText1 = (EditText) v.findViewById(R.id.editText1);
    fr = (CNFCInterface) getActivity().getApplication();

    WifiManager wifi;
    try {//w  ww  . jav a  2s.c  om
        wifi = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);

        // Get WiFi status
        WifiInfo info = wifi.getConnectionInfo();
        // List available networks

        wifi.startScan();
        List<ScanResult> results;
        results = wifi.getScanResults();
        int array_length = results.size();
        int i = 0;
        String[] values = new String[array_length];
        for (ScanResult result : results) {
            values[i] = result.SSID;
            i = i + 1;
        }

        Set<String> set = new HashSet<String>();
        Collections.addAll(set, values);
        String[] uniques = set.toArray(new String[0]);
        int length = uniques.length;

        List<String> SpinnerArray = new ArrayList<String>();

        for (int i1 = 1; i1 < length; i1++) {
            SpinnerArray.add(uniques[i1]);
        }

        // Create an ArrayAdapter using the string array and a default

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
                android.R.layout.simple_spinner_item, SpinnerArray);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        mSpinner.setAdapter(adapter);

    } catch (Exception e) {
        Toast toast = Toast.makeText(getActivity().getApplicationContext(), "Turn on the wifi",
                Toast.LENGTH_SHORT);
        toast.show();

    }

    return v;
}

From source file:de.teambluebaer.patientix.activities.LoginActivity.java

/**
 * Method to get the MACAddress of the device
 *
 * @return String of the MACAddress of the device
 *//*from   w  w w. java  2 s.c o m*/
private String getMacAddress() {
    WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = manager.getConnectionInfo();
    return info.getMacAddress();
}

From source file:biz.easymenu.easymenung.SettingsFragment.java

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

    View v = inflater.inflate(R.layout.settings, container, false);

    String strVersion = "";

    PackageInfo packageInfo;/*from  w  w w  .  j a va2  s .  co  m*/
    try {
        packageInfo = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
        strVersion = packageInfo.versionName;
    } catch (NameNotFoundException e) {
        //e.printStackTrace();
        strVersion = "Cannot load Version!";
    }

    WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String macAddress = wifiInfo == null ? "" : wifiInfo.getMacAddress();

    ((EditText) v.findViewById(R.id.txtVersion)).setText(strVersion);
    ((EditText) v.findViewById(R.id.txtMac)).setText(macAddress);
    (etServer = (EditText) v.findViewById(R.id.txtServer)).setText(emp.getValue("server"));
    (etPassword = (EditText) v.findViewById(R.id.txtPassword)).setText(emp.getValue("password"));

    rg = (RadioGroup) v.findViewById(R.id.rgSync);
    pbar = (ProgressBar) v.findViewById(R.id.pBarSync);

    btnSync = (Button) v.findViewById(R.id.btnSync);
    btnSave = (Button) v.findViewById(R.id.btnConfSave);
    btnClose = (Button) v.findViewById(R.id.btnConfClose);

    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            emp.setKeyValue("server", etServer.getText().toString());
            emp.setKeyValue("password", etPassword.getText().toString());
            emp.commit();

            /*FragmentManager fm = getActivity().getSupportFragmentManager();
             FragmentTransaction ft ;
             Fragment f = null;
             if(fm.findFragmentByTag("rightfragment")!=null){
              ft = fm.beginTransaction();
              ft.setCustomAnimations(android.R.anim.fade_in,
                  android.R.anim.fade_out);
              ft.remove(fm.findFragmentByTag("rightfragment"));
              ft.commit();
            }
             f = new TableListFragment();
             ft = fm.beginTransaction();
            ft.setCustomAnimations(android.R.anim.fade_in,
                android.R.anim.fade_out);
            ft.add(R.id.rightcontent,f,"rightfragment");
            ft.commit();
            getActivity().findViewById(R.id.btnConfig).setVisibility(View.GONE);
            */}
    });

    btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            /*emp.setKeyValue("server", etServer.getText().toString());
            emp.setKeyValue("password", etPassword.getText().toString());
            emp.commit();
            */
            FragmentManager fm = getActivity().getSupportFragmentManager();
            FragmentTransaction ft;
            Fragment f = null;
            if (fm.findFragmentByTag("rightfragment") != null) {
                ft = fm.beginTransaction();
                ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
                ft.remove(fm.findFragmentByTag("rightfragment"));
                ft.commit();
            }
            f = new TableListFragment();
            ft = fm.beginTransaction();
            ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
            ft.add(R.id.rightcontent, f, "rightfragment");
            ft.commit();
            getActivity().findViewById(R.id.btnConfig).setVisibility(View.GONE);
        }
    });

    btnSync.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int rbid = rg.getCheckedRadioButtonId();
            pbar.setVisibility(View.VISIBLE);
            new Thread(new syncRun(rbid)).start();
        }
    });

    return v;
}

From source file:net.olejon.spotcommander.MyTools.java

public String getCurrentNetwork() {
    final WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);

    if (wifiManager.isWifiEnabled())
        return wifiManager.getConnectionInfo().getSSID();

    return "";
}