0

I am developing App that will recognize the ibeacons and perform pre-defined action and What I want to do is that when my app is launch the bluetooth will automatically get "ON". If it is possible in iPhone how can i do that?

2 Answers2

1

Sorry, you can't do it programmatically, at least not with iOS. Perhaps you have an option by which you can detect the state of your device bluetooth. Based on that you can set enable/disable criteria for your app.

Below is how to detect bluetooth state in your app:


I am suggesting you to make it simple, create a model class subclassing to NSObject.

make it single ton class then,

#import <CoreBluetooth/CoreBluetooth.h>

add a property

@property (nonatomic, strong) CBCentralManager *bluetoothManager;

assign a delegate in your class

YourClass<CBCentralManagerDelegate>

add an init method like this,

- (instancetype) init {
    self = [super init];
    if(self) {
        self.bluetoothManager = [[CBCentralManager alloc]
                                 initWithDelegate:self
                                 queue:dispatch_get_main_queue()
                                 options:@{CBCentralManagerOptionShowPowerAlertKey: @(NO)}];
    }
    return self;
}

add this delegate method to get states for your bluetooth

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    NSString *stateString = nil;
    switch(central.state)
    {
        case CBCentralManagerStateResetting:
        {
            stateString = @"The connection with the system service was momentarily lost, update imminent.";
            break;
        }
        case CBCentralManagerStateUnsupported:{
            stateString = @"The platform doesn't support Bluetooth Low Energy.";
            break;
        }
        case CBCentralManagerStateUnauthorized: {
            stateString = @"The app is not authorized to use Bluetooth Low Energy.";
            break;
        }
        case CBCentralManagerStatePoweredOff: {
            stateString = @"Bluetooth is currently powered off.";
            break;
        }
        case CBCentralManagerStatePoweredOn: {
            stateString = @"Bluetooth is currently powered on and available to use.";
            break;
        }
        default: stateString = @"State unknown, update imminent."; break;
    }
    NSLog(@"Bluetooth State: %@",stateString);
}

mainly important: you need enable following capabilities for your project.

enter image description here

Hemang
  • 26,840
  • 19
  • 119
  • 186
0

Add this in project

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

#import <BluetoothManager/BluetoothManager.h>

To enable the bluetooth here is the code:

BluetoothManager *manager = [BluetoothManager sharedInstance];
[manager setEnabled:YES];  
Awesome.Apple
  • 1,316
  • 1
  • 11
  • 24
  • Thanks Kiran,I have created header file and pest the code of all the .h file that u provided me but i m getting these errors Undefined symbols for architecture arm64: "_OBJC_CLASS_$_BluetoothManager", referenced from: objc-class-ref in AppDelegate.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – Shashi Kumar Mar 11 '16 at 05:47
  • Have you included BluetoothManager in project? – Awesome.Apple Mar 11 '16 at 05:48
  • Are you sure the code u have provided it will work.. – Shashi Kumar Mar 11 '16 at 06:39
  • http://stackoverflow.com/questions/1743610/programmatically-turn-on-bluetooth-in-the-iphone-sdk – Awesome.Apple Mar 11 '16 at 06:44