B4A Library Android Wifi Hotspot Manager

Discussion in 'Additional libraries, classes and official updates' started by Johan Schoeman, Dec 11, 2015.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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
    Code:
    AddPermission(android.permission.CHANGE_WIFI_STATE)
    AddPermission(android.permission.ACCESS_WIFI_STATE)
    AddPermission(android.permission.INTERNET)
    Sample Code:

    Code:
    #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 [​IMG]
     

    Attached Files:

    Last edited: May 2, 2016
  2. MarcoRome

    MarcoRome Expert Licensed User

    Great Johan ;) +10
     
    Johan Schoeman likes this.
  3. Johan Schoeman

    Johan Schoeman Expert Licensed 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:

    Code:
    #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 + 1mod 4 = 0 Then
                
    Log(" ")   
              
    End If                    
            
    End If
           
        
    Next

    End Sub
     

    Attached Files:

  4. iCAB

    iCAB Well-Known Member Licensed User

    I tried it on 3 different devices and it seems to be working nicely.
    Thanks Johan, works great.
     
    Johan Schoeman likes this.
  5. tuhatinhvn

    tuhatinhvn Active Member Licensed User

    can you improve library to config AP name/password,...??
     
  6. freedom2000

    freedom2000 Well-Known Member Licensed 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
     
  7. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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....
     
    freedom2000 and Erel like this.
  8. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Will add some configuration to it for at least AP name and password.
     
  9. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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:
    Code:
    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:

    Code:
    #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 + 1mod 4 = 0 Then
                
    Log(" ")
              
    End If                 
            
    End If
        
        
    Next

    End Sub
     

    Attached Files:

    Last edited: Jan 7, 2016
    MarcoRome and freedom2000 like this.
  10. elitevenkat

    elitevenkat Active Member Licensed User

    is it possible to disconnect a connected user from the wifi network either by ip or macid dynamically using the wifi hotspot manager ?
     
  11. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Unfortunately not.
     
  12. welu1805

    welu1805 Active Member Licensed 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
     
  13. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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
     
    inakigarm, welu1805 and DonManfred like this.
  14. Johan Schoeman

    Johan Schoeman Expert Licensed User

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

    Attached Files:

    welu1805 and MarcoRome like this.
  15. welu1805

    welu1805 Active Member Licensed User

    Hi Johan,

    I tried it now and it works good.

    Thank you
     
    Johan Schoeman likes this.
  16. Croïd

    Croïd Active Member Licensed 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

    Code:
    ** 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 **
     
  17. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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:

    Code:
    #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+1mod 4 = 1 Then Log("IpAddr = " & ml.Get(i))
            
    If (i+1mod 4 = 2 Then Log("Device = " & ml.Get(i))
            
    If (i+1mod 4 = 3 Then Log("HWAddr = " & ml.Get(i))       
            
    If (i+1mod 4 = 0 Then Log("Reachable = " & ml.Get(i))       
            
    If (i+1mod 4 = 0 Then Log(" ")               
        
    Next

    End Sub
     

    Attached Files:

    Humberto and Croïd like this.
  18. susu

    susu Well-Known Member Licensed 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.
     
  19. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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...
     
  20. susu

    susu Well-Known Member Licensed User

    If I comment out that line, the hotspot is not enable (Is Wifi AP enabled = false).
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice