B4A Library Android Wifi Hotspot Manager

It wraps this Github project. It prints the connected clients to the B4A log. We can panel beat it a bit if I know what info you would like to have returned to the B4A project. I have tested it between my tablet (client) and cell phone (hotspot) and it seems to work 100%.

Note the permissions added to the B4A manifest file
B4X:
AddPermission(android.permission.CHANGE_WIFI_STATE)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.INTERNET)

Sample Code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: HotSpot
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
 
    Dim t As Timer

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.
 
    Dim hs As HotSpot

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")
 
    t.Initialize("t", 2000)
 
    hs.Initialize("")

End Sub

Sub Activity_Resume
 
t.Enabled = True

End Sub

Sub Activity_Pause (UserClosed As Boolean)
 
    t.Enabled = False

End Sub

Sub t_tick
 
 
hs.scan
 
End Sub

You can download and test any posting of mine in this thread but if you want to use it then you need to
 

Attachments

  • b4aHotSpot.zip
    6.7 KB · Views: 802
  • HotSpotLibFiles.zip
    7 KB · Views: 829
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
New lib files and B4A demo attached. It brings back the client info to the B4A project. I have successfully tested it with my Samsung S4 mini as the HotSpot and my Samsung tablet and Blackberry cell phone as clients.

The 4 values returned per client are:
1. IP address
2. Device (wlan0)
3. HW Address
4. Reachable (boolean that will toggle between true and false if a connected device is disconnected from the HotSpot - and vice versa)

The new lib also adds the following two methods:
1. enableHotSpot()
2. disableHotSpot()

Sample code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: HotSpot
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Dim t As Timer

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.
   
    Dim hs As HotSpot

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")
   
    t.Initialize("t", 2000)
   
    hs.Initialize("hotspot")

End Sub

Sub Activity_Resume

hs.enableHotSpot
DoEvents   
t.Enabled = True

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
t.Enabled = False
hs.disableHotSpot

End Sub

Sub t_tick
   
hs.scan
   
End Sub

Sub hotspot_scan_result (client() As String)
   
    For i = 0 To client.Length - 1

        If client(i) <> Null Then
          Log(client(i))
            If (i + 1) mod 4 = 0 Then
            Log(" ")   
          End If                    
        End If
       
    Next

End Sub
 

Attachments

  • b4aHotSpot.zip
    6.8 KB · Views: 536
  • HotSpotLibFiles.zip
    7.1 KB · Views: 538

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi,

I tried on my Samsung S5 as a hotspot

I connected an old Lenovo and the cheap ESP8266

And it works !

However I can't figure out what is doing :
1. enableHotSpot()
2. disableHotSpot()

It doesn(t start nor stop my hotspot on my S5

Thank you again for this great lib
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi,

I tried on my Samsung S5 as a hotspot

I connected an old Lenovo and the cheap ESP8266

And it works !

However I can't figure out what is doing :
1. enableHotSpot()
2. disableHotSpot()

It doesn(t start nor stop my hotspot on my S5

Thank you again for this great lib
Hi JP

This is the sequence:

Before you start the app - the mobile hotspot is off:
1.png


Starting the app - the app turns on the hotspot (now green):
2.png


First device connected to the hotspot:

FirstDeviceConnected.png


Second device connected:

SecondDeviceConnected.png



Second device disconnected:

SecondDeviceDisconnected.png


First device disconnected:

FirstDeviceDisconnected.png


Quitting the app - the hotspot is turned off by hs.disableHotSpot (green gone)

QuittingTheHSManager.png



When you disconnect a device from the hotspot the false value tell you that the device is now disconnected from the hotspot. But it keeps showing you what devices are or were connected from the time that the hotspot became active and devices connected to it....
 

Johan Schoeman

Expert
Licensed User
Longtime User
can you improve library to config AP name/password,...??
Here we go - you can set the AP name and password from B4A code. Attached new lib files and B4A project. Have tested it and it seems to be working fine.

Setting the AP name and password:
B4X:
hs.AccessPointNameAndPassword("My_B4A_HotSpot", "b4a_b4i_b4j")   'first the AP name, then the password

This is what it looks like in the "Configure Mobile hotspot" UI of my S4 mini after the B4A code has been executed:

HotSpotSetup.png



Sample Code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: HotSpot
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Dim t As Timer

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.

    Dim hs As HotSpot

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")

    t.Initialize("t", 2000)

    hs.Initialize("hotspot")

    'ADDED 6 JAN 2016 so that AP NAME AND PASSWORD CAN BE SET PROGRAMATICALLY
    hs.AccessPointNameAndPassword("My_B4A_HotSpot", "b4a_b4i_b4j")   'first the AP name, then the password

End Sub

Sub Activity_Resume

hs.enableHotSpot
DoEvents
t.Enabled = True

End Sub

Sub Activity_Pause (UserClosed As Boolean)

t.Enabled = False
hs.disableHotSpot

End Sub

Sub t_tick

hs.scan

End Sub

Sub hotspot_scan_result (client() As String)

    For i = 0 To client.Length - 1

        If client(i) <> Null Then
          Log(client(i))
            If (i + 1) mod 4 = 0 Then
            Log(" ")
          End If                 
        End If
    
    Next

End Sub
 

Attachments

  • b4aHotSpot.zip
    6.9 KB · Views: 524
  • HotSpotLibFilesNew.zip
    7.9 KB · Views: 538
Last edited:

elitevenkat

Active Member
Licensed User
Longtime User
is it possible to disconnect a connected user from the wifi network either by ip or macid dynamically using the wifi hotspot manager ?
 

welu1805

Active Member
Licensed User
Longtime User
Hi Johan,

very interesting lib for me. Because it takes several seconds from enableHospot until it is really enabled, can I get the Hotspot state to retrieve it in a Timer1_Tick?

Is your lib free for commercial usw?

Greetings from Germany
Lutz
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan,

very interesting lib for me. Because it takes several seconds from enableHospot until it is really enabled, can I get the Hotspot state to retrieve it in a Timer1_Tick?

Is your lib free for commercial usw?

Greetings from Germany
Lutz
Will see if I can amend the code to bring back the Hotspot state. Feel free to use the lib which ever way you want
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan,

very interesting lib for me. Because it takes several seconds from enableHospot until it is really enabled, can I get the Hotspot state to retrieve it in a Timer1_Tick?

Is your lib free for commercial usw?

Greetings from Germany
Lutz
Try with the attached library files:
B4X:
Sub t_tick
  
hs.scan
Log("Is Wifi AP Enabled = " & hs.isWifiApEnabled)
  
End Sub
 

Attachments

  • HotSpotLibFiles.zip
    8 KB · Views: 461

Croïd

Active Member
Licensed User
Longtime User
Hello Johan

can you tell me, what I'm missing? I use your template
(66 = Sub hotspot_scan_result (client() As String) )

Wifi is not enabled, after scan !

Thanks

B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Est - Wifi AP Enabled =false
b4a.example.main:_hotspot_scan_result, [[]]
Error occurred on line: 66 (Main)
java.lang.IllegalArgumentException: method b4a.example.main._hotspot_scan_result argument 1 has type java.lang.String[], got java.util.ArrayList
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at hotspotwrapper.hotSpotWrapper$1.onFinishScan(hotSpotWrapper.java:74)
    at main.java.WifiApManager$1$1.run(WifiApManager.java:168)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
** Activity (main) Pause, UserClosed = true **
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hello Johan

can you tell me, what I'm missing? I use your template
(66 = Sub hotspot_scan_result (client() As String) )

Wifi is not enabled, after scan !

Thanks

B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Est - Wifi AP Enabled =false
b4a.example.main:_hotspot_scan_result, [[]]
Error occurred on line: 66 (Main)
java.lang.IllegalArgumentException: method b4a.example.main._hotspot_scan_result argument 1 has type java.lang.String[], got java.util.ArrayList
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at hotspotwrapper.hotSpotWrapper$1.onFinishScan(hotSpotWrapper.java:74)
    at main.java.WifiApManager$1$1.run(WifiApManager.java:168)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
** Activity (main) Pause, UserClosed = true **
Try with the attached B4A project and B4A library files. I changed the String[] array to an ArrayList at some stage and not sure that you are using the right combination of the B4A project and the B4A library files. This is the sample code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: HotSpot
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Dim t As Timer

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.
   
    Dim hs As HotSpot

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")
   
    t.Initialize("t", 2000)
   
  
    hs.Initialize("hotspot")
   
    'ADDED 6 JAN 2016 so that AP NAME AND PASSWORD CAN BE SET PROGRAMATICALLY
    hs.AccessPointNameAndPassword("My_B4A_HotSpot", "b4a_b4i_b4j")   'first the AP name, then the password

End Sub

Sub Activity_Resume

hs.enableHotSpot
DoEvents   
t.Enabled = True

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
t.Enabled = False
hs.disableHotSpot

End Sub

Sub t_tick
   
hs.scan
Log("Is Wifi AP enabled = " & hs.isWifiApEnabled)
   
End Sub

Sub hotspot_scan_result (mylist As Object)
   
    Dim ml As List
    ml= mylist
   
    For i = 0 To ml.Size - 1
        If (i+1) mod 4 = 1 Then Log("IpAddr = " & ml.Get(i))
        If (i+1) mod 4 = 2 Then Log("Device = " & ml.Get(i))
        If (i+1) mod 4 = 3 Then Log("HWAddr = " & ml.Get(i))       
        If (i+1) mod 4 = 0 Then Log("Reachable = " & ml.Get(i))       
        If (i+1) mod 4 = 0 Then Log(" ")               
    Next

End Sub
 

Attachments

  • b4aHotSpot.zip
    7 KB · Views: 518
  • HotSpotNewLibFiles.zip
    8 KB · Views: 562

susu

Well-Known Member
Licensed User
Longtime User
Hi @Johan Schoeman. Thank you for your super library.
But how to setup an open hotspot? I tried hs.AccessPointNameAndPassword("Hotspot Name", "") but it's a locked hotspot, not open.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi @Johan Schoeman. Thank you for your super library.
But how to setup an open hotspot? I tried hs.AccessPointNameAndPassword("Hotspot Name", "") but it's a locked hotspot, not open.
What happens when you comment out that line? I seem to recall it would then create an open hotspot. If not, I will have to go back to the library...
 

susu

Well-Known Member
Licensed User
Longtime User
If I comment out that line, the hotspot is not enable (Is Wifi AP enabled = false).
 
Top