use of android.net.wifi.WifiManager in project RoMote by wseemann.
the class ConfigureDeviceFragment method getWirelessNetworkName.
private String getWirelessNetworkName(Context context) {
String networkName = "";
final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return networkName;
}
final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
if (isConnected && isWiFi) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
networkName = connectionInfo.getSSID();
}
return networkName;
}
use of android.net.wifi.WifiManager in project AnExplorer by 1hakr.
the class ConnectionUtils method getLocalInetAddress.
public static InetAddress getLocalInetAddress(Context context) {
if (!isConnectedToLocalNetwork(context)) {
Log.e(TAG, "getLocalInetAddress called and no connection");
return null;
}
if (isConnectedToWifi(context)) {
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int ipAddress = wm.getConnectionInfo().getIpAddress();
if (ipAddress == 0)
return null;
return intToInet(ipAddress);
}
try {
Enumeration<NetworkInterface> netinterfaces = NetworkInterface.getNetworkInterfaces();
while (netinterfaces.hasMoreElements()) {
NetworkInterface netinterface = netinterfaces.nextElement();
Enumeration<InetAddress> adresses = netinterface.getInetAddresses();
while (adresses.hasMoreElements()) {
InetAddress address = adresses.nextElement();
// this is the condition that sometimes gives problems
if (!address.isLoopbackAddress() && !address.isLinkLocalAddress())
return address;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of android.net.wifi.WifiManager in project AnExplorer by 1hakr.
the class ConnectionUtils method isConnectedToLocalNetwork.
public static boolean isConnectedToLocalNetwork(Context context) {
boolean connected = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
connected = ni != null && ni.isConnected() && (ni.getType() & (ConnectivityManager.TYPE_WIFI | ConnectivityManager.TYPE_ETHERNET)) != 0;
if (!connected) {
Log.d(TAG, "isConnectedToLocalNetwork: see if it is an WIFI AP");
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
try {
Method method = wm.getClass().getDeclaredMethod("isWifiApEnabled");
connected = (Boolean) method.invoke(wm);
} catch (Exception e) {
e.printStackTrace();
}
}
if (!connected) {
Log.d(TAG, "isConnectedToLocalNetwork: see if it is an USB AP");
try {
for (NetworkInterface netInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
if (netInterface.getDisplayName().startsWith("rndis")) {
connected = true;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
return connected;
}
use of android.net.wifi.WifiManager in project underlx by underlx.
the class MainService method onCreate.
@Override
public void onCreate() {
creationDate = new Date();
stateTickHandler = new Handler();
PreferenceManager.setDefaultValues(this.getApplicationContext(), R.xml.notif_settings, false);
api = API.getInstance();
wfc = new WiFiChecker(this, (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE));
wfc.setScanInterval(10000);
if (networks.size() == 0) {
loadNetworks();
}
SharedPreferences sharedPref = getSharedPreferences("settings", MODE_PRIVATE);
sharedPref.registerOnSharedPreferenceChangeListener(generalPrefsListener);
if (new Date().getTime() - sharedPref.getLong(PreferenceNames.LastAutoTopologyUpdateCheck, 0) > TimeUnit.HOURS.toMillis(2)) {
SharedPreferences.Editor e = sharedPref.edit();
e.putLong(PreferenceNames.LastAutoTopologyUpdateCheck, new Date().getTime());
e.apply();
checkForTopologyUpdates();
}
realmForListeners = Application.getDefaultRealmInstance(this);
tripRealmResults = realmForListeners.where(Trip.class).findAll();
tripRealmResults.addChangeListener(tripRealmChangeListener);
feedbackRealmResults = realmForListeners.where(Feedback.class).findAll();
feedbackRealmResults.addChangeListener(feedbackRealmChangeListener);
pairManager = new PairManager(getApplicationContext());
// pairManager.unpair();
API.getInstance().setPairManager(pairManager);
if (!pairManager.isPaired()) {
pairManager.pairAsync();
}
synchronizer = new Synchronizer(getApplicationContext());
}
use of android.net.wifi.WifiManager in project WifiUtils by ThanosFisherman.
the class ConnectorUtils method connectWps.
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
static void connectWps(@NonNull final WifiManager wifiManager, @NonNull final ScanResult scanResult, @NonNull String pin, long timeOutMillis, @NonNull final ConnectionWpsListener connectionWpsListener) {
final WeakHandler handler = new WeakHandler();
final WpsInfo wpsInfo = new WpsInfo();
final Runnable handlerTimeoutRunnable = new Runnable() {
@Override
public void run() {
wifiManager.cancelWps(null);
wifiLog("Connection with WPS has timed out");
cleanPreviousConfiguration(wifiManager, scanResult);
connectionWpsListener.isSuccessful(false);
handler.removeCallbacks(this);
}
};
final WifiManager.WpsCallback wpsCallback = new WifiManager.WpsCallback() {
@Override
public void onStarted(String pin) {
}
@Override
public void onSucceeded() {
handler.removeCallbacks(handlerTimeoutRunnable);
wifiLog("CONNECTED With WPS successfully");
connectionWpsListener.isSuccessful(true);
}
@Override
public void onFailed(int reason) {
handler.removeCallbacks(handlerTimeoutRunnable);
final String reasonStr;
switch(reason) {
case 3:
reasonStr = "WPS_OVERLAP_ERROR";
break;
case 4:
reasonStr = "WPS_WEP_PROHIBITED";
break;
case 5:
reasonStr = "WPS_TKIP_ONLY_PROHIBITED";
break;
case 6:
reasonStr = "WPS_AUTH_FAILURE";
break;
case 7:
reasonStr = "WPS_TIMED_OUT";
break;
default:
reasonStr = String.valueOf(reason);
}
wifiLog("FAILED to connect with WPS. Reason: " + reasonStr);
cleanPreviousConfiguration(wifiManager, scanResult);
reenableAllHotspots(wifiManager);
connectionWpsListener.isSuccessful(false);
}
};
wifiLog("Connecting with WPS...");
wpsInfo.setup = WpsInfo.KEYPAD;
wpsInfo.BSSID = scanResult.BSSID;
wpsInfo.pin = pin;
wifiManager.cancelWps(null);
if (!cleanPreviousConfiguration(wifiManager, scanResult))
disableAllButOne(wifiManager, scanResult);
handler.postDelayed(handlerTimeoutRunnable, timeOutMillis);
wifiManager.startWps(wpsInfo, wpsCallback);
}
Aggregations