18

I have seen a lot of questions about this but no one actually gives a real answer (frameworks to import, actual code etc). They only say with a private api and that will get your app rejected from the app store.

I am aware that use of a private api will get my app rejected by I was wondering how to do it for personal use. (iPhone SDK 3.1.2, iPod touch 2g)

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
johnathon
  • 181
  • 1
  • 1
  • 3

6 Answers6

10

I've been looking into this as well. You need to include the bluetoothmanager framework and header file in your project. It should be in

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/System/Library/PrivateFrameworks/BluetoothManager.framework/

If the header file is not there, you'll need to grab a .h file that was generated from the library and include it in your project. I googled to find it; Here is one here:

http://iphone-dev.googlecode.com/svn/branches/include-1.2-sdk/include/BluetoothManager/

Once that is added to your project, your import should look like this if the header file was already in the framework:

#import <BluetoothManager/BluetoothManager.h>

Or this if you added your own BluetoothManager.h file to your project:

#import "BluetoothManager.h

To toggle the bluetooth here is the code:

BluetoothManager *manager = [BluetoothManager sharedInstance];
[manager setEnabled:![manager enabled]];    

I have built a utility to do this myself and it does work. Note, if all you want to do is create a utility to toggle the bluetooth and exit, without any UI, create a new project in XCode and use the Window-based Application template. Add the code to the didFinishLaunchingWithOptions method and replace [window makeKeyAndVisible] with exit(0).

memmons
  • 40,222
  • 21
  • 149
  • 183
  • 1
    Just an update. This solution worked great for OS 3.x, but stopped working at iOS 4.0. – Nate Mar 15 '11 at 07:49
  • I find the trick and it works in ios 4.3.5 download the headers file from the site indicatesd, then not put in your project but just copy in PrivateFramework! you need to create "Headers" folder under /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Library/PrivateFrameworks/BluetoothManager.framework/ then copy all the 4 files downloaded then, import BluetooothManager framework in you project (you need to select using "add other", then the above code works! – Achille Oct 12 '11 at 07:14
  • @Achille can you tell me what what your import statement says exactly? – mheavers Jan 03 '12 at 19:44
  • @mheavers just #import then I'm able to call Class BluetoothManager = objc_getClass( "BluetoothManager" ) ; id btCont = [BluetoothManager sharedInstance] ; – Achille Jan 20 '12 at 15:58
7

You need to make sure that binaries and header files are BOTH in the PrivateFrameworks folders under:

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library/PrivateFrameworks

This will allow you to import PrivateFrameworks such as BluetoothManager.framework into your app and not get errors. You can find how to get the headers online. This works for 3.1.2 +cause Im writing an app right now that works perfectly on my device as well as Sim.

If your gonna test in the simulator, use the following:

#if TARGET_IPHONE_SIMULATOR
        //This is where simulator code goes that use private frameworks
#else
        /* this works in iOS 4.2.1 */
        Class BluetoothManager = objc_getClass("BluetoothManager");
        id btCont = [BluetoothManager sharedInstance];
        [btCont setPowered:YES];
#endif
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • I tried this, and though I'm properly loading the BluetoothManager class and getting a shared instance of it...it just doesn't do anything. Calling enabled always returns NO, even when Bluetooth is enabled. Calling setEnabled never changes its state. Absolutely every other BluetoothManager call I've tried always returns NO, nil, 0-element arrays, etc. In short, the BluetoothManager that gets returned seems totally neutered. (I'm running off my 4.2.1 iPhone, not the simulator.) How are people getting this to work? Thanks. – Greg Maletic Feb 14 '11 at 19:23
  • 1
    The above code most certainly does not work on iOS 4.2.1. This worked on 3.x, and WrightsCS apparently just updated his comment by replacing 3.x with 4.2. It builds, but does not function on a real device. – Nate Mar 15 '11 at 07:47
5
- (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 method will work in iOS 4.2.3

Raj
  • 5,895
  • 4
  • 27
  • 48
3

To get the BluetoothManager private api working you need to do the following: 1. get the 4 header files indicated by Harkonian and adding them to your SDK files (adding the header files to your project) 2. add the Framework to your project (adding the binary files to your project) 3. create a variable, for working with the BluetoothManager service Example: btManager = [BluetoothManager sharedInstance]; 4. use the BluetoothManager's methods, you can see them all in BluetoothManager.h

I put together a complete sample that is available here: http://www.pocketmagic.net/?p=2827

Hope this helps, Radu

radhoo
  • 2,877
  • 26
  • 27
  • This was developed for OS 5.1 , and tested on actual hardware, an iPod. Just adding this info since many users complained about having issues implementing this on newer OS versions. It still works fine! – radhoo Jul 16 '12 at 20:50
  • Works fine with `XCode 5.1.1` and `iOS 7.1`. – Jonathan F. Jul 29 '14 at 14:54
0

I believe the solution is to make a system call to launchctl since that's the daemon responsible for starting/stopping system services.

Epsilon Prime
  • 4,576
  • 5
  • 31
  • 34
0

With Iphone 5s
[btCont setEnabled:!currentState] ; [btCont setPowered:!currentState] ;
not running

vualoaithu
  • 936
  • 10
  • 9