B4A Library Simple WIFI library

HotShoe

Well-Known Member
Licensed User
Longtime User
Oh, I see what you mean, yes that can be done easily in MLScan. I'll add it in the next update. BTW, you can check the MLScan.wifis array at any time, but it will not update without explicitly doing a new scan right now.

--- Jem
 

HotShoe

Well-Known Member
Licensed User
Longtime User
I am getting a NullPointerException when using connectToAP

Thanks John, I have updated the library and have also fixed the connectToAP method again. I missed a set of required quote marks on a wifimanager call.

A new version 2.11 is now available (as soon as I finish this reply), in the first post. Only MLscan is changed as follows.

This version no longer needs a timer or any other means of ensuring that the wifi scan list is ready. It now fires an event when the list is ready. The event looks like this assuming your scan event is called Scan :

Sub Scan_ScanDone(Results() As String, Count As Int)

Results is the string array as before, and Count is the number of results in the array. The wifis array is still available as well and is updated automatically.

The StartScan method has changed and now requires an event name to be passed. It also checks to see that wifi is enabled and enables it if it is not. Through a bit of magic, I was able to squeeze another 70% speed increase in the scan results time, so the speed is finally where I am comfortable with the library as a whole. It felt a bit clunky before, but my custom event was not working until today. Scans are virtually instant now.

Also, the getWifiList is now gone (I'm sorry, I promise I will stop breaking your code), and is replaced with updateWifiList. WifiList is no longer needed in this version since updates are done in the new event. A call to updateWifiList updates the scan results each time it is called, so you can set up a timer if you wish to do live updates of a listview for example as new AP's appear. You only need to call StartScan("eventname") the first time a scan is run, the same as you would use Initialize for any other library class. I was tempted to rename it to Initialize, but I can already here the pitchfoks rattling for all of the changes up to now.

If you see any more problems, let me know as always, and thanks for putting up with the changes.

--- Jem
 

praktikant

Member
Licensed User
Longtime User
hallo,thank you @@HotShoe , for this Lib
i have now a question.

when a wifi ist connected,
i scan wifi netzwerk again
I noticed that, signal strength of this connected wifi dont changes with different distances
how can i test this signal strength of the connected wifi with different distances???
Can you give me an example of that?

B4X:
Sub wifi_Tick
scan.startScan("Scan")
scan.updateWifiList
End Sub
Sub Scan_ScanDone(Results() As String, Count As Int)
    Dim tmp As String
   
    For i = 0 To Count - 1
    tmp = Results(i).Replace(",","    ")
    Log(CRLF&"temp:"&tmp)
    scanlist.AddSingleLine(tmp)
    Next
   
    ProgressDialogHide
   
End Sub

 
Last edited:

HotShoe

Well-Known Member
Licensed User
Longtime User
You can set a timer to repeat the update every minute or other length of time.

B4X:
'Dim a timer in process_globals named scantime and initialize it in activity_create
   scantime.Initialize("scantime", 60000)

'enable the timer after the initial scan is done
   scantime.Enabled = True

'create the tick event for the timer to update the scan list
Sub scantime_Tick
     scantime.Enable = False
     scan.updateWifiList

     scantime.Enable = True

End Sub

Some variation of this should work.

--- Jem
 

praktikant

Member
Licensed User
Longtime User


B4X:
Sub Service_Create
Log("start")
scan.startScan("Scan")
WiFiScanTimer.Initialize("WiFiScanTimer",5000)
WiFiScanTimer.Enabled=True
End Sub

Sub WiFiScanTimer_Tick
WiFiScanTimer.Enable = False
     scan.updateWifiList
   WiFiScanTimer.Enable = True
End sub

then
Error compiling programm.
error description:Unknown member:enable
WiFiScanTimer.Enable=false
Word:enable

i dont know, what happend
Code dont work.
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Try the new version attached to the first post. Also look at the new wifitest example to se exactly how to do what you need.

--- Jem
 

XorAndOr

Active Member
Licensed User
Longtime User
Hi, I'm using your library, very useful. I have a question, when I go to connect with an access point in the moment that I enter the password I get the
B4X:
MsgBox("Could not connect to" & ssid, "Information")
, but if I do it from the list of devices your phone is everything ok, where do you think I'm wrong? I also added
B4X:
AddPermission (android.permission.WRITE_SETTINGS)
but I get always the msgbox, thanks
 

HotShoe

Well-Known Member
Licensed User
Longtime User
hmmm, I don't know. Look through the wifitest project and make sure you are doing the connection the same way.

--- Jem
 

XorAndOr

Active Member
Licensed User
Longtime User
hmmm, I don't know. Look through the wifitest project and make sure you are doing the connection the same way.

--- Jem
hello, thank you for responding, I am using the example attached in the first post. I noticed that in the list of access points do not see the message "Saved"
B4X:
If tmp.Contains("Saved") Then
   ok = scan.connectToAP(cursel)
I think that's why it does not work, but if I do it with paired device and insert the password from the phone menu, then returning to the example works ok. thanks
 

jjcc

Member
Licensed User
Longtime User
@HotShoe Great lib!
However, I tried the example project. connectToAP doesn't work with a few phones I tried. I tried to forget a connection within the android setting, then tried the sample code to let the the app saves the password and connect to phone. the result the of connectToAP is OK but it's not really connect to the AP. (the IP shows 0.0.0.0 and call isWifeConnected return false after init)

Did you have the same issue?
 

HotShoe

Well-Known Member
Licensed User
Longtime User
I have not seen this, but I will look at it again to see if there is something I missed.

Thanks,

--- Jem
 

camolas

Member
Licensed User
Longtime User
Hi,

Can some one please present a example using "forgetAP"? and how can i get the "NetId" from my saved wifis list?

Thanks in advance

cam
 

jjcc

Member
Licensed User
Longtime User
@HotShoe I guess it might be caused by quote string of SSID. There are different versions of SDK, some use quoted string, the others don't.

Here's the link about the problem

Do you have time to fix the lib? or if you have already uploaded the code somewhere in github?
 

HotShoe

Well-Known Member
Licensed User
Longtime User
Sorry I've been away so long.

Can some one please present a example using "forgetAP"? and how can i get the "NetId" from my saved wifis list?

Use the listSavedNetworks method. It returns a list of saved networks on your device. the 3rd item in each listing is the network ID. Each element is separated by a comma. Then you can call the forgetAP method based on the SSID and network ID that you want to erase from your device. I will update the wifitest project later and include an example in there.

Do you have time to fix the lib? or if you have already uploaded the code somewhere in github?

I have updated mlwifi to handle both quoted and unquoted SSID strings, so try version 2.13 attached in the 1st post of this thread.

--- Jem
 

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi.
Thanks for useful library.
If user connect to internet via gsm,your library can detect it?
 

HotShoe

Well-Known Member
Licensed User
Longtime User
If user connect to internet via gsm,your library can detect it?

It can, but that is the only non-wifi related thing that it can do. The rest of the methods are specifically for wifi.

--- Jem
 

Mark Hollingworth

Member
Licensed User
Longtime User
Great Library has been working fine but I seem to have hit a problem and I am not sure whats causing it.

I have a service which listens for network connection events and then runs code depending on the connection state, it works fine when a wifi connection is found but when the wifi is disconnected I am getting the following error:

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 } in wifi.MLwifi$MLWifiScanner$WifiScanReceiver@274d9542
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:982)
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:6837)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at wifi.MLwifi$MLWifiScanner.checkSSID(MLwifi.java:346)
at wifi.MLwifi$MLWifiScanner.access$0(MLwifi.java:339)
at wifi.MLwifi$MLWifiScanner$WifiScanReceiver.onReceive(MLwifi.java:324)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:972)

I have tried various things to resolve it but have been unable to. I am attaching a sample project that is causing the same error and it always happens after calling .StartScan, if I remove that line of code from the service then it doesn't crash.

Also the .StartScan seems to be continually returning results, is that intended or should it perform one scan then stop?

Your help is appreciated
 

Attachments

  • sample.zip
    7 KB · Views: 316
Cookies are required to use this site. You must accept them to continue using the site. Learn more…