Is there a way to toggle bluetooth and/or wifi on and off programmatically in iOS?
Thanks to Matt Farrugia (@mattfarrugia on Twitter) the answer I was looking for was:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
#if TARGET_IPHONE_SIMULATOR
exit( EXIT_SUCCESS ) ;
#else
/* this works in iOS 4.2.3 */
Class BluetoothManager = objc_getClass( "BluetoothManager" ) ;
id btCont = [BluetoothManager sharedInstance] ;
[self performSelector:@selector(toggle:) withObject:btCont afterDelay:0.1f] ;
#endif
return YES ;
}
#if TARGET_IPHONE_SIMULATOR
#else
- (void)toggle:(id)btCont
{
BOOL currentState = [btCont enabled] ;
[btCont setEnabled:!currentState] ;
[btCont setPowered:!currentState] ;
exit( EXIT_SUCCESS ) ;
}
#endif
You need to link against the Gamekit framework as well, but simply add in this code to a new Xcode project and run on the device. Doing so creates a 1-tap App that toggles Bluetooth on and off.
creednmd
Updated on April 27, 2020Comments
-
creednmd about 4 years
I am looking for an easy way to toggle both bluetooth and wifi between on and off states on iOS 4.x devices (iPhone and iPad).
I am constantly toggling these functions as I move between different locations and usage scenarios, and right now it takes multiple taps and visits to the Settings App. I am looking to create a simple App, that lives on Springboard, that I can just tap and it will turn off the wifi if it's on, and vice versa, then immediately quit. Similarly with an App for toggling bluetooth’s state.
I have the developer SDK, and am comfortable in Xcode and with iOS development, so am happy to write the required code to create the App. I am just at a loss as to which API, private or not, has the required functionality to simply toggle the state of these facilities.
Because this is scratching a very personal itch, I have no intent to try and sell the App or get it up on the App store, so conforming with App guidelines on API usage is a non-issue. What I don’t want to do is jailbreak the devices, as I want to keep the core software as shipped.
Can anyone point me at some sample code or more info on achieving this goal, as my Google-fu is letting me down, and if the information is out there for 4.x devices I just can’t find it.
-
Raj almost 13 yearsI tried the above method in my own App, but its not working, I am using Xcode3.2.5 and iPod 4, iOS 4.3.2,,,can you please help me?
-
Raj almost 13 yearssorry i gt the solutions..by giving more delay its working fine for me..thank you creednmd
-
marcus over 12 years@creednmd do you know about the effects of setEnabled and setPowered properties? Which of these settings achieves what?
-
creednmd over 12 years@marcus no idea I’m afraid. Will have to look into it when I get a chance.
-
creednmd over 12 years@dertoni. Glad it was of help; real thanks should go to mattfarrugia of course.
-
mheavers over 12 yearsI have one error - [btCont setEnabled:!currentState] ; apparently conflicts with other methods called setEnabled. How do you get around this?
-
creednmd over 12 yearsI didn’t; worked for me fine. Have you got a method with that name in your own code anywhere? May be a namespace clash?
-
Nate almost 12 yearsActually, you don't need to link against the GameKit framework. The
BluetoothManager
class is fromPrivateFrameworks/BluetoothManager.framework
. So, you can either link against that, or dynamically open it withdlopen()
. Yes, I have built this, and am 100% sure.