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.castoffline.castActivity.CastMedia.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    actionBar = getSupportActionBar();/*from   www. j a v  a  2  s  .com*/
    setContentView(R.layout.castmedia);
    mediatype = this.getIntent().getType();
    extras = this.getIntent();

    //Get the ip of the device running NanoHTTPD server
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    ipdevice = String.format("http://%d.%d.%d.%d:8080", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
            (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));

    // start the webserver
    mediaserver = new webserver();
    try {
        mediaserver.start();
    } catch (IOException ioe) {
        Log.d("Httpd", "The server could not start.");
    }

    //The application needs to obtain an instance of the MediaRouter and needs to hold onto that instance for the lifetime of the sender application
    mMediaRouter = MediaRouter.getInstance(getApplicationContext());
    //The MediaRouter needs to filter discovery for Cast devices that can launch the receiver application associated with the sender app. For that a MediaRouteSelector is created by calling MediaRouteSelector.Builder
    mMediaRouteSelector = new MediaRouteSelector.Builder()
            .addControlCategory(CastMediaControlIntent.categoryForCast(getString(R.string.app_id))).build();
    // create an instance of MediaRouterCallback
    mMediaRouterCallback = new MyMediaRouterCallback();
    mediaplay();
}

From source file:com.lgallardo.qbittorrentclient.NotifierService.java

protected void getSettings() {
    // Preferences stuff
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);

    builderPrefs = new StringBuilder();

    builderPrefs.append("\n" + sharedPrefs.getString("language", "NULL"));

    // Get values from preferences
    currentServer = Integer.parseInt(sharedPrefs.getString("currentServer", "1"));

    hostname = sharedPrefs.getString("hostname", "");
    subfolder = sharedPrefs.getString("subfolder", "");

    protocol = sharedPrefs.getString("protocol", "NULL");

    // If user leave the field empty, set 8080 port
    try {/* w  w w . jav a2s  . c o m*/
        port = Integer.parseInt(sharedPrefs.getString("port", "8080"));
    } catch (NumberFormatException e) {
        port = 8080;

    }
    username = sharedPrefs.getString("username", "NULL");
    password = sharedPrefs.getString("password", "NULL");
    https = sharedPrefs.getBoolean("https", false);

    // Check https
    if (https) {
        protocol = "https";

    } else {
        protocol = "http";
    }

    // Get connection and data timeouts
    try {
        connection_timeout = Integer.parseInt(sharedPrefs.getString("connection_timeout", "10"));
    } catch (NumberFormatException e) {
        connection_timeout = 10;
    }

    try {
        data_timeout = Integer.parseInt(sharedPrefs.getString("data_timeout", "20"));
    } catch (NumberFormatException e) {
        data_timeout = 20;
    }

    qb_version = sharedPrefs.getString("qb_version", "3.2.x");

    cookie = sharedPrefs.getString("qbCookie2", null);

    // Get last state
    lastState = sharedPrefs.getString("lastState", null);

    // Notifications
    enable_notifications = sharedPrefs.getBoolean("enable_notifications", false);
    completed_hashes = sharedPrefs.getString("completed_hashes" + currentServer, "");

    // Get local SSID properties
    ssid = sharedPrefs.getString("ssid", "");
    local_hostname = sharedPrefs.getString("local_hostname", null);

    // If user leave the field empty, set 8080 port
    try {
        local_port = Integer.parseInt(sharedPrefs.getString("local_port", "-1"));
    } catch (NumberFormatException e) {
        local_port = -1;

    }

    // Set SSI and local hostname and port
    if (ssid != null && !ssid.equals("")) {

        // Get SSID if WiFi
        WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        String wifiSSID = wifiInfo.getSSID();

        //            Log.d("Debug", "NotifierService - WiFi SSID: " + wifiSSID);
        //            Log.d("Debug", "NotifierService - SSID: " + ssid);

        if (wifiSSID.equals("\"" + ssid + "\"") && wifiMgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {

            if (local_hostname != null && !local_hostname.equals("")) {
                hostname = local_hostname;
            }

            if (local_port != -1) {
                port = local_port;
            }

            //                Log.d("Debug", "NotifierService - hostname: " + hostname);
            //                Log.d("Debug", "NotifierService - port: " + port);
            //                Log.d("Debug", "NotifierService - local_hostname: " + local_hostname);
            //                Log.d("Debug", "NotifierService -  local_port: " + local_port);

        }
    }

    // Get keystore for self-signed certificate
    keystore_path = sharedPrefs.getString("keystore_path" + currentServer, "");
    keystore_password = sharedPrefs.getString("keystore_password" + currentServer, "");

}

From source file:cn.apputest.ctria.section2.Lucha2Activity.java

public String getLocalMacAddress() {
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    return info.getMacAddress();
}

From source file:org.pidome.client.services.aidl.service.SystemServiceAidl.java

private void setHome() {
    if (prefs != null) {
        if (prefs.getBoolPreference("wifiConnectHomeEnabled", false)) {
            ConnectivityManager connManager = (ConnectivityManager) getSystemService(
                    Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
            if (networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
                if (connectionInfo != null) {
                    String SSID = connectionInfo.getSSID();
                    String BSSID = connectionInfo.getBSSID();
                    if (SSID != null && BSSID != null) {
                        if (SSID.equals(prefs.getStringPreference("wifiConnectSSID",
                                java.util.UUID.randomUUID().toString()))
                                && BSSID.equals(prefs.getStringPreference("wifiConnectBSSID",
                                        java.util.UUID.randomUUID().toString()))) {
                            singleThreadfPipeExecutor.execute((Runnable) () -> {
                                int i = SystemServiceAidl.this.clientCallBackList.beginBroadcast();
                                while (i > 0) {
                                    i--;
                                    try {
                                        clientCallBackList.beginBroadcast();
                                        clientCallBackList.getBroadcastItem(i).updateUserPresence(1);
                                        clientCallBackList.finishBroadcast();
                                    } catch (RemoteException ex) {
                                        Logger.getLogger(SystemServiceAidl.class.getName()).log(Level.SEVERE,
                                                null, ex);
                                    }/*  ww  w .  j a v a2 s.c o  m*/
                                }
                            });
                            localizationService.setHome(true);
                        } else {
                            localizationService.setHome(false);
                        }
                    }
                }
            } else if (networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                localizationService.setHome(false);
            }
        }
    }
}

From source file:com.carapp.login.splashActivity.java

private void getClientBranch() {

    MultipartEntity entity = new MultipartEntity();
    try {/*  w  ww.  j  a v  a  2s  .  c o m*/
        entity.addPart("action", new StringBody("init_config"));

        new AsyncWebServiceProcessingTask(context, entity, messagecheck, new AsynckCallback() {

            @Override
            public void run(String result) {
                if (UIUtils.checkJson(result, context)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.optString("satus").equals("success")) {
                            PdfInfo.client = jsonObject.optString("client");
                            PdfInfo.branch = jsonObject.optString("branch");
                            Util.showCustomDialogWithoutButton(context, "Message", messagechecksucces,
                                    new Callback2() {
                                        @Override
                                        public void ok(final Dialog dialog) {
                                            final Handler handler = new Handler();
                                            final Runnable runnable = new Runnable() {
                                                @Override
                                                public void run() {
                                                    dialog.dismiss();
                                                    new AsyncWebServiceProcessingTask(context, null,
                                                            "Please wait while checking date",
                                                            new AsynckCallback() {

                                                                @Override
                                                                public void run(String result) {
                                                                    Log.e("date", "" + result);
                                                                    String sysdate = android.text.format.DateFormat
                                                                            .format("dd/MM/yyyy",
                                                                                    new java.util.Date())
                                                                            .toString();
                                                                    if (result.equals(sysdate)) {
                                                                        Toast.makeText(context, "date match",
                                                                                Toast.LENGTH_SHORT).show();
                                                                        new AsyncWebServiceProcessingTask(
                                                                                context, null, messagecheckday,
                                                                                new AsynckCallback() {
                                                                                    @Override
                                                                                    public void run(
                                                                                            String result) {
                                                                                        int day = Integer
                                                                                                .parseInt(
                                                                                                        result);
                                                                                        Calendar calendar = Calendar
                                                                                                .getInstance();
                                                                                        int Today = calendar
                                                                                                .get(Calendar.DAY_OF_WEEK);
                                                                                        day++;
                                                                                        if (day == Today) {
                                                                                            Toast.makeText(
                                                                                                    context,
                                                                                                    "day is correct",
                                                                                                    Toast.LENGTH_SHORT)
                                                                                                    .show();
                                                                                            String ma_a = null;

                                                                                            try {
                                                                                                WifiManager wiman = (WifiManager) getSystemService(
                                                                                                        Context.WIFI_SERVICE);
                                                                                                ma_a = wiman
                                                                                                        .getConnectionInfo()
                                                                                                        .getMacAddress();
                                                                                                Log.i(t, ""
                                                                                                        + ma_a);
                                                                                            } catch (Exception e1) {

                                                                                                e1.printStackTrace();
                                                                                                Log.i(t, " "
                                                                                                        + e1);
                                                                                            }
                                                                                            MultipartEntity entity = new MultipartEntity();
                                                                                            try {
                                                                                                entity.addPart(
                                                                                                        "action",
                                                                                                        new StringBody(
                                                                                                                "device_authentication"));
                                                                                                entity.addPart(
                                                                                                        "mac_address",
                                                                                                        new StringBody(
                                                                                                                ma_a));
                                                                                                new AsyncWebServiceProcessingTask(
                                                                                                        context,
                                                                                                        entity,
                                                                                                        "Checking License",
                                                                                                        new AsynckCallback() {

                                                                                                            @Override
                                                                                                            public void run(
                                                                                                                    String result) {

                                                                                                                if (UIUtils
                                                                                                                        .checkJson(
                                                                                                                                result,
                                                                                                                                context)) {
                                                                                                                    try {
                                                                                                                        JSONObject jsonObject = new JSONObject(
                                                                                                                                result);
                                                                                                                        if (jsonObject
                                                                                                                                .optString(
                                                                                                                                        "satus")
                                                                                                                                .equals("success")) {
                                                                                                                            Toast.makeText(
                                                                                                                                    context,
                                                                                                                                    "LicenseCheck sucesses",
                                                                                                                                    Toast.LENGTH_SHORT)
                                                                                                                                    .show();
                                                                                                                            startActivity(
                                                                                                                                    new Intent(
                                                                                                                                            context,
                                                                                                                                            LoginActivity.class));
                                                                                                                            finish();

                                                                                                                        } else if (jsonObject
                                                                                                                                .optString(
                                                                                                                                        "satus")
                                                                                                                                .equals("error")) {
                                                                                                                            Util.showCustomDialog(
                                                                                                                                    context,
                                                                                                                                    "Error",
                                                                                                                                    jsonObject
                                                                                                                                            .optString(
                                                                                                                                                    "msg"));

                                                                                                                        }

                                                                                                                    } catch (Exception e) {

                                                                                                                        e.printStackTrace();

                                                                                                                    }

                                                                                                                }
                                                                                                            }
                                                                                                        }).execute(
                                                                                                                PdfInfo.newjobcard);

                                                                                            } catch (Exception e) {

                                                                                                e.printStackTrace();
                                                                                            }

                                                                                        } else {
                                                                                            Util.showCustomDialogWithoutButton(
                                                                                                    context,
                                                                                                    "Error",
                                                                                                    messagecheckdayerror
                                                                                                            + " server day is "
                                                                                                            + day
                                                                                                            + " but your device is"
                                                                                                            + Today,
                                                                                                    new Callback2() {

                                                                                                        @Override
                                                                                                        public void ok(
                                                                                                                final Dialog dialog) {

                                                                                                            final Handler handler = new Handler();
                                                                                                            final Runnable runnable = new Runnable() {
                                                                                                                @Override
                                                                                                                public void run() {
                                                                                                                    dialog.dismiss();
                                                                                                                    finish();
                                                                                                                }
                                                                                                            };
                                                                                                            handler.postDelayed(
                                                                                                                    runnable,
                                                                                                                    5000);

                                                                                                        }
                                                                                                    });
                                                                                        }

                                                                                    }
                                                                                }).execute(PdfInfo.dayaddress);
                                                                    } else {

                                                                        Util.showCustomDialogWithoutButton(
                                                                                context, "Error",
                                                                                messagecheckdateerror
                                                                                        + " server date is "
                                                                                        + result
                                                                                        + " but device date is "
                                                                                        + sysdate,
                                                                                new Callback2() {

                                                                                    @Override
                                                                                    public void ok(
                                                                                            final Dialog dialog) {

                                                                                        new Timer().schedule(
                                                                                                new TimerTask() {

                                                                                                    @Override
                                                                                                    public void run() {
                                                                                                        dialog.dismiss();
                                                                                                        finish();

                                                                                                    }
                                                                                                }, 5000);
                                                                                    }
                                                                                });

                                                                    }
                                                                }
                                                            }).execute(PdfInfo.dateaddress);

                                                }
                                            };
                                            handler.postDelayed(runnable, 5000);

                                        }
                                    });

                        } else if (jsonObject.optString("satus").equals("error")) {
                            Util.showCustomDialogWithoutButton(context, "Message", messagechecksucces,
                                    new Callback2() {

                                        @Override
                                        public void ok(final Dialog dialog) {

                                            new Timer().schedule(new TimerTask() {

                                                @Override
                                                public void run() {
                                                    dialog.dismiss();
                                                    finish();

                                                }
                                            }, 5000);
                                        }
                                    });

                        }

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                }
            }
        }).execute(PdfInfo.newjobcard);

    } catch (Exception e) {

        e.printStackTrace();
    }

}

From source file:com.prey.PreyPhone.java

private void updateWifi() {
    wifi = new Wifi();
    try {//  w ww.ja  v a2  s  . c o m
        WifiManager wifiMgr = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

        wifi.setWifiEnabled(wifiMgr.isWifiEnabled());

        int ipAddress = wifiInfo.getIpAddress();
        wifi.setIpAddress(formatterIp(ipAddress));
        wifi.setMacAddress(wifiInfo.getMacAddress());
        DhcpInfo dhcpInfo = wifiMgr.getDhcpInfo();
        wifi.setNetmask(formatterIp(dhcpInfo.netmask));
        wifi.setGatewayIp(formatterIp(dhcpInfo.serverAddress));
        if (ipAddress != 0) {
            wifi.setInterfaceType("Wireless");
        } else {
            if (PreyConnectivityManager.getInstance(ctx).isMobileConnected()) {
                wifi.setInterfaceType("Mobile");
            } else {
                wifi.setInterfaceType("");
            }
        }
        wifi.setName("eth0");
        String ssid = wifiInfo.getSSID();
        try {
            ssid = ssid.replaceAll("\"", "");
        } catch (Exception e) {

        }
        wifi.setSsid(ssid);

        for (int i = 0; listWifi != null && i < listWifi.size(); i++) {
            Wifi _wifi = listWifi.get(i);
            ssid = _wifi.getSsid();
            try {
                ssid = ssid.replaceAll("\"", "");
            } catch (Exception e) {

            }
            if (ssid.equals(wifi.getSsid())) {
                wifi.setSecurity(_wifi.getSecurity());
                wifi.setSignalStrength(_wifi.getSignalStrength());
                wifi.setChannel(_wifi.getChannel());
                break;
            }
        }
    } catch (Exception e) {
    }
}

From source file:com.example.gemswin.screencastrecevertest.MainActivity_Reciever.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main_reciever);
   WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
   String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
   TextView text = (TextView) findViewById(R.id.textView);


   planetList = new ArrayList<String>();

   mTextureView = (TextureView)findViewById(R.id.textureView);
   mTextureView.setSurfaceTextureListener(this);
   mTextureView.requestLayout();//from   w w  w . ja  v a  2 s.co  m
   mTextureView.invalidate();
   mTextureView.setOpaque(false);
   logout = (Button) findViewById(R.id.logout);
   cast = (Button) findViewById(R.id.castAsmaster);


   doubtbutton = (Button) findViewById(R.id.doubt);
   viewbutton = (Button) findViewById(R.id.viewdoubt);

   pref = new PrefManager(getApplicationContext());
   text.setText(ip);
   logout.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
         pref.logout();
         Intent i = new Intent(MainActivity_Reciever.this, LoginActivity.class);
         startActivity(i);
         finish();
      }
   });
   cast.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

         new allowcast().execute();
      }
   });
   viewbutton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
         //pref.logout();                                               // LOOK HERE ASH
         /*Intent i = new Intent(MainActivity_Reciever.this,DoubtActivity.class);
         startActivity(i);*/
         new viewdoubttask().execute();


      }
   });

   doubtbutton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
         //pref.logout();                                               // LOOK HERE ASH
         /*Intent i = new Intent(MainActivity_Reciever.this,DoubtActivity.class);
         startActivity(i);*/
         DoubtBox();

      }
   });

   mTask = new ReceiverAsyncTask(this);
   mTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

   Intent intent1 = new Intent(this, MyBroadcastReceiver.class);
   intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   PendingIntent pendingIntent = PendingIntent.getBroadcast(
         getApplicationContext(), 234324243, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
   AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
   alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);


}

From source file:org.noise_planet.noisecapture.MainActivity.java

private boolean checkWifiState() {

    // Check connection state
    WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    if (wifiMgr.isWifiEnabled()) { // WiFi adapter is ON
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        if (wifiInfo.getNetworkId() == -1) {
            return false; // Not connected to an access-Point
        }//from w w  w .j av  a2 s.  co m
        // Connected to an Access Point
        return true;
    } else {
        return false; // WiFi adapter is OFF
    }
}

From source file:com.jtechme.apphub.views.ManageReposActivity.java

private void checkIfNewRepoOnSameWifi(NewRepoConfig newRepo) {
    // if this is a local repo, check we're on the same wifi
    if (!TextUtils.isEmpty(newRepo.getBssid())) {
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        String bssid = wifiInfo.getBSSID();
        if (TextUtils.isEmpty(bssid)) /* not all devices have wifi */
            return;
        bssid = bssid.toLowerCase(Locale.ENGLISH);
        String newRepoBssid = Uri.decode(newRepo.getBssid()).toLowerCase(Locale.ENGLISH);
        if (!bssid.equals(newRepoBssid)) {
            String msg = String.format(getString(R.string.not_on_same_wifi), newRepo.getSsid());
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        }/*from ww  w .ja v  a  2  s . c  om*/
        // TODO we should help the user to the right thing here,
        // instead of just showing a message!
    }
}