|
@@ -12,9 +12,9 @@ import android.net.wifi.WifiInfo;
|
|
|
import android.net.wifi.WifiManager;
|
|
|
import android.util.Log;
|
|
|
|
|
|
-import com.wdkl.ncs.android.component.nursehome.settingconfig.SettingConfig;
|
|
|
-
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
|
|
|
@SuppressLint("NewApi")
|
|
|
public class NetworkUtils {
|
|
@@ -69,6 +69,8 @@ public class NetworkUtils {
|
|
|
|
|
|
private static boolean checkNetConnection = false;
|
|
|
|
|
|
+ private final static ExecutorService threadPool = Executors.newSingleThreadExecutor();
|
|
|
+
|
|
|
/**
|
|
|
* >= Android 10(Q版本)推荐
|
|
|
*
|
|
@@ -256,7 +258,7 @@ public class NetworkUtils {
|
|
|
|
|
|
|
|
|
//连接wifi,优先preferSSID
|
|
|
- public static void connectWifi(Context context, String preferSSID) {
|
|
|
+ public static boolean connectWifi(Context context, String preferSSID) {
|
|
|
try {
|
|
|
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
|
|
//扫描wifi
|
|
@@ -290,7 +292,7 @@ public class NetworkUtils {
|
|
|
boolean connect = wifiManager.enableNetwork(wifiConfiguration.networkId, true);
|
|
|
Log.e(TAG, "connect wifi: " + connect);
|
|
|
if (connect) {
|
|
|
- return;
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -299,31 +301,53 @@ public class NetworkUtils {
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
+
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
- public static void checkNetworkConnect(Context context) {
|
|
|
- if (checkNetConnection) {
|
|
|
- Log.e(TAG, "正在检查网络连接...");
|
|
|
- return;
|
|
|
- }
|
|
|
+ public static void checkNetworkConnect(final Context context) {
|
|
|
+ threadPool.execute(() -> {
|
|
|
+ if (checkNetConnection) {
|
|
|
+ Log.e(TAG, "正在检查网络连接状态...");
|
|
|
+ } else {
|
|
|
+ checkNetConnection = true;
|
|
|
+ boolean connected = false;
|
|
|
+ if (NetworkUtils.isNetConnection(context)) {
|
|
|
+ //网络连接正常,返回
|
|
|
+ connected = true;
|
|
|
+ Log.e(TAG, "network available...");
|
|
|
+ /*if (NetworkUtils.getNetworkInfoType(context) == ConnectivityManager.TYPE_WIFI) {
|
|
|
+ String wifiSSID = NetworkUtils.getConnectWifi(context);
|
|
|
+ SettingConfig.setWifiSSID(context, wifiSSID);
|
|
|
+ }*/
|
|
|
+ } else {
|
|
|
+ //无网络连接,判断是否开启了wifi,如开启了则尝试自动连接
|
|
|
+ boolean wifiState = NetworkUtils.getWifiStatus(context);
|
|
|
+ if (wifiState) {
|
|
|
+ //wifi扫描需要时间,延迟3s后再连接
|
|
|
+ try {
|
|
|
+ Thread.sleep(3000);
|
|
|
+ } catch (Exception e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ connected = NetworkUtils.connectWifi(context, "");
|
|
|
+ }
|
|
|
+ Log.e(TAG, "network unavailable...wifiState: " + wifiState + ", connected: " + connected);
|
|
|
+ }
|
|
|
|
|
|
- checkNetConnection = true;
|
|
|
- if (NetworkUtils.isNetConnection(context)) {
|
|
|
- //网络连接正常,返回
|
|
|
- Log.e(TAG, "network available...");
|
|
|
- /*if (NetworkUtils.getNetworkInfoType(context) == ConnectivityManager.TYPE_WIFI) {
|
|
|
- String wifiSSID = NetworkUtils.getConnectWifi(context);
|
|
|
- SettingConfig.setWifiSSID(context, wifiSSID);
|
|
|
- }*/
|
|
|
- } else {
|
|
|
- //无网络连接,判断是否开启了wifi,如开启了则尝试自动连接
|
|
|
- boolean wifiState = NetworkUtils.getWifiStatus(context);
|
|
|
- Log.e(TAG, "network unavailable..." + wifiState);
|
|
|
- if (wifiState) {
|
|
|
- NetworkUtils.connectWifi(context, "");
|
|
|
- }
|
|
|
- }
|
|
|
+ checkNetConnection = false;
|
|
|
|
|
|
- checkNetConnection = false;
|
|
|
+ if (!connected) {
|
|
|
+ Log.e(TAG, "wifi not connect, wait 30s try again...");
|
|
|
+ try {
|
|
|
+ Thread.sleep(30000);
|
|
|
+ } catch (Exception e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+
|
|
|
+ checkNetworkConnect(context);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
}
|