|
|
|
@ -3,6 +3,9 @@ package com.cloud.diplomaticquarterapp.utils
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Context
|
|
|
|
import android.net.ConnectivityManager
|
|
|
|
import android.net.ConnectivityManager
|
|
|
|
|
|
|
|
import android.net.Network
|
|
|
|
|
|
|
|
import android.net.NetworkCapabilities
|
|
|
|
|
|
|
|
import android.net.NetworkRequest
|
|
|
|
import android.net.wifi.ScanResult
|
|
|
|
import android.net.wifi.ScanResult
|
|
|
|
import android.net.wifi.WifiConfiguration
|
|
|
|
import android.net.wifi.WifiConfiguration
|
|
|
|
import android.net.wifi.WifiManager
|
|
|
|
import android.net.wifi.WifiManager
|
|
|
|
@ -17,16 +20,18 @@ import java.util.*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("MissingPermission")
|
|
|
|
@SuppressLint("MissingPermission")
|
|
|
|
class HMG_Wifi(flutterMainActivity: FlutterMainActivity) {
|
|
|
|
class HMG_Wifi_(flutterMainActivity: FlutterMainActivity) {
|
|
|
|
val TAG = "WIFI"
|
|
|
|
val TAG = "WIFI"
|
|
|
|
|
|
|
|
val TEST = true
|
|
|
|
|
|
|
|
|
|
|
|
var context = flutterMainActivity;
|
|
|
|
var context = flutterMainActivity;
|
|
|
|
var completionListener: ((status:Boolean, message:String) -> Unit)? = null
|
|
|
|
var completionListener: ((status: Boolean, message: String) -> Unit)? = null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private var SSID = "HMG-GUEST"
|
|
|
|
private var SSID = "HMG-GUEST"
|
|
|
|
private var USER_NAME = ""
|
|
|
|
private var USER_NAME = ""
|
|
|
|
private var PASSWORD = ""
|
|
|
|
private var PASSWORD = ""
|
|
|
|
|
|
|
|
var NETWORK_ID = -1 // HMG-GUEST Assigned Network ID by Android
|
|
|
|
private lateinit var PATIENT_ID:String
|
|
|
|
private lateinit var PATIENT_ID:String
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Helpful:
|
|
|
|
* Helpful:
|
|
|
|
@ -41,7 +46,10 @@ class HMG_Wifi(flutterMainActivity: FlutterMainActivity) {
|
|
|
|
* Helpful:
|
|
|
|
* Helpful:
|
|
|
|
* http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-programmatically
|
|
|
|
* http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-programmatically
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
fun connectToWifiNetworkWith(patientId:String): HMG_Wifi {
|
|
|
|
fun connectToWifiNetworkWith(patientId: String): HMG_Wifi_ {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val connectivityManager = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
|
|
|
|
|
|
|
|
|
|
PATIENT_ID = patientId
|
|
|
|
PATIENT_ID = patientId
|
|
|
|
|
|
|
|
|
|
|
|
val security = "OPEN"
|
|
|
|
val security = "OPEN"
|
|
|
|
@ -66,16 +74,16 @@ class HMG_Wifi(flutterMainActivity: FlutterMainActivity) {
|
|
|
|
// Then, you need to add it to Android wifi manager settings:
|
|
|
|
// Then, you need to add it to Android wifi manager settings:
|
|
|
|
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
|
|
|
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
|
|
|
|
|
|
|
|
|
|
|
val networkId = wifiManager.addNetwork(conf)
|
|
|
|
NETWORK_ID = wifiManager.addNetwork(conf)
|
|
|
|
Log.d(TAG, "Network ID: $networkId")
|
|
|
|
Log.d(TAG, "Network ID: $NETWORK_ID")
|
|
|
|
|
|
|
|
|
|
|
|
//wifiManager.disconnect();
|
|
|
|
//wifiManager.disconnect();
|
|
|
|
val result = wifiManager.enableNetwork(networkId, true)
|
|
|
|
val result = wifiManager.enableNetwork(NETWORK_ID, true)
|
|
|
|
//wifiManager.reconnect();
|
|
|
|
//wifiManager.reconnect();
|
|
|
|
wifiManager.saveConfiguration()
|
|
|
|
wifiManager.saveConfiguration()
|
|
|
|
|
|
|
|
|
|
|
|
if(result == true){
|
|
|
|
if(result == true){
|
|
|
|
authNetworkConnection(networkId);
|
|
|
|
authNetworkConnection(NETWORK_ID);
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
completionListener?.let { it(false, "Error connecting to HMG network") }
|
|
|
|
completionListener?.let { it(false, "Error connecting to HMG network") }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -83,38 +91,36 @@ class HMG_Wifi(flutterMainActivity: FlutterMainActivity) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private var authTimer:Timer? = null
|
|
|
|
private var authTimer:Timer? = null
|
|
|
|
fun authNetworkConnection(networkId:Int){
|
|
|
|
fun authNetworkConnection(networkId: Int){
|
|
|
|
authTimer = Timer()
|
|
|
|
authTimer = Timer()
|
|
|
|
authTimer?.scheduleAtFixedRate( object : TimerTask(){
|
|
|
|
authTimer?.scheduleAtFixedRate(object : TimerTask() {
|
|
|
|
override fun run() {
|
|
|
|
override fun run() {
|
|
|
|
if (connectedNetworkId() == networkId && connectedNetworkIPAddress() > 0){
|
|
|
|
if (connectedNetworkId() == networkId && connectedNetworkIPAddress() > 0) {
|
|
|
|
authServerCall()
|
|
|
|
authServerCall()
|
|
|
|
authTimer?.cancel()
|
|
|
|
authTimer?.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
},2000,1000)
|
|
|
|
}, 2000, 1000)
|
|
|
|
|
|
|
|
|
|
|
|
// If wifi not connected in 5 sec terminate with fail status
|
|
|
|
// If wifi not connected in 5 sec terminate with fail status
|
|
|
|
Timer().schedule(object : TimerTask(){
|
|
|
|
Timer().schedule(object : TimerTask() {
|
|
|
|
override fun run() {
|
|
|
|
override fun run() {
|
|
|
|
if (null != authTimer) {
|
|
|
|
if (null != authTimer) {
|
|
|
|
authTimer?.cancel()
|
|
|
|
authTimer?.cancel()
|
|
|
|
completionListener?.let { it(false, "Error connecting to HMG network") }
|
|
|
|
completionListener?.let { it(false, "Error connecting to HMG network") }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},5000)
|
|
|
|
}, 5000)
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun authServerCall(){
|
|
|
|
fun authServerCall(){
|
|
|
|
fun forceCallOverWifi(){
|
|
|
|
|
|
|
|
val connectivityManager = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
|
|
|
|
|
|
connectivityManager.networkPreference = ConnectivityManager.TYPE_WIFI
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun call(){
|
|
|
|
fun call(){
|
|
|
|
forceCallOverWifi()
|
|
|
|
|
|
|
|
|
|
|
|
forceNetworkCallOverWifi()
|
|
|
|
|
|
|
|
|
|
|
|
val params = listOf("cmd" to "authenticate", "password" to PASSWORD, "user" to USER_NAME)
|
|
|
|
val params = listOf("cmd" to "authenticate", "password" to PASSWORD, "user" to USER_NAME)
|
|
|
|
val serverUrl = "https://captiveportal-login.hmg.com/cgi-bin/login"
|
|
|
|
val serverUrl = "https://captiveportal-login.hmg.com/cgi-bin/login"
|
|
|
|
// val serverUrl = "http://192.168.102.223/cgi-bin/login"
|
|
|
|
// val serverUrl = "http://192.168.102.223/cgi-bin/login"
|
|
|
|
@ -122,14 +128,14 @@ class HMG_Wifi(flutterMainActivity: FlutterMainActivity) {
|
|
|
|
.httpPost(params)
|
|
|
|
.httpPost(params)
|
|
|
|
.timeout(10000)
|
|
|
|
.timeout(10000)
|
|
|
|
.response { request, response, result ->
|
|
|
|
.response { request, response, result ->
|
|
|
|
Log.v(TAG,response.statusCode.toString())
|
|
|
|
Log.v(TAG, response.statusCode.toString())
|
|
|
|
|
|
|
|
|
|
|
|
haveInternet { have ->
|
|
|
|
haveInternet { have ->
|
|
|
|
if(have){
|
|
|
|
if(have){
|
|
|
|
Log.v(TAG,"Connected to internet via $SSID network at HMG")
|
|
|
|
Log.v(TAG, "Connected to internet via $SSID network at HMG")
|
|
|
|
completionListener?.let { it(true, "Successfully connected to the internet") }
|
|
|
|
completionListener?.let { it(true, "Successfully connected to the internet") }
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
Log.e(TAG,"failed to connect to internet via $SSID network at HMG")
|
|
|
|
Log.e(TAG, "failed to connect to internet via $SSID network at HMG")
|
|
|
|
completionListener?.let { it(false, "Authentication failed or you are already using your credentials on another device") }
|
|
|
|
completionListener?.let { it(false, "Authentication failed or you are already using your credentials on another device") }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -142,21 +148,23 @@ class HMG_Wifi(flutterMainActivity: FlutterMainActivity) {
|
|
|
|
call()
|
|
|
|
call()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
completionListener?.let { it(false,"You must have active internet connection to connect with HMG Network") }
|
|
|
|
completionListener?.let { it(false, "You must have active internet connection to connect with HMG Network") }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun haveInternet(completion:((status:Boolean)->Unit)){
|
|
|
|
fun haveInternet(completion: ((status: Boolean) -> Unit)){
|
|
|
|
"https://captive.apple.com".httpGet().response {request, response, result ->
|
|
|
|
if (TEST)
|
|
|
|
val have = response.statusCode == 200 && String(response.data).contains("<TITLE>Success</TITLE>",true)
|
|
|
|
completion(true)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"https://captive.apple.com".httpGet().response { request, response, result ->
|
|
|
|
|
|
|
|
val have = response.statusCode == 200 && String(response.data).contains("<TITLE>Success</TITLE>", true)
|
|
|
|
completion(have)
|
|
|
|
completion(have)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun getAuthCredentials(completion:(() -> Unit)){
|
|
|
|
fun getAuthCredentials(completion: (() -> Unit)){
|
|
|
|
val test = true
|
|
|
|
if (TEST){
|
|
|
|
if (test){
|
|
|
|
|
|
|
|
USER_NAME = "2300"
|
|
|
|
USER_NAME = "2300"
|
|
|
|
PASSWORD = "1820"
|
|
|
|
PASSWORD = "1820"
|
|
|
|
completion()
|
|
|
|
completion()
|
|
|
|
@ -166,7 +174,7 @@ class HMG_Wifi(flutterMainActivity: FlutterMainActivity) {
|
|
|
|
val jsonBody = """{"PatientID":$PATIENT_ID}"""
|
|
|
|
val jsonBody = """{"PatientID":$PATIENT_ID}"""
|
|
|
|
API.WIFI_CREDENTIALS
|
|
|
|
API.WIFI_CREDENTIALS
|
|
|
|
.httpPost()
|
|
|
|
.httpPost()
|
|
|
|
.jsonBody(jsonBody,Charsets.UTF_8)
|
|
|
|
.jsonBody(jsonBody, Charsets.UTF_8)
|
|
|
|
.response { request, response, result ->
|
|
|
|
.response { request, response, result ->
|
|
|
|
val jsonString = String(response.data)
|
|
|
|
val jsonString = String(response.data)
|
|
|
|
Log.d(TAG, "JSON $jsonString")
|
|
|
|
Log.d(TAG, "JSON $jsonString")
|
|
|
|
@ -176,7 +184,7 @@ class HMG_Wifi(flutterMainActivity: FlutterMainActivity) {
|
|
|
|
val jsonObject = JSONObject(jsonString)
|
|
|
|
val jsonObject = JSONObject(jsonString)
|
|
|
|
if(!jsonObject.getString("ErrorMessage").equals("null")){
|
|
|
|
if(!jsonObject.getString("ErrorMessage").equals("null")){
|
|
|
|
val errorMsg = jsonObject.getString("ErrorMessage")
|
|
|
|
val errorMsg = jsonObject.getString("ErrorMessage")
|
|
|
|
completionListener?.let { it(false,errorMsg) }
|
|
|
|
completionListener?.let { it(false, errorMsg) }
|
|
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
jsonObject.getJSONArray("Hmg_SMS_Get_By_ProjectID_And_PatientIDList").let { array ->
|
|
|
|
jsonObject.getJSONArray("Hmg_SMS_Get_By_ProjectID_And_PatientIDList").let { array ->
|
|
|
|
@ -186,18 +194,45 @@ class HMG_Wifi(flutterMainActivity: FlutterMainActivity) {
|
|
|
|
PASSWORD = object_.getString("Password")
|
|
|
|
PASSWORD = object_.getString("Password")
|
|
|
|
completion()
|
|
|
|
completion()
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
completionListener?.let { it(false,"Failed to get your internet credentials") }
|
|
|
|
completionListener?.let { it(false, "Failed to get your internet credentials") }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
completionListener?.let { it(false,"Failed to get your internet credentials") }
|
|
|
|
completionListener?.let { it(false, "Failed to get your internet credentials") }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun forceNetworkCallOverWifi(){
|
|
|
|
|
|
|
|
val connectivityManager = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
|
|
|
|
|
|
// val network = Network
|
|
|
|
|
|
|
|
// connectivityManager.activeNetwork
|
|
|
|
|
|
|
|
// Exit app if Network disappears.
|
|
|
|
|
|
|
|
// Exit app if Network disappears.
|
|
|
|
|
|
|
|
// val networkCapabilities: NetworkCapabilities = ConnectivityManager.from(context).getNetworkCapabilities(network)
|
|
|
|
|
|
|
|
// val networkCapabilities: NetworkCapabilities = connectivityManager.getNetworkCapabilities(network)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (networkCapabilities == null) {
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val mNetworkCallback = object : ConnectivityManager.NetworkCallback() {
|
|
|
|
|
|
|
|
override fun onLost(lostNetwork: Network?) {
|
|
|
|
|
|
|
|
// if (network.equals(lostNetwork)){
|
|
|
|
|
|
|
|
// //GlyphLayout.done(false)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
val builder: NetworkRequest.Builder = NetworkRequest.Builder()
|
|
|
|
|
|
|
|
// for (transportType in networkCapabilities.getTransportTypes()) {
|
|
|
|
|
|
|
|
// builder.addTransportType(transportType)
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
connectivityManager.registerNetworkCallback(builder.build(), mNetworkCallback)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Helpful:
|
|
|
|
* Helpful:
|
|
|
|
* http://stackoverflow.com/questions/6517314/android-wifi-connection-programmatically
|
|
|
|
* http://stackoverflow.com/questions/6517314/android-wifi-connection-programmatically
|