Android Question How to use List returned by MLScan.listSavedNetworks ? <SOLVED>

SimonInCanada

Member
Licensed User
The documentation for MLWifi/MLScan 3.03 says that listSavedNetworks "Returns a comma separated list of: SSID,BSSID,Network ID number".

Reading the numbers from my phone then outputting with Log() shows

B4X:
(ArrayList) ["WIFI-ONDA",null,11, "goatgarden",null,33, "JoesGuests 2016",null,28, "Wyndham-Guest",null,37, "Wyndham-Lobby",null,36, "molo2",any,1, "molo1",null,15, "FVGWiFi",null,6, "LIDO 2B",null,26, "__Official TempleBar WiFi ",null,23, "Free Airport WIFI",null,41, "_Free Pub WiFi",null,25, "TWHRomsey",null,42, "GUINNESS STOREHOUSE FREE WIFI",null,17, "bitbuzz",null,21, "LIDO HALL",null,14, "StayPlanet",null,16, "The Winchester Hotel",null,7, "Parliament Hotel Lobby ",null,20, "LanServWS",null,2, "MSL Oilfield Services",null,8, "Complimentary Lord Nelson",null,30, "Boston Pizza",null,29, "Marco Polo Lounge",null,13, "Gogartys Bar WiFi",null,22, "OVALWIFI",null,19, "SKY9AA6B",null,5, "MSL_R&D",null,3, "HUAWEI-E5330-4A72",null,4, "briennet",null,27, "Halifax_Airport",null,34, "ARTHURS",null,18, "virginmedia2975679",null,0, "TG_Public",null,39, "Gahan House ",null,32, "ClearWELL Oilfield Solutions",null,38, "Marriott Gatwick",null,9, "Tailgate Free",null,40, "Dublin Airport Free WiFi",null,24, "Wyndham",null,35, "Rum-guest",null,31, "SKY27422",null,12, "WORLD.OF.TUI",null,10]

So I declare

B4X:
    Public ApsList As List
    Public Scan As MLScan
    ' record type for saved networks list
    Type ApsListEntry (SSID As String, BSSID As String, Index As String)

Then I get the current list by using

B4X:
Public Sub BuildApsList
    ApsList = Scan.listSavedNetworks
    Log("Access Points")
    Log(ApsList)
End Sub

Then I try to process it by extracting the SSIDs into a list and selecting one for deletion:

B4X:
Sub imgIcon_LongClick
    
    If WifiCommon.Wifi.isWifiEnabled Then
        ' log ap list showing loads of long forgotten aps!
        WifiCommon.BuildApsList
        If WifiCommon.ApsList.Size = 0 Then
            ToastMessageShow("No device found.", True)
        Else
            Dim i As Int
            Dim rec As ApsListEntry
            rec.Initialize
            Dim l As List
            l.Initialize
            Dim foundSSID As String
            
            For i = 0 To WifiCommon.ApsList.Size - 1
                rec = WifiCommon.ApsList.Get(i) ' <<<--- ERROR Line 901
                l.Add(rec.SSID)
            Next
            Dim res As Int
            res = InputList(l, "Choose Network to Delete", -1)
            If res <> DialogResponse.CANCEL Then
                foundSSID = l.Get(res)
                ToastMessageShow(foundSSID, True)
            End If
        End If ' no entries found
    Else
        ToastMessageShow("Enable Wifi to see saved networks list", True)
    End If

End Sub

but the line that tries to fetch records from ApsList and assign to rec fails thus:

B4X:
Process_Globals called
*** Service (starter) Create ***
** Service (starter) Start **
Globals called
** Activity (main) Create, isFirst = true **
Activity_Create completed
** Activity (main) Resume **
Activity_Resume called
Timer1_Tick
Access Points
(ArrayList) ["WIFI-ONDA",null,11, "goatgarden",null,33, "JoesGuests 2016",null,28, "Wyndham-Guest",null,37, "Wyndham-Lobby",null,36, "molo2",any,1, "molo1",null,15, "FVGWiFi",null,6, "LIDO 2B",null,26, "__Official TempleBar WiFi ",null,23, "Free Airport WIFI",null,41, "_Free Pub WiFi",null,25, "TWHRomsey",null,42, "GUINNESS STOREHOUSE FREE WIFI",null,17, "bitbuzz",null,21, "LIDO HALL",null,14, "StayPlanet",null,16, "The Winchester Hotel",null,7, "Parliament Hotel Lobby ",null,20, "LanServWS",null,2, "MSL Oilfield Services",null,8, "Complimentary Lord Nelson",null,30, "Boston Pizza",null,29, "Marco Polo Lounge",null,13, "Gogartys Bar WiFi",null,22, "OVALWIFI",null,19, "SKY9AA6B",null,5, "MSL_R&D",null,3, "HUAWEI-E5330-4A72",null,4, "briennet",null,27, "Halifax_Airport",null,34, "ARTHURS",null,18, "virginmedia2975679",null,0, "TG_Public",null,39, "Gahan House ",null,32, "ClearWELL Oilfield Solutions",null,38, "Marriott Gatwick",null,9, "Tailgate Free",null,40, "Dublin Airport Free WiFi",null,24, "Wyndham",null,35, "Rum-guest",null,31, "SKY27422",null,12, "WORLD.OF.TUI",null,10]
Error occurred on line: 901 (Main)
java.lang.ClassCastException: java.lang.String cannot be cast to com.msluk.net.siot_approach_data.wificommon$_apslistentry
    at com.msluk.net.siot_approach_data.main._imgicon_longclick(main.java:741)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.objects.ViewWrapper$2.onLongClick(ViewWrapper.java:90)
    at android.view.View.performLongClick(View.java:5285)
    at android.view.View$CheckForLongPress.run(View.java:21230)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6934)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
** Activity (main) Pause, UserClosed = true **

This is pretty much an identical technique that I've used with other CSV data from MLWifi.

Any ideas greatly appreciated.

Simon M
 

Emme Developer

Well-Known Member
Licensed User
Longtime User
You're trying to cast a string into your type. This is the error.
Try this
B4X:
Dim l As List = Utils.MlScan.listSavedNetworks
Dim rete() As String
For i = 0 To l.Size-1
 rete = Regex.Split(",",l.Get(i))
    Log(rete)
    rec.initialize
    rec.ssid = rete(0)
    rec.bssid = rete(1)
    rec.index = rete(2)
    l.add(rec)
Next
 
Upvote 0

SimonInCanada

Member
Licensed User
Thanks !

Not covering myself in glory today :-( You wouldn't think that I've been doing this for about 40 years starting with machine code lol. I got a bit confused by the way that the debug output had me convinced that it was some sort of record type rather than a bunch of mini CSV records. I was heading that way when you replied.

That's a neat way to split the string up :)

Simon M
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
Don't worry ;)
By the way, Always read the documentation first ;)


upload_2017-12-11_16-29-0.png
 
Upvote 0
Top