在iPhone上以编程方式打开蓝牙

在iPhone上以编程方式打开蓝牙

问题描述:

我只想知道是否可以在iPhone上以编程方式打开蓝牙?

I just want to know whether is it possible to switch on bluetooth programmatically on iPhone?

可以使用以下代码行打开/关闭蓝牙,但是由于它访问Apple的私有框架,因此您的App可能会在App中被拒绝商店推送

it is possible to switch on/off the Bluetooth by using the below lines of code , but since it access private frameworks of Apple, your App may reject in App store push

- (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