Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 501
creednmd Avatar asked Dec 23 '10 11:12

creednmd


People also ask

How do I permanently turn off wifi and Bluetooth on Iphone?

If you want to completely disable Wi-Fi and Bluetooth for all networks and devices, follow these steps: To turn off Wi-Fi, go to Settings > Wi-Fi and turn off Wi-Fi. To turn off Bluetooth, go to Settings > Bluetooth and turn off Bluetooth.


1 Answers

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.

like image 86
creednmd Avatar answered Oct 01 '22 07:10

creednmd