|
@@ -6,13 +6,19 @@ import android.net.ConnectivityManager;
|
|
|
import android.net.Network;
|
|
|
import android.net.NetworkCapabilities;
|
|
|
import android.net.NetworkInfo;
|
|
|
+import android.net.NetworkRequest;
|
|
|
+import android.net.NetworkSpecifier;
|
|
|
import android.net.wifi.ScanResult;
|
|
|
import android.net.wifi.WifiConfiguration;
|
|
|
import android.net.wifi.WifiInfo;
|
|
|
import android.net.wifi.WifiManager;
|
|
|
+import android.net.wifi.WifiNetworkSpecifier;
|
|
|
+import android.os.Build;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+
|
|
|
import com.wdkl.ncs.android.component.home.settingconfig.SettingConfig;
|
|
|
import com.wdkl.ncs.android.lib.base.BaseApplication;
|
|
|
import com.wdkl.ncs.android.middleware.common.Constants;
|
|
@@ -75,6 +81,7 @@ public class NetworkUtils {
|
|
|
|
|
|
|
|
|
private static boolean checkNetConnection = false;
|
|
|
+ private static boolean connected = false;
|
|
|
|
|
|
private final static ExecutorService threadPool = Executors.newSingleThreadExecutor();
|
|
|
|
|
@@ -312,17 +319,100 @@ public class NetworkUtils {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ public static WifiConfiguration getPreWifiConfig(Context context) {
|
|
|
+ try {
|
|
|
+ WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
|
|
+ //扫描wifi
|
|
|
+ List<ScanResult> scanResults = wifiManager.getScanResults();
|
|
|
+ //获取已保存的wifi列表
|
|
|
+ List<WifiConfiguration> wifiConfigs = wifiManager.getConfiguredNetworks();
|
|
|
+
|
|
|
+ if (scanResults != null && wifiConfigs != null) {
|
|
|
+ for (WifiConfiguration wifiConfiguration : wifiConfigs) {
|
|
|
+ for (ScanResult scanResult : scanResults) {
|
|
|
+ Log.e(TAG, "get prefered wifiConfig: ssid=" + wifiConfiguration.SSID + ", p=" + wifiConfiguration.preSharedKey
|
|
|
+ + "------scanResult: ssid=" + scanResult.SSID + ", leve=" + scanResult.level + "dBm");
|
|
|
+ String scanSSID = "\"" + scanResult.SSID + "\"";
|
|
|
+ if (scanSSID.equals(wifiConfiguration.SSID) && scanResult.level > -85) {
|
|
|
+ //连接WiFi,信号强度大于-85dBm才认为WiFi可用
|
|
|
+ return wifiConfiguration;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void connectByP2P(Context context, ConnectivityManager.NetworkCallback callback) {
|
|
|
+ WifiConfiguration wifiConfiguration = getPreWifiConfig(context);
|
|
|
+ if (wifiConfiguration != null) {
|
|
|
+ NetworkSpecifier specifier = new WifiNetworkSpecifier.Builder()
|
|
|
+ .setSsid(wifiConfiguration.SSID)
|
|
|
+ .setWpa2Passphrase(wifiConfiguration.preSharedKey)
|
|
|
+ .build();
|
|
|
+ NetworkRequest request = new NetworkRequest.Builder()
|
|
|
+ .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
|
|
|
+ .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
|
+ .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
|
|
|
+ .addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
|
|
|
+ .setNetworkSpecifier(specifier)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ connectivityManager.requestNetwork(request, callback);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static void checkNetworkConnect(final Context context, final boolean forceConnect) {
|
|
|
threadPool.execute(() -> {
|
|
|
if (checkNetConnection) {
|
|
|
Log.e(TAG, "正在检查网络连接状态...");
|
|
|
} else {
|
|
|
checkNetConnection = true;
|
|
|
- boolean connected = false;
|
|
|
+ connected = false;
|
|
|
//判断是否开启了wifi,如开启了则尝试自动连接
|
|
|
boolean wifiState = NetworkUtils.getWifiStatus(context);
|
|
|
- if (forceConnect) {
|
|
|
- if (wifiState) {
|
|
|
+ if (!wifiState) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
|
+ try {
|
|
|
+ ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
|
|
|
+ @Override
|
|
|
+ public void onAvailable(@NonNull Network network) {
|
|
|
+ Log.e(TAG, "connectByP2P==> onAvailable");
|
|
|
+ connected = true;
|
|
|
+ super.onAvailable(network);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onUnavailable() {
|
|
|
+ Log.e(TAG, "connectByP2P==> onUnavailable");
|
|
|
+ connected = false;
|
|
|
+ super.onUnavailable();
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ connectByP2P(context, networkCallback);
|
|
|
+
|
|
|
+ Thread.sleep(15000);
|
|
|
+
|
|
|
+ Log.e(TAG, "wifi connect: " + connected + ", tcp connect: " + TcpClientHandler.getConnected());
|
|
|
+ if (!connected && !TcpClientHandler.getConnected()) {
|
|
|
+ checkNetworkConnect(context, forceConnect);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ checkNetConnection = false;
|
|
|
+ } else {*/
|
|
|
+ if (forceConnect) {
|
|
|
//wifi扫描需要时间,延迟3s后再连接
|
|
|
try {
|
|
|
Thread.sleep(3000);
|
|
@@ -330,19 +420,19 @@ public class NetworkUtils {
|
|
|
//
|
|
|
}
|
|
|
connected = NetworkUtils.connectWifi(context, "");
|
|
|
- }
|
|
|
- Log.e(TAG, "force connect wifi...wifiState: " + wifiState + ", connected: " + connected);
|
|
|
- } else {
|
|
|
- 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);
|
|
|
- }*/
|
|
|
+
|
|
|
+ Log.e(TAG, "force connect wifi...wifiState: " + wifiState + ", connected: " + connected);
|
|
|
} else {
|
|
|
- if (wifiState) {
|
|
|
+ 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扫描需要时间,延迟3s后再连接
|
|
|
try {
|
|
|
Thread.sleep(3000);
|
|
@@ -350,23 +440,24 @@ public class NetworkUtils {
|
|
|
//
|
|
|
}
|
|
|
connected = NetworkUtils.connectWifi(context, "");
|
|
|
+
|
|
|
+ Log.e(TAG, "network unavailable...wifiState: " + wifiState + ", connected: " + connected);
|
|
|
}
|
|
|
- Log.e(TAG, "network unavailable...wifiState: " + wifiState + ", connected: " + connected);
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- checkNetConnection = false;
|
|
|
+ checkNetConnection = false;
|
|
|
|
|
|
- if (!connected && wifiState && !TcpClientHandler.getConnected()) {
|
|
|
- Log.e(TAG, "wifi not connect or disconnect server, wait 10s try again...");
|
|
|
- try {
|
|
|
- Thread.sleep(10000);
|
|
|
- } catch (Exception e) {
|
|
|
- //
|
|
|
- }
|
|
|
+ if (!connected && !TcpClientHandler.getConnected()) {
|
|
|
+ Log.e(TAG, "wifi not connect or disconnect server, wait 10s try again...");
|
|
|
+ try {
|
|
|
+ Thread.sleep(15000);
|
|
|
+ } catch (Exception e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
|
|
|
- checkNetworkConnect(context, forceConnect);
|
|
|
- }
|
|
|
+ checkNetworkConnect(context, forceConnect);
|
|
|
+ }
|
|
|
+ //}
|
|
|
}
|
|
|
});
|
|
|
}
|