Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim timer1 As Timer
Dim timerDos As Timer
Dim job1 As HttpJob
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private ListView1 As ListView
Dim WiFiConnect1 As WiFiConnect
Dim isInit As Boolean = False
Private btnRefresh As Button
Private lblWifiSig As Label
Private lblWifiSec As Label
Private etWifiPwd As EditText
Private cbxWifiAdv As CheckBox
Dim ResultsBkp As List
Dim cnt As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Layout1")
'Wait For (PreferWifiRouting) Complete (Success As Boolean)
WiFiConnect1.Initialize
ResultsBkp.Initialize
timer1.Initialize("timer1", 1000)
If Not(isInit) Then
isInit = True
End If
refresh
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub timer1_tick()
If cnt > 4 Then
timer1.Enabled = False
ProgressDialogHide
Else
cnt = cnt + 1
refresh
End If
End Sub
Sub tryAdd(Value As Int)
Dim strSplit() As String = Regex.Split(",", ResultsBkp.Get(Value))
Dim iSignal As Int = strSplit(2).Trim
Dim sSignal As String
Dim wifiPasswd As String
Dim security As Int
Dim dialog As CustomLayoutDialog
Dim sf As Object = dialog.ShowAsync(strSplit(0), "Conecte", "Cancel", "", Null, False)
dialog.SetSize(100%x, 300dip)
Wait For (sf) Dialog_Ready (DialogPanel As Panel)
DialogPanel.LoadLayout("wifiDialog")
If iSignal >= -30 Then
sSignal = "Excellent"
Else If strSplit(2).Trim >= -67 Then
sSignal = "Very Good"
Else If strSplit(2).Trim >= -70 Then
sSignal = "Good"
Else If strSplit(2).Trim >= -80 Then
sSignal = "Bad"
Else
sSignal = "Very Bad"
End If
lblWifiSec.Text = strSplit(1)
lblWifiSig.Text = sSignal
Wait For (sf) Dialog_Result (Result As Int)
wifiPasswd = etWifiPwd.Text
etWifiPwd.Enabled = False
If strSplit(1).Contains("WPA") Then
security = WiFiConnect1.WIFI_WPA
Else If strSplit(1).Contains("WEP") Then
security = WiFiConnect1.WIFI_WEP
Else
security = WiFiConnect1.WIFI_OPEN
End If
If Result = -1 Then
WiFiConnect1.ConnectToSSID(security, strSplit(0), wifiPasswd)
ToastMessageShow("Connect to: " & strSplit(0),False)
waitRefresh
ToastMessageShow("ACA ACA Estoy !!! Gil",True)
Sleep(4000)
'timerDos.Initialize("timerDos",3000)
'timerDos.Enabled = True
job1.Initialize("Job1", Me)
job1.Download("http://100.100.0.10/")
' job1.GetRequest.Timeout = 10000 'timeout will be set before the download will start
End If
End Sub
Sub waitRefresh()
' WiFiConnect1.setWifiEnabled(False)
' WiFiConnect1.setWifiEnabled(True)
ProgressDialogShow("Please, wait...")
cnt = 0
timer1.Enabled = True
refresh
End Sub
Sub btnRefresh_Click
refresh
End Sub
Sub refresh()
Dim strSplit() As String
Dim strSplit2() As String
Dim strSSID As String
Dim strBSSID As String
Dim strSECURITY As String
Dim strLEVEL As String
Dim strFREQUENCY As String
Dim list1 As List
Dim listConfigured As List
Dim sSignal As String
' Dim listState As List
Dim sState As String
Dim sRank As String
Try
list1.Initialize
listConfigured.Initialize
' listState.Initialize
If WiFiConnect1.getWifiState <> WiFiConnect1.WIFI_STATE_ENABLED Then
WiFiConnect1.setWifiEnabled(True)
End If
WiFiConnect1.startScan
list1 = WiFiConnect1.getScanResults
listConfigured = WiFiConnect1.getConfiguredNetworks
ListView1.Clear
ResultsBkp.Clear
' listState.Clear
For i = 0 To list1.Size -1
' Msgbox(list1.Get(i), "")
strSSID = ""
strSECURITY = ""
strSplit = Regex.Split(",", list1.Get(i))
For j = 0 To strSplit.Length -1
strSplit2 = Regex.Split(": ", strSplit(j))
If strSplit2.Length = 2 Then
If strSplit2(0).Contains("BSSID") Then
strBSSID = strSplit2(1).Trim
Else If strSplit2(0).Contains("SSID") Then
strSSID = strSplit2(1).Trim
Else If strSplit2(0).Contains("capabilities") Then
strSECURITY = strSplit2(1).Trim
Else If strSplit2(0).Contains("level") Then
strLEVEL = strSplit2(1).Trim
Else If strSplit2(0).Contains("frequency") Then
strFREQUENCY = strSplit2(1).Trim
End If
End If
Next
If strSECURITY.Contains("WPA2") Then
strSECURITY = "WPA2"
Else If strSECURITY.Contains("WPA") Then
strSECURITY = "WPA"
Else If strSECURITY.Contains("WEP") Then
strSECURITY = "WEP"
Else
strSECURITY = "OPEN"
End If
If strLEVEL >= -30 Then
sSignal = "Excellent"
Else If strLEVEL >= -67 Then
sSignal = "Very Good"
Else If strLEVEL >= -70 Then
sSignal = "Good"
Else If strLEVEL >= -80 Then
sSignal = "Bad"
Else
sSignal = "Very Bad"
End If
sState = ""
sRank = 0
For k = 0 To listConfigured.Size -1
' Msgbox(listConfigured.Get(k), "")
Dim strCont As String = listConfigured.Get(k)
If strCont.Contains(Chr(34) & strSSID & Chr(34)) Then
sRank = strCont.SubString2(strCont.IndexOf("ID: ")+4, _
strCont.IndexOf("SSID: "))
' Msgbox(sRank, "")
If IsNumber(sRank) = False Then
sRank = 0
End If
If strCont.Contains("DSBLE") Then
sState = "Saved"
Else
sState = "Connected"
End If
End If
Next
If sState = "Saved" Then
ListView1.AddTwoLines2(strSSID & " [" & strBSSID & "]", _
"Saved, Signal: " & sSignal & _
" (" & strLEVEL & " dBm)", i)
Else If sState = "Connected" Then
ListView1.AddTwoLines2(strSSID & " [" & strBSSID & "]", _
"Connected, Signal: " & sSignal & _
" (" & strLEVEL & " dBm)", i)
Else
ListView1.AddTwoLines2(strSSID & " [" & strBSSID & "]", _
"Security: " & strSECURITY & _
", Signal: " & sSignal & _
" (" & strLEVEL & " dBm)", i)
End If
ResultsBkp.Add(strSSID & "," & _
strSECURITY & "," & _
strLEVEL & "," & _
strFREQUENCY & "," & _
sState & "," & _
sRank)
Next
Catch
Log(LastException)
End Try
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
Dim strSplit() As String = Regex.Split(",", ResultsBkp.Get(Value))
Dim result As Int
' ResultsBkp.Add(strSSID & "," & _
' strSECURITY & "," & _
' strLEVEL & "," & _
' strFREQUENCY & "," & _
' sState & "," & _
' sRank)
If WiFiConnect1.getWifiState <> WiFiConnect1.WIFI_STATE_ENABLED Then
WiFiConnect1.setWifiEnabled(True)
End If
If strSplit(4) <> "" Then
If strSplit(4) = "Connected" Then
result = Msgbox2("", strSplit(0), "Desconectar", "", "Modify", Null)
' ToastMessageShow(result,False)
If result = -1 Then ' "Disconnect"
WiFiConnect1.DisconnectCurrentSSID
waitRefresh
Else If result = -2 Then ' "Modify"
tryAdd(Value)
End If
Else
result = Msgbox2("", strSplit(0), "Conectar", "", "Modify", Null)
' ToastMessageShow(result,False)
If result = -2 Then ' "Modify"
tryAdd(Value)
Else If result = -1 Then ' "Connect"
' WiFiConnect1.enableNetwork(WiFiConnect1.getNetworkIdBySSID(strSplit(0)), True)
WiFiConnect1.enableNetwork(strSplit(5), True)
waitRefresh
ToastMessageShow("Aca Termine", True)
End If
End If
Else
tryAdd(Value)
End If
End Sub
Sub ListView1_ItemLongClick (Position As Int, Value As Object)
Dim strSplit() As String = Regex.Split(",", ResultsBkp.Get(Value))
Dim result As Int
' timer1.Enabled = False
' ResultsBkp.Add(strSSID & "," & _
' strSECURITY & "," & _
' strLEVEL & "," & _
' strFREQUENCY & "," & _
' sState & "," & _
' sRank)
If strSplit(4) <> "" Then
result = Msgbox2("", strSplit(0), "Disable", "", "Cancel", Null)
If result = -1 Then ' "Forget"
If strSplit(4) = "Connected" Then
WiFiConnect1.DisconnectCurrentSSID
End If
' WiFiConnect1.getNetworkIdBySSID(strSplit(0))
WiFiConnect1.disableNetwork( WiFiConnect1.getNetworkIdBySSID(strSplit(0)))
End If
End If
refresh
End Sub
Sub cbxWifiAdv_CheckedChange(Checked As Boolean)
ToastMessageShow("This is not implemented yet",False)
End Sub
Sub JobDone(job As HttpJob)
If job.Success Then
ToastMessageShow("contesto OK !!",True)
Else
ToastMessageShow("Errorrrrrr",True)
'timerDos.Enabled = True
End If
End Sub
Sub timerDos_tick
timerDos.Enabled = False
job1.Initialize("Job1", Me)
job1.Download("http://100.100.0.10/")
End Sub
Sub PreferWifiRouting As ResumableSub
Dim p As Phone
If p.SdkVersion >= 21 Then
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim builder As JavaObject
builder.InitializeNewInstance("android.net.NetworkRequest.Builder", Null)
Dim manager As JavaObject = ctxt.RunMethod("getSystemService", Array("connectivity"))
builder.RunMethod("addTransportType", Array(1)) 'NetworkCapabilities.TRANSPORT_WIFI
Dim event As JavaObject
event.InitializeNewInstance(Application.PackageName & ".main$NetworkCallback", Null)
manager.RunMethod("requestNetwork", Array(builder.RunMethod("build", Null), event))
Wait For network_available (Network As Object)
ToastMessageShow("Network found.",False)
'Log("Network found.")
Dim cm As JavaObject
Return cm.InitializeStatic("android.net.ConnectivityManager").RunMethod("setProcessDefaultNetwork", Array(Network))
End If
Return False
End Sub
#if Java
public static class NetworkCallback extends android.net.ConnectivityManager.NetworkCallback {
public void onAvailable(android.net.Network network) {
processBA.raiseEventFromUI(null, "network_available", network);
}
}
#End If