Is there a way to toggle bluetooth and/or wifi on and off programmatically in iOS?
Asked Answered
I

1

20

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.

Interfluent answered 23/12, 2010 at 11:39 Comment(1)
I pay for a developer account, so surely if the certificate expires I just redownload it and reinstall the App from Xcode? Just like I’d install any other work-in-progress App. This is for an internal, personal App to solve my own, one-off issue. Private APIs are fine, as the only devices it will see are mine.Interfluent
I
25

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.

Interfluent answered 18/4, 2011 at 21:11 Comment(8)
I 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?Acrosstheboard
sorry i gt the solutions..by giving more delay its working fine for me..thank you creednmdAcrosstheboard
@Interfluent do you know about the effects of setEnabled and setPowered properties? Which of these settings achieves what?Flied
@Flied no idea I’m afraid. Will have to look into it when I get a chance.Interfluent
@dertoni. Glad it was of help; real thanks should go to mattfarrugia of course.Interfluent
I have one error - [btCont setEnabled:!currentState] ; apparently conflicts with other methods called setEnabled. How do you get around this?Smilax
I didn’t; worked for me fine. Have you got a method with that name in your own code anywhere? May be a namespace clash?Interfluent
Actually, you don't need to link against the GameKit framework. The BluetoothManager class is from PrivateFrameworks/BluetoothManager.framework. So, you can either link against that, or dynamically open it with dlopen(). Yes, I have built this, and am 100% sure.Silvio

© 2022 - 2024 — McMap. All rights reserved.