Search code examples
iphoneobjective-ciosios4

Is there any way to switch on/off bluetooth in iPhone programmatically?


In my application I want to switch on/off iPhone Bluetooth.i m using sdk 4.3..I got some idea regarding Bluetooth manager framework, but it's not working in 4.3..any ideas? or can we programmatically determine whether Bluetooth is ON/OFF?


Solution

  • - (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:1.0f] ;
    #endif
        return YES ;
    }
    
    #if TARGET_IPHONE_SIMULATOR
    #else
    - (void)toggle:(id)btCont
    {
        BOOL currentState = [btCont enabled] ;
        [btCont setEnabled:!currentState] ;
        [btCont setPowered:!currentState] ;
    
    }
    #endif
    

    the above lines of code i got from here