Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically turn on bluetooth in the iphone sdk?

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)

like image 239
johnathon Avatar asked Nov 16 '09 17:11

johnathon


People also ask

How do you enable Bluetooth on iPhone?

On iPhone, go to Settings > Bluetooth, turn on Bluetooth, then tap the name of the device.

How do I get my iPhone app to work with Bluetooth?

If there's an app that you use with the Bluetooth accessory, go to Settings > Privacy > Bluetooth on your iOS or iPadOS device, and make sure that you've turned on Bluetooth for the app.

Can iOS simulator connect to Bluetooth?

The simulator does support Bluetooth Low Energy (4.0) according to this appnote from Apple.

How do I turn my Bluetooth App on?

General Android Bluetooth settings:Search for the Settings app on your device, then select the app. Scroll down to and select Bluetooth or the Bluetooth symbol. Toggle Bluetooth to On.


2 Answers

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).

like image 180
2 revs Avatar answered Oct 05 '22 11:10

2 revs


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
like image 36
WrightsCS Avatar answered Oct 05 '22 12:10

WrightsCS