package com.gordonlu.networkutilities; import android.app.Activity; import android.content.Context; import com.google.appinventor.components.annotations.*; import com.google.appinventor.components.common.ComponentCategory; import com.google.appinventor.components.runtime.AndroidNonvisibleComponent; import com.google.appinventor.components.runtime.ComponentContainer; import com.google.appinventor.components.runtime.EventDispatcher; import android.bluetooth.BluetoothAdapter; import android.net.ConnectivityManager; import android.content.BroadcastReceiver; import android.net.NetworkInfo; import android.util.Log; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; import android.net.wifi.WifiManager; import android.os.Build; import android.util.Log; import android.net.wifi.WifiInfo; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; import android.net.ConnectivityManager; import android.net.LinkProperties; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkInfo; import android.net.RouteInfo; import android.net.wifi.WifiManager; import android.os.Build; import android.provider.Settings; import android.util.Log; import androidx.annotation.RequiresApi; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.lang.reflect.Method; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @DesignerComponent( version = 1, description = "An extension that provides basic information about the network connectivity of the device.", category = ComponentCategory.EXTENSION, nonVisible = true, iconName = "https://docs.google.com/drawings/d/e/2PACX-1vQCI87PHLBF0jb8QWyYmIRQSjjNW3EFXf-qpsWCvBYkUQ9vEgPAB8SpxcMpblxNpbIYrjCjLrRLIU2c/pub?w=16&h=16") @SimpleObject(external = true) //Libraries @UsesLibraries(libraries = "") //Permissions @UsesPermissions({"android.permission.ACCESS_NETWORK_STATE", "android.permission.ACCESS_WIFI_STATE", "android.permission.BLUETOOTH_ADMIN", "android.permission.READ_PRIVILEGED_PHONE_STATE", "android.permission.CHANGE_WIFI_STATE", "android.permission.ACCESS_FINE_LOCATION"}) public class NetworkUtilities extends AndroidNonvisibleComponent { //Activity and Context private Context context; private Activity activity; public NetworkUtilities(ComponentContainer container){ super(container.$form()); this.activity = container.$context(); this.context = container.$context(); } @SimpleFunction (description = "Turns on Bluetooth without user interaction.") public void EnableBluetooth() { BluetoothAdapter.getDefaultAdapter().enable(); } @SimpleFunction (description = "Turns off Bluetooth without user interaction.") public void DisableBluetooth() { BluetoothAdapter.getDefaultAdapter().disable(); } @SimpleFunction (description = "Checks if Bluetooth is on.") public boolean IsBluetoothConnected() { BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { return false; } else if (!mBluetoothAdapter.isEnabled()) { return false; } else { return true; } } @SimpleFunction(description = "Returns true if the connection is through WiFi. If WiFi and mobile is connected, this will return true.") public boolean IsWiFiConnection() { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo(); NetworkInfo networkInfo = activeNetworkInfo; return activeNetworkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.getType() == 1; } @SimpleFunction(description = "Returns true if connection is through mobile connection. If WiFi and mobile is connected, this will return false.") public boolean IsMobileConnection() { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo(); NetworkInfo networkInfo = activeNetworkInfo; return activeNetworkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.getType() == 0; } @SimpleFunction(description = "Returns true if the device is using roaming.") public boolean IsRoaming() { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo(); NetworkInfo networkInfo = activeNetworkInfo; return activeNetworkInfo != null && networkInfo.isConnectedOrConnecting() && networkInfo.isRoaming(); } @SimpleFunction(description = "Return the basic service set identifier (BSSID) of the current access point.") public String WifiBssid() { WifiManager wifiMgr = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); if (wifiInfo.getBSSID() == null) { return ""; } else { return wifiInfo.getBSSID(); } } @SimpleFunction(description = "Return whether this adapter supports 5 GHz band.") public boolean Is5GHzSupported() { if (Build.VERSION.SDK_INT >= 21) { WifiManager wifiMgr = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE); return wifiMgr.is5GHzBandSupported(); } return false; } @SimpleFunction(description = "Returns the service set identifier (SSID) of the current 802.11 network.") public String WifiSsid() { WifiManager wifiMgr = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); if (wifiInfo.getSSID() == null) { return ""; } else { return wifiInfo.getSSID(); } } @SimpleFunction(description = "Returns the received WiFi signal strength indicator of the current 802.11 network, in dBm.") public int WifiSignalStrength() { WifiManager wifiMgr = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (IsWiFiConnection()) { return wifiMgr.getConnectionInfo().getRssi(); } return 0; } @SimpleFunction(description="Returns the current WiFi link speed in Mbps.") public int WifiLinkSpeed() { WifiManager wifiManager = (WifiManager)this.activity.getApplicationContext().getSystemService("wifi"); if (this.IsWiFiConnection()) { return wifiManager.getConnectionInfo().getLinkSpeed(); } return 0; } }