Android Question Scan wifi and MLwifi library

uniplan

Active Member
Licensed User
Longtime User
Hi to all...i need to scan the all wifi around me at the moment and know the ssid.
I test the MLwifi library:
https://www.b4x.com/android/help/mlwifi.html
but the wifi_ScanDone not triggered...why?
it necessary or possible to set a time limit to scan?
this is my code...thank you

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
  
    Dim myMLWifiObject As MLwifi
    Dim myMLScan As MLScan
    Dim rp As RuntimePermissions
    Type SSIDentry (SSID As String, BSSID As String, Index As String)
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
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("main")
    If FirstTime Then
        Dim numberOfValidPermissions As Int = 0
        rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
        wait for Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            ' Fine location permission OK
            numberOfValidPermissions = numberOfValidPermissions +1
        End If
        rp.CheckAndRequest(rp.PERMISSION_ACCESS_COARSE_LOCATION)
        wait for Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            ' COARSE location permission OK
            numberOfValidPermissions = numberOfValidPermissions + 1
        End If
        If numberOfValidPermissions = 2 Then
            Log("   ")
            Log("Doing the Scan...")
            myMLScan.startscan("wifi",False)
        Else
            Log("Permission not set")
        End If
    End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub wifi_ScanDone(Results() As String, Count As Int)
    Dim record As SSIDentry
    record.initialize
  
    Dim ListForList As List
    ListForList.Initialize
  
    Dim listofNetworks As List
    listofNetworks = myMLScan.listSavedNetworks
      
    Dim returnedEntry() As String
    For i = 0 To listofNetworks.Size-1
        returnedEntry = Regex.Split(",",listofNetworks.Get(i))
      
        record.ssid = returnedEntry(0).SubString2(1,returnedEntry(0).Length-1) 'Remove the quotes
        record.bssid = returnedEntry(1)
        record.index = returnedEntry(2)
      
        ListView1.AddTwoLines2(record.SSID,record.Index,record.SSID)
      
    Next
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
  
    Private connectionStart As Long = DateTime.Now
    Private connectionTimeOut As Long = 30000
  
    Dim selectedSSID As String
  
    selectedSSID = Value
    ToastMessageShow(selectedSSID,False)
  
    myMLWifiObject.disconnectWifiAP
    connectionStart = DateTime.Now
    Do Until myMLWifiObject.isWifiConnected = False
        Log("Waiting for Disconnect..." & (DateTime.Now - (connectionStart + connectionTimeOut)))
        Sleep(0)
        If DateTime.Now > connectionStart + connectionTimeOut Then
            Log("DISCONNECT FAILED")
            Return
        End If
    Loop
      
    Log("Call to connect to " & selectedSSID & " returned " & myMLWifiObject.connectWifiAP(selectedSSID))
    Log("The current SSID is " & myMLWifiObject.WifiSSID)
  
    connectionStart = DateTime.Now
    Do Until myMLWifiObject.isWifiConnected
        Log("Waiting for MLWifi to connect..." & (DateTime.Now - (connectionStart + connectionTimeOut)))
        Sleep(0)
        If DateTime.Now > connectionStart + connectionTimeOut Then
            Log("CONNECTION FAILED")
            Log("Now MLwifi is connected to " & myMLWifiObject.WifiSSID)
            Return
        End If
    Loop
    Log("Managed to connect to " & myMLWifiObject.WifiSSID)
    Log("Asked to connect to " & selectedSSID)
    If myMLWifiObject.WifiSSID <> selectedSSID Then
        Log("Wrong SSID!!!")
    Else
        Log("Correct SSID - hurrah")
    End If
    ToastMessageShow(myMLWifiObject.ActiveNetworkTypeName,False)
  
End Sub
 

vhd_bgh

Member
Hi to all...i need to scan the all wifi around me at the moment and know the ssid.
I test the MLwifi library:
https://www.b4x.com/android/help/mlwifi.html
but the wifi_ScanDone not triggered...why?
it necessary or possible to set a time limit to scan?
this is my code...thank you

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
 
    Dim myMLWifiObject As MLwifi
    Dim myMLScan As MLScan
    Dim rp As RuntimePermissions
    Type SSIDentry (SSID As String, BSSID As String, Index As String)
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
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("main")
    If FirstTime Then
        Dim numberOfValidPermissions As Int = 0
        rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
        wait for Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            ' Fine location permission OK
            numberOfValidPermissions = numberOfValidPermissions +1
        End If
        rp.CheckAndRequest(rp.PERMISSION_ACCESS_COARSE_LOCATION)
        wait for Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            ' COARSE location permission OK
            numberOfValidPermissions = numberOfValidPermissions + 1
        End If
        If numberOfValidPermissions = 2 Then
            Log("   ")
            Log("Doing the Scan...")
            myMLScan.startscan("wifi",False)
        Else
            Log("Permission not set")
        End If
    End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub wifi_ScanDone(Results() As String, Count As Int)
    Dim record As SSIDentry
    record.initialize
 
    Dim ListForList As List
    ListForList.Initialize
 
    Dim listofNetworks As List
    listofNetworks = myMLScan.listSavedNetworks
     
    Dim returnedEntry() As String
    For i = 0 To listofNetworks.Size-1
        returnedEntry = Regex.Split(",",listofNetworks.Get(i))
     
        record.ssid = returnedEntry(0).SubString2(1,returnedEntry(0).Length-1) 'Remove the quotes
        record.bssid = returnedEntry(1)
        record.index = returnedEntry(2)
     
        ListView1.AddTwoLines2(record.SSID,record.Index,record.SSID)
     
    Next
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
 
    Private connectionStart As Long = DateTime.Now
    Private connectionTimeOut As Long = 30000
 
    Dim selectedSSID As String
 
    selectedSSID = Value
    ToastMessageShow(selectedSSID,False)
 
    myMLWifiObject.disconnectWifiAP
    connectionStart = DateTime.Now
    Do Until myMLWifiObject.isWifiConnected = False
        Log("Waiting for Disconnect..." & (DateTime.Now - (connectionStart + connectionTimeOut)))
        Sleep(0)
        If DateTime.Now > connectionStart + connectionTimeOut Then
            Log("DISCONNECT FAILED")
            Return
        End If
    Loop
     
    Log("Call to connect to " & selectedSSID & " returned " & myMLWifiObject.connectWifiAP(selectedSSID))
    Log("The current SSID is " & myMLWifiObject.WifiSSID)
 
    connectionStart = DateTime.Now
    Do Until myMLWifiObject.isWifiConnected
        Log("Waiting for MLWifi to connect..." & (DateTime.Now - (connectionStart + connectionTimeOut)))
        Sleep(0)
        If DateTime.Now > connectionStart + connectionTimeOut Then
            Log("CONNECTION FAILED")
            Log("Now MLwifi is connected to " & myMLWifiObject.WifiSSID)
            Return
        End If
    Loop
    Log("Managed to connect to " & myMLWifiObject.WifiSSID)
    Log("Asked to connect to " & selectedSSID)
    If myMLWifiObject.WifiSSID <> selectedSSID Then
        Log("Wrong SSID!!!")
    Else
        Log("Correct SSID - hurrah")
    End If
    ToastMessageShow(myMLWifiObject.ActiveNetworkTypeName,False)
 
End Sub
i have this problem too !!! but its in android 8 or above and works correctly in android 4 and 5
do u find any solution ???
 
Upvote 0

Xicu

Active Member
Licensed User
Longtime User
I test the MLwifi library:
https://www.b4x.com/android/help/mlwifi.html
but the wifi_ScanDone not triggered...why?
Jem Miller has stopped updating the library and left the source code. But our friend @bgsoft has kept updating it

Are you using this library version?

Its works perfectly!
 
Last edited:
Upvote 0

vhd_bgh

Member
Hi to all...i need to scan the all wifi around me at the moment and know the ssid.
I test the MLwifi library:
https://www.b4x.com/android/help/mlwifi.html
but the wifi_ScanDone not triggered...why?
it necessary or possible to set a time limit to scan?
this is my code...thank you

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
 
    Dim myMLWifiObject As MLwifi
    Dim myMLScan As MLScan
    Dim rp As RuntimePermissions
    Type SSIDentry (SSID As String, BSSID As String, Index As String)
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
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("main")
    If FirstTime Then
        Dim numberOfValidPermissions As Int = 0
        rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
        wait for Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            ' Fine location permission OK
            numberOfValidPermissions = numberOfValidPermissions +1
        End If
        rp.CheckAndRequest(rp.PERMISSION_ACCESS_COARSE_LOCATION)
        wait for Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            ' COARSE location permission OK
            numberOfValidPermissions = numberOfValidPermissions + 1
        End If
        If numberOfValidPermissions = 2 Then
            Log("   ")
            Log("Doing the Scan...")
            myMLScan.startscan("wifi",False)
        Else
            Log("Permission not set")
        End If
    End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub wifi_ScanDone(Results() As String, Count As Int)
    Dim record As SSIDentry
    record.initialize
 
    Dim ListForList As List
    ListForList.Initialize
 
    Dim listofNetworks As List
    listofNetworks = myMLScan.listSavedNetworks
     
    Dim returnedEntry() As String
    For i = 0 To listofNetworks.Size-1
        returnedEntry = Regex.Split(",",listofNetworks.Get(i))
     
        record.ssid = returnedEntry(0).SubString2(1,returnedEntry(0).Length-1) 'Remove the quotes
        record.bssid = returnedEntry(1)
        record.index = returnedEntry(2)
     
        ListView1.AddTwoLines2(record.SSID,record.Index,record.SSID)
     
    Next
End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
 
    Private connectionStart As Long = DateTime.Now
    Private connectionTimeOut As Long = 30000
 
    Dim selectedSSID As String
 
    selectedSSID = Value
    ToastMessageShow(selectedSSID,False)
 
    myMLWifiObject.disconnectWifiAP
    connectionStart = DateTime.Now
    Do Until myMLWifiObject.isWifiConnected = False
        Log("Waiting for Disconnect..." & (DateTime.Now - (connectionStart + connectionTimeOut)))
        Sleep(0)
        If DateTime.Now > connectionStart + connectionTimeOut Then
            Log("DISCONNECT FAILED")
            Return
        End If
    Loop
     
    Log("Call to connect to " & selectedSSID & " returned " & myMLWifiObject.connectWifiAP(selectedSSID))
    Log("The current SSID is " & myMLWifiObject.WifiSSID)
 
    connectionStart = DateTime.Now
    Do Until myMLWifiObject.isWifiConnected
        Log("Waiting for MLWifi to connect..." & (DateTime.Now - (connectionStart + connectionTimeOut)))
        Sleep(0)
        If DateTime.Now > connectionStart + connectionTimeOut Then
            Log("CONNECTION FAILED")
            Log("Now MLwifi is connected to " & myMLWifiObject.WifiSSID)
            Return
        End If
    Loop
    Log("Managed to connect to " & myMLWifiObject.WifiSSID)
    Log("Asked to connect to " & selectedSSID)
    If myMLWifiObject.WifiSSID <> selectedSSID Then
        Log("Wrong SSID!!!")
    Else
        Log("Correct SSID - hurrah")
    End If
    ToastMessageShow(myMLWifiObject.ActiveNetworkTypeName,False)
 
End Sub
Have you tested this library on Samsung devices?
My app works well on all devices except Samsung
( "ScanDone" function doesn't return anything)
 
Upvote 0
Top