B4A Library ToggleLibrary

Hey everyone,

This is an improvement of my TurnGPS Library.
ToggleLibrary is great to use in homescreen widgets or just normal apps.
With ToggleLibrary, you can currently do the following:

- Bluetooth: Toggle, Enable, Disable, Check.
- Airplane Mode: Toggle, Enable, Disable, Check.
- WiFi: Toggle, Enable, Disable, Check.
- GPS: Toggle, Enable, Disable, Check.
- DataConnection** (GPRS,...): Toggle, Enable, Disable, Check.
- RingerMode: Toggle, Enable, Disable. (Vibrate, Silent or Normal)
- Change Brightness
- Change Mediavolume.
- Reboot, GotoSleep, UserActivity, isScreenOn

There might still be a few bugs. Please let me know.

* With check, I mean that you can check if it's enabled(true) or disabled (false)
** Depends on device

Attached are the library files.
No sample is added because it is all self-explanatory.

Code: Copy
Dim TGL As Toggle
TGL.Initialize()
TGL.TurnBluetoothOn



Tomas
 

Attachments

Last edited:
To root a device is something the owner of the device can do with help of particular scripts although the warrancy will probably be lost. So of course it is (probably) something you cannot do from an app - at least an app which will work on devices of different brands.

So in a few words, rebooting a device is something you will probably not be able to do since most users do not have rooted devices.
 
Last edited:
Sorry my eng


I saw that and missing
AddPermission (android.permission.CHANGE_NETWORK_STATE)
for it to work well TurnDataConnectionOn!

Dim Toggle As TGL
TGL.Initialize ()
TGL.TurnDataConnectionOn

add to manifest editor:
AddPermission (android.permission.CHANGE_NETWORK_STATE)
 
DataConnection doesn't work on my Defy motorola mobile

Hi,

I have test this Toggle Library on my Motorola Defy with android 2.2.2 but the DataConnection doesn't work.

I have put the permission AddPermission (android.permission.CHANGE_NETWORK_STATE) on the manifest editor, but don't change the state of the network data by code (on my device).
I have tested the 'Wifi On' and 'Wifi Off' and it work fine,then:
¿Someone has tested this toggle library with Motorola Defy with the DataConnection On and Off?

Thanks in advance to all the community

Pablo
 
Toggle Library

Anybody with the same problem?

My GPS not turn OFF and not turn ON with the commands....

Dim TGL As Toggle
TGL.Initialize()
TGL.TurnGPSOff

Not show any errors, but not work!!

I try the previous version TURNGPS but not work too.

anyone can help me?

I use the Android in Samsung Tablet Galaxy with 2.3.

Thank You
 
Hello!
I have the same problem with GPS.
I use the Android ver. 3.2 on tablet Samsung Galaxy 8.9 LTE.
Thanks for help
Regards...

Rafalski76
 
A little demo zip file would help. Maybe a little layout with buttons?

Also, this is for Bluetooth, but most printers nowadays use wireless IP though a home network. Is it possible to print to an ip address?
 
Last edited:
Anybody with the same problem?

My GPS not turn OFF and not turn ON with the commands....

Dim TGL As Toggle
TGL.Initialize()
TGL.TurnGPSOff

Not show any errors, but not work!!

I try the previous version TURNGPS but not work too.

anyone can help me?

I use the Android in Samsung Tablet Galaxy with 2.3.

Thank You

I have the same problem with TurnGPSOn, no errors but not works! I have Samsung GalaxyTab 2 with Android 4.0.3.
There is a solution?
 
HI ALL I HAVE THIS PROBLEM WITH
TOGGLE.GOTOSLEEP

CAN SOMEBODY HELP ME

java.lang.SecurityException: Neither user 10131 nor current process has android.permission.DEVICE_POWER.
at android.os.Parcel.readException(Parcel.java:1322)
at android.os.Parcel.readException(Parcel.java:1276)
at android.os.IPowerManager$Stub$Proxy.goToSleep(IPowerManager.java:387)
at android.os.PowerManager.goToSleep(PowerManager.java:434)
at com.rootsoft.togglelibrary.ToggleLibrary.goToSleep(ToggleLibrary.java:503)
at b4a.example.main._imageview1_click(main.java:1290)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:155)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:59)
at android.view.View.performClick(View.java:2586)
at android.view.View$PerformClick.run(View.java:9262)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3744)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
at dalvik.system.NativeStart.main(Native Method)
java.lang.SecurityException: Neither user 10131 nor current process has android.permission.DEVICE_POWER.
 
android.permission.DEVICE_POWER is a protected permission. You cannot use it. Only system apps can use it (on unrooted devices).

Hi erel thx for ur answer, but how can i put the phone on sleep or simulate the lock button
 
In a simple teste with Toggle Library I have this error in a Bluetooth Status comparsion



If objTGL.Bluetooth = True Then strStatus = strStatus & "Bluetooth:ON,"
java.lang.NullPointerException
at com.rootsoft.togglelibrary.ToggleLibrary.getBluetooth(ToggleLibrary.java:199)
at com.visualnet.vnservice.servicevn._si_messagereceived(servicevn.java:1014)
at com.visualnet.vnservice.servicevn._su_testesms(servicevn.java:1721)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.keywords.Common$4.run(Common.java:930)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
 
Does not give any answer ... Android 4.1.2 Galaxy notes, you have found a solution, I have to add something in the manifest.
Code: Copy
Dim t As Toggle
t.Initialize()
t.TurnGPSOn

thank.
 
IMPORTANT WARNING:

Many devices cannot switch WiFi ON immediately, or at all, if .... Bluetooth is already ON !
And if to firstly switch BT off - WiFi is ON immediately.
Next BT also can be switched on OK.

Checked on 4 devices, with Android 2.3 and 4.0.x
 
I tested on Samsung Galaxy WIN with android 4.1.2
- Turn Bluetooth : Works well
- Turn GPS: Not Work
- Turn DataConnection: Not Work

Appreciate for any help.

Jonh,
 
I, too need to be able to turn on and off the GPS. I found the following code at Stackoverflow

http://stackoverflow.com/questions/...gps-programmatically-in-android-4-0-and-above

that folks claim works in 4.0.

Now if someone can turn it in to something useable in B4A...

Code: Copy
public void turnGPSOn()
{
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", true);
    this.ctx.sendBroadcast(intent);

    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(!provider.contains("gps")){ //if gps is disabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3"));
        this.ctx.sendBroadcast(poke);


    }
}
// automatic turn off the gps
public void turnGPSOff()
{
    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(provider.contains("gps")){ //if gps is enabled
        final Intent poke = new Intent();
        poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
        poke.setData(Uri.parse("3"));
        this.ctx.sendBroadcast(poke);
    }
}
 
Back
Top