B4A Library ToggleWIFIData library

I needed a way to toggle WIFI-data programatically in a small app of mine but I couldn't find a way to do it in Basic4Android so I wrote this small library to toggle WIFI (disabled/enabled).

The java-code was easily found using Google. It was also a good exercise to get familiar with wrapping java-code and writing libraries for use with Basic4Android. I am sure the library can be written in a much better way but it does work (tested on Froyo and Gingerbread) and therefore I decided to share it with you.

Please note that the library does not check for connectivity-status. It only verifies if WIFI is enabled or disabled and gives you the possibility to turn it on or off.

I am also trying to extend this little library with the possibility to toggle 3G (mobile-data) and Bluetooth but so far I haven't been able to get the wrapper-code running. Maybe in the future if I am able to come to grip with java.

Library is attached and so is a small example-project.

Edit: 01/11/2011 - Updated library and got rid of ActivityObject annotation so it may be used within service-modules. I haven't tested it within a service-module so if it doesn't work, then let me know.

Edit2: 01/11/2011 - Forgot to change version-number. Now library shows correct version i.e. version 2.0
 

Attachments

  • ToggleWIFIData.zip
    2.2 KB · Views: 944
  • TestTogglingWIFI.zip
    6.3 KB · Views: 852
Last edited:

glouie

Member
Licensed User
Longtime User
Using ToggleWifiData library from service module?

Is there a reason this library only works from an activity module? I wanted to use it in a service module but right now I am using CallSub from within my service module to execute it because I cannot compile when referencing this object in the service module.

I also noticed this when trying to do the same using the TurnGPS library, is this an Android limitation or something set in the library?

Thanks.
 

moster67

Expert
Licensed User
Longtime User
Good question! I have not played with services yet so I really don't know.

My Java-knowledge isn't much so I can't say. Perhaps some of the brighter people here in the forum can help you.
For ease of reference, here is the source of the library. If anyone can enhance it so it may be used in a service-module (or improve it), please be my guest.
Please just post the library and make it available for others.

B4X:
package tillekesoft.b4a.ToggleWifiData;

import android.net.wifi.WifiManager;
import android.content.Context;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Version;

@ActivityObject
@ShortName("ToggleWifiData")
@Permissions(values={"android.permission.CHANGE_WIFI_STATE", "android.permission.ACCESS_WIFI_STATE","android.permission.UPDATE_DEVICE_STATS","android.permission.MODIFY_PHONE_STATE"})
@Author("Moster67")
@Version(1.0f)

public class ToggleWifiData  {
   
    private BA ba;

     public void Initialize(BA ba)
     {
       this.ba = ba;
     }
   
public boolean isWIFI_enabled()
{
   WifiManager wifiManager = (WifiManager) this.ba.context.getSystemService(Context.WIFI_SERVICE);
     
   if(wifiManager.isWifiEnabled()){
      return true;
   }else{   
      return false;
   }
}     
     
public boolean toggleWIFI()
{
   WifiManager wifiManager = (WifiManager) this.ba.context.getSystemService(Context.WIFI_SERVICE);
   
    if(wifiManager.isWifiEnabled()){
        if(wifiManager.setWifiEnabled(false))
           return true;
      }else{
        if(wifiManager.setWifiEnabled(true))
           return true;
    }
    return false;   
}

}
 
Last edited:

moster67

Expert
Licensed User
Longtime User
As suggested by Erel and requested by glouie, I have now updated the library (see 1st post) and got rid of the ActivityObject annotation so it may be used within service-modules.

I haven't tested it within a service-module so if it doesn't work, then let me know.

New version is Version 2.0
 
Last edited:

glouie

Member
Licensed User
Longtime User
Moster67,

Thanks for making this change so quickly, I just tested your new library and it works from within service modules now and is no longer restricted to just activity modules.

Now we can run background processes which can enable the Wifi, previously if the user were to close the running activity there was no way to enable wifi.

Erel, thanks for letting us know how to enable this for service modules. I am really enjoying what an excellent product B4A is and the support community is top-notch.

:sign0098:
 
Top