Android Question need an application that detect all Wi-fi available

TILogistic

Expert
Licensed User
Longtime User
?
test:
B4X:
   Sub Process_Globals
       Private wifiManager As JavaObject
       Private wifiList As List
   End Sub

   Sub Globals

   End Sub

   Sub Activity_Create(FirstTime As Boolean)
       ' Initialize the Wi-Fi Manager
       wifiManager.InitializeContext
       wifiManager = wifiManager.Initialize("android.net.wifi.WifiManager")
     
       ' Enable Wi-Fi if it's not already enabled
       If wifiManager.RunMethod("isWifiEnabled", Null) = False Then
           wifiManager.RunMethod("setWifiEnabled", Array(True))
       End If
     
       ' Scan for available Wi-Fi networks
       wifiManager.RunMethod("startScan", Null)
     
       ' Get the list of Wi-Fi networks
       wifiList = wifiManager.RunMethod("getScanResults", Null)
     
       ' Process the results
       For Each result As JavaObject In wifiList
           Dim ssid As String = result.GetField("SSID")
           Dim bssid As String = result.GetField("BSSID")
           Dim level As Integer = result.GetField("level")
         
           Log("SSID: " & ssid & ", BSSID: " & bssid & ", Level: " & level)
       Next
   End Sub

Ref:
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
You need a receiver module not a class to perform "android.net.wifi.WifiManager"
 
Upvote 0

Adamdam

Active Member
Licensed User
Longtime User
?
test:
B4X:
   Sub Process_Globals
       Private wifiManager As JavaObject
       Private wifiList As List
   End Sub

   Sub Globals

   End Sub

   Sub Activity_Create(FirstTime As Boolean)
       ' Initialize the Wi-Fi Manager
       wifiManager.InitializeContext
       wifiManager = wifiManager.Initialize("android.net.wifi.WifiManager")
    
       ' Enable Wi-Fi if it's not already enabled
       If wifiManager.RunMethod("isWifiEnabled", Null) = False Then
           wifiManager.RunMethod("setWifiEnabled", Array(True))
       End If
    
       ' Scan for available Wi-Fi networks
       wifiManager.RunMethod("startScan", Null)
    
       ' Get the list of Wi-Fi networks
       wifiList = wifiManager.RunMethod("getScanResults", Null)
    
       ' Process the results
       For Each result As JavaObject In wifiList
           Dim ssid As String = result.GetField("SSID")
           Dim bssid As String = result.GetField("BSSID")
           Dim level As Integer = result.GetField("level")
        
           Log("SSID: " & ssid & ", BSSID: " & bssid & ", Level: " & level)
       Next
   End Sub

Ref:
Many thanks for your support,
I just have two errors as in attached image:

1730415921181.png


1730415831603.png


How to solve them, please.
Best regards
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
see
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Many thanks for your support,
I just have two errors as in attached image:

View attachment 158177

View attachment 158175

How to solve them, please.
Best regards

In your project, wifiManager is a JavaObject, which does not have the simple Initialize method (ChatGPT code?).
JavaObject has 4 other initialization methods.

I'm not sure, but Try:
B4X:
Dim wifiManager As JavaObject
wifiManager.InitializeContext.RunMethod("getSystemService", Array("wifi"))


Dim level As Integer = result.GetField("level")
Dim level As Int = result.GetField("level")


(Obviously I haven't looked at the rest of the project and I don't know if it will work).
 
Last edited:
Upvote 0

Adamdam

Active Member
Licensed User
Longtime User
In your project, wifiManager is a JavaObject, which does not have the simple Initialize method (ChatGPT code?).
JavaObject has 4 other initialization methods.

I'm not sure, but Try:
B4X:
Dim wifiManager As JavaObject
wifiManager.InitializeContext.RunMethod("getSystemService", Array("wifi"))



Dim level As Int = result.GetField("level")


(Obviously I haven't looked at the rest of the project and I don't know if it will work).
Thanks for your effort and support.
I still can't catch any result.
At running, the error message occurred "startScan not found" and also "isWifiEnabled not found".
by the way I tested this code


it worked but just gave me the name of only one SSID network name if I connected with it, else it gave me un-known.
Sorry for your efforts, I still need to solve that,
best regards
 
Upvote 0

Adamdam

Active Member
Licensed User
Longtime User
I tried to get solution with ChatGPT, he gave me solution with WifiManager,
I found a problem with "WifiManager" that not defined,
ChatGPT tell it embedded, and I need to confirm for found "android.jar", I can't find it inside library directory,
Any help to solve that (i.e. I need to run WifiManager inside B4A)
Waiting for any solution.
Best regards

the code suggested from ChatGPT as following :
B4X:
Sub Process_Globals
    Private wifiManager As WifiManager
End Sub

Sub Globals
    Private ListView1 As ListView
    Private btnScan As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
   
    wifiManager.Initialize("WifiManager")
   
    If PermissionManager.CheckPermission("android.permission.ACCESS_FINE_LOCATION") = False Then
        PermissionManager.RequestPermission("android.permission.ACCESS_FINE_LOCATION")
    End If

    ListView1.SingleLineLayout.Label.TextSize = 14
    ListView1.SingleLineLayout.Label.TextColor = Colors.Black
   
    btnScan.Initialize("btnScan")
    btnScan.Text = "Scan Wi-Fi"
    Activity.AddView(btnScan, 10dip, 10dip, 100dip, 50dip)
   
    ListView1.Initialize("ListView1")
    Activity.AddView(ListView1, 10dip, 70dip, 300dip, 400dip)
End Sub

Sub btnScan_Click
    ' Check if Wi-Fi is enabled and start scan
    If wifiManager.WifiEnabled = False Then
        wifiManager.SetWifiEnabled(True)
    End If
    wifiManager.StartScan
End Sub

Sub WifiManager_ScanResultsAvailable
    Dim results As List = wifiManager.GetScanResults
    ListView1.Clear
    For Each result As Map In results
        Dim ssid As String = result.Get("SSID")
        Dim bssid As String = result.Get("BSSID")
        ListView1.AddSingleLine(ssid & " (" & bssid & ")")
    Next
End Sub

Sub Activity_PermissionResult(Permission As String, Result As Boolean)
    If Result Then
        ToastMessageShow("Permission granted", False)
    Else
        ToastMessageShow("Permission denied", False)
    End If
End Sub

the error at start of code at this definition : Private wifiManager As WifiManager

Waiting for any support
Best regards
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
the code suggested from ChatGPT as following :
That code is completely wrong.

You will need "many" things, the main one being a broadcast receiver.

It's hard; I've been trying since yesterday, hours and hours :mad:, without success and, unfortunately, only now I saw that StartScan is deprecated, so I don't really know what should be used.
 
Upvote 0

Adamdam

Active Member
Licensed User
Longtime User
That code is completely wrong.

You will need "many" things, the main one being a broadcast receiver.

It's hard; I've been trying since yesterday, hours and hours :mad:, without success and, unfortunately, only now I saw that StartScan is deprecated, so I don't really know what should be used.
Many thanks for your efforts.
So, what is the optimal solution ?

Best regards
 
Upvote 0
Top