|
@@ -0,0 +1,457 @@
|
|
|
+package com.wdkl.ncs.android.component.home.util;
|
|
|
+
|
|
|
+import android.annotation.SuppressLint;
|
|
|
+import android.content.Context;
|
|
|
+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.util.Log;
|
|
|
+
|
|
|
+import com.wdkl.ncs.android.middleware.tcp.TcpClientHandler;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
+
|
|
|
+@SuppressLint("NewApi")
|
|
|
+public class NetworkUtils {
|
|
|
+
|
|
|
+ private static final String TAG = "NetworkUtils";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicates this network uses a Cellular transport.
|
|
|
+ */
|
|
|
+ public static final int TRANSPORT_CELLULAR = 0;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicates this network uses a Wi-Fi transport.
|
|
|
+ */
|
|
|
+ public static final int TRANSPORT_WIFI = 1;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicates this network uses a Bluetooth transport.
|
|
|
+ */
|
|
|
+ public static final int TRANSPORT_BLUETOOTH = 2;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicates this network uses an Ethernet transport.
|
|
|
+ */
|
|
|
+ public static final int TRANSPORT_ETHERNET = 3;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicates this network uses a VPN transport.
|
|
|
+ */
|
|
|
+ public static final int TRANSPORT_VPN = 4;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicates this network uses a Wi-Fi Aware transport.
|
|
|
+ */
|
|
|
+ public static final int TRANSPORT_WIFI_AWARE = 5;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicates this network uses a LoWPAN transport.
|
|
|
+ */
|
|
|
+ public static final int TRANSPORT_LOWPAN = 6;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicates this network uses a Test-only virtual interface as a transport.
|
|
|
+ * @hide
|
|
|
+ */
|
|
|
+ public static final int TRANSPORT_TEST = 7;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Indicates this network uses a USB transport.
|
|
|
+ */
|
|
|
+ public static final int TRANSPORT_USB = 8;
|
|
|
+
|
|
|
+
|
|
|
+ private static boolean checkNetConnection = false;
|
|
|
+ private static boolean connected = false;
|
|
|
+
|
|
|
+ private final static ExecutorService threadPool = Executors.newSingleThreadExecutor();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * >= Android 10(Q版本)推荐
|
|
|
+ *
|
|
|
+ * 当前使用MOBILE流量上网
|
|
|
+ */
|
|
|
+ public static boolean isMobileNetwork(Context context) {
|
|
|
+ ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+
|
|
|
+ Network network = cm.getActiveNetwork();
|
|
|
+ if (null == network) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
|
|
|
+ if (null == capabilities) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
|
|
|
+ && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * >= Android 10(Q版本)推荐
|
|
|
+ *
|
|
|
+ * 当前使用WIFI上网
|
|
|
+ */
|
|
|
+
|
|
|
+ public static boolean isWifiNetwork(Context context) {
|
|
|
+ ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ Network network = cm.getActiveNetwork();
|
|
|
+ if (null == network) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
|
|
|
+ if (null == capabilities) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
|
|
|
+ && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * >= Android 10(Q版本)推荐
|
|
|
+ *
|
|
|
+ * 当前使用以太网上网
|
|
|
+ */
|
|
|
+ public static boolean isEthernetNetwork(Context context) {
|
|
|
+ ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ Network network = cm.getActiveNetwork();
|
|
|
+ if (null == network) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
|
|
|
+ if (null == capabilities) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
|
|
|
+ && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * >= Android 10(Q版本)推荐
|
|
|
+ *
|
|
|
+ * NetworkCapabilities.NET_CAPABILITY_INTERNET,表示此网络应该(maybe)能够访问internet
|
|
|
+ *
|
|
|
+ * 判断当前网络可以正常上网
|
|
|
+ * 表示此连接此网络并且能成功上网。 例如,对于具有NET_CAPABILITY_INTERNET的网络,这意味着已成功检测到INTERNET连接。
|
|
|
+ */
|
|
|
+ public static boolean isConnectedAvailableNetwork(Context context) {
|
|
|
+ ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ Network network = cm.getActiveNetwork();
|
|
|
+ if (null == network) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
|
|
|
+ if (null == capabilities) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
|
+ && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * >= Android 10(Q版本)推荐
|
|
|
+ *
|
|
|
+ * 获取成功上网的网络类型
|
|
|
+ * value = {
|
|
|
+ * TRANSPORT_CELLULAR, 0 表示此网络使用蜂窝传输。
|
|
|
+ * TRANSPORT_WIFI, 1 表示此网络使用Wi-Fi传输。
|
|
|
+ * TRANSPORT_BLUETOOTH, 2 表示此网络使用蓝牙传输。
|
|
|
+ * TRANSPORT_ETHERNET, 3 表示此网络使用以太网传输。
|
|
|
+ * TRANSPORT_VPN, 4 表示此网络使用VPN传输。
|
|
|
+ * TRANSPORT_WIFI_AWARE, 5 表示此网络使用Wi-Fi感知传输。
|
|
|
+ * TRANSPORT_LOWPAN, 6 表示此网络使用LoWPAN传输。
|
|
|
+ * TRANSPORT_TEST, 7 指示此网络使用仅限测试的虚拟接口作为传输。
|
|
|
+ * TRANSPORT_USB, 8 表示此网络使用USB传输
|
|
|
+ * }
|
|
|
+ */
|
|
|
+ public static int getConnectedNetworkType(Context context) {
|
|
|
+ ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ Network network = cm.getActiveNetwork();
|
|
|
+ if (null == network) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
|
|
|
+ if (null == capabilities) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if (capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
|
|
|
+ if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
|
|
+ return NetworkCapabilities.TRANSPORT_CELLULAR;
|
|
|
+ } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
|
|
|
+ return NetworkCapabilities.TRANSPORT_WIFI;
|
|
|
+ } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
|
|
|
+ return NetworkCapabilities.TRANSPORT_BLUETOOTH;
|
|
|
+ } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
|
|
|
+ return NetworkCapabilities.TRANSPORT_ETHERNET;
|
|
|
+ } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
|
|
|
+ return NetworkCapabilities.TRANSPORT_VPN;
|
|
|
+ } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE)) {
|
|
|
+ return NetworkCapabilities.TRANSPORT_WIFI_AWARE;
|
|
|
+ } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_LOWPAN)) {
|
|
|
+ return NetworkCapabilities.TRANSPORT_LOWPAN;
|
|
|
+ } /*else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_USB)) {
|
|
|
+ return NetworkCapabilities.TRANSPORT_USB;
|
|
|
+ }*/
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断当前是否有网络连接,但是如果该连接的网络无法上网,也会返回true
|
|
|
+ */
|
|
|
+ public static boolean isNetConnection(Context context) {
|
|
|
+ ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
|
|
|
+
|
|
|
+ if (networkInfo != null && networkInfo.isConnected()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getNetworkInfoType(Context context) {
|
|
|
+ ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
|
|
|
+
|
|
|
+ if (networkInfo != null) {
|
|
|
+ return networkInfo.getType();
|
|
|
+ }
|
|
|
+
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ///////////////////////////**********WIFI***********/////////////////////////
|
|
|
+ public static boolean getWifiStatus(Context context){
|
|
|
+ //从系统服务中获取无线网络管理器
|
|
|
+ WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
|
|
+ return wm.isWifiEnabled();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<WifiConfiguration> getConfiguredWifiNetworks(Context context) {
|
|
|
+ WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
|
|
+ return wm.getConfiguredNetworks();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getConnectWifi(Context context) {
|
|
|
+ WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
|
|
+ if (wifiManager != null) {
|
|
|
+ WifiInfo info = wifiManager.getConnectionInfo();
|
|
|
+ if (info != null) {
|
|
|
+ return info.getSSID();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //连接wifi,优先preferSSID
|
|
|
+ public static boolean connectWifi(Context context, String preferSSID) {
|
|
|
+ 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) {
|
|
|
+ //优先连接原来最后一次连接的网络
|
|
|
+ /*if (!TextUtils.isEmpty(preferSSID)) {
|
|
|
+ for (WifiConfiguration wifiConfiguration: wifiConfigs) {
|
|
|
+ Log.e(TAG, "preferSSID: " + preferSSID + ", wifi ssid: " + wifiConfiguration.SSID);
|
|
|
+ if (preferSSID.equals(wifiConfiguration.SSID)) {
|
|
|
+ boolean connect = wifiManager.enableNetwork(wifiConfiguration.networkId, true);
|
|
|
+ Log.e(TAG, "connect prefer wifi: " + connect);
|
|
|
+ if (connect) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+
|
|
|
+ for (WifiConfiguration wifiConfiguration: wifiConfigs) {
|
|
|
+ for (ScanResult scanResult : scanResults) {
|
|
|
+ Log.e(TAG, "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可用
|
|
|
+ boolean connect = wifiManager.enableNetwork(wifiConfiguration.networkId, true);
|
|
|
+ Log.e(TAG, "connect wifi: " + connect);
|
|
|
+ if (connect) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ connected = false;
|
|
|
+ //判断是否开启了wifi,如开启了则尝试自动连接
|
|
|
+ boolean wifiState = NetworkUtils.getWifiStatus(context);
|
|
|
+ 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);
|
|
|
+ } catch (Exception e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }*/
|
|
|
+ } else {
|
|
|
+ //wifi扫描需要时间,延迟3s后再连接
|
|
|
+ try {
|
|
|
+ Thread.sleep(3000);
|
|
|
+ } catch (Exception e) {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ connected = NetworkUtils.connectWifi(context, "");
|
|
|
+
|
|
|
+ Log.e(TAG, "network unavailable...wifiState: " + wifiState + ", connected: " + connected);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ checkNetConnection = false;
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|