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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
creednmd
  • 2,001
  • 2
  • 18
  • 25
  • 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. – creednmd Dec 24 '10 at 02:00

1 Answers1

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.

creednmd
  • 2,001
  • 2
  • 18
  • 25
  • 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? – Raj Jul 07 '11 at 05:48
  • 1
    sorry i gt the solutions..by giving more delay its working fine for me..thank you creednmd – Raj Jul 07 '11 at 06:11
  • @creednmd do you know about the effects of setEnabled and setPowered properties? Which of these settings achieves what? – marcus Nov 07 '11 at 20:16
  • @marcus no idea I’m afraid. Will have to look into it when I get a chance. – creednmd Nov 17 '11 at 15:07
  • @dertoni. Glad it was of help; real thanks should go to mattfarrugia of course. – creednmd Nov 17 '11 at 15:08
  • I have one error - [btCont setEnabled:!currentState] ; apparently conflicts with other methods called setEnabled. How do you get around this? – mheavers Jan 03 '12 at 18:47
  • 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? – creednmd Jan 06 '12 at 20:27
  • 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. – Nate Jun 27 '12 at 00:23