I trying to get the Status of iPhone/iPod Bluetooth that whether it is ON or OFF programmatically. Is it possible using some Apple API or third party API.
Turn on Bluetooth through the SettingsOpen Settings. Go to Bluetooth. Tap the toggle switch to the right of Bluetooth to turn it on. With Bluetooth turned On, you will see a list of available devices to connect to.
Unfortunately, there is no way to make the iPhone undiscoverable w/o turning Bluetooth off. Users must either leave Bluetooth turned on or off.
A little bit of research into Sam's answer that I thought I'd share You can do so without utilizing private API, but with a few caveats:
CBCentralManagerOptionShowPowerAlertKey
option to NO to prevent permissions prompt. That being said, this method does seem to provide real time updates of bluetooth stack state.
After including the CoreBluetooth framework,
#import <CoreBluetooth/CoreBluetooth.h>
These tests were easy to perform using:
- (void)detectBluetooth { if(!self.bluetoothManager) { // Put on main queue so we can call UIAlertView from delegate callbacks. self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()]; } [self centralManagerDidUpdateState:self.bluetoothManager]; // Show initial state } - (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSString *stateString = nil; switch(self.bluetoothManager.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; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bluetooth state" message:stateString delegate:nil cancelButtonTitle:@"ok" otherButtonTitles: nil]; [alert show]; }
To disable the default alert message you just need to pass through an option dictionary when you instantiate the CBPeripheralManager:
SWIFT tested on iOS8+
import CoreBluetooth //Define class variable in your VC/AppDelegate var bluetoothPeripheralManager: CBPeripheralManager? //On viewDidLoad/didFinishLaunchingWithOptions let options = [CBCentralManagerOptionShowPowerAlertKey:0] //<-this is the magic bit! bluetoothPeripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: options)
Obviously you also need to implement the CKManagerDelegate delegate method peripheralManagerDidUpdateState as outlined above as well:
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) { var statusMessage = "" switch peripheral.state { case .poweredOn: statusMessage = "Bluetooth Status: Turned On" case .poweredOff: statusMessage = "Bluetooth Status: Turned Off" case .resetting: statusMessage = "Bluetooth Status: Resetting" case .unauthorized: statusMessage = "Bluetooth Status: Not Authorized" case .unsupported: statusMessage = "Bluetooth Status: Not Supported" case .unknown: statusMessage = "Bluetooth Status: Unknown" } print(statusMessage) if peripheral.state == .poweredOff { //TODO: Update this property in an App Manager class } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With