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

前端 未结 1 2036
花落未央
花落未央 2020-11-29 01:12

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 functio

相关标签:
1条回答
  • 2020-11-29 01:22

    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.

    0 讨论(0)
提交回复
热议问题