I'm developing an app for Apple Watch Series 3, and it seems that the state of CBCentralManager is always CBManagerStateUnsupported.
I'm using the following code:
#import <CoreBluetooth/CoreBluetooth.h>
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *stateString = nil;
switch(central.state)
{
case CBManagerStateResetting:
stateString = #"The connection with the system service was momentarily lost, update imminent.";
break;
case CBManagerStateUnsupported:
stateString = #"The platform doesn't support Bluetooth Low Energy.";
break;
case CBManagerStateUnauthorized:
stateString = #"The app is not authorized to use Bluetooth Low Energy.";
break;
case CBManagerStatePoweredOff:
stateString = #"Bluetooth is currently powered off.";
break;
case CBManagerStatePoweredOn:
stateString = #"Bluetooth is currently powered on and available to use.";
[central scanForPeripheralsWithServices:nil options:
[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
forKey:CBCentralManagerOptionShowPowerAlertKey]];
break;
default:
stateString = #"State unknown, update imminent.";
break;
}
}
What am I doing wrong?
Related
I am using the centralManagerDidUpdateState in my code.
I can check the bluetooth ON/Off in programming like below:
#interface ViewController ()<CBCentralManagerDelegate,CBPeripheralManagerDelegate>
{
CBCentralManager *bleCentralManager;
CBPeripheralManager *blePeripheralManager;
}
#end
- (void)viewDidLoad {
[super viewDidLoad];
bleCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:#{CBCentralManagerOptionShowPowerAlertKey: #NO}];
blePeripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:#{CBConnectPeripheralOptionNotifyOnDisconnectionKey:#YES}];
bleStatus = #"CBManagerStateUnknown";
}
-(void) centralManagerDidUpdateState:(CBCentralManager *)central{
NSLog(#"centralManagerDidUpdateState:%ld",central.state);
switch (central.state) {
case CBManagerStateUnknown:
bleStatus = #"CBManagerStateUnknown";
break;
case CBManagerStateResetting:
bleStatus = #"CBManagerStateResetting";
break;
case CBManagerStateUnsupported:
bleStatus = #"CBManagerStateUnsupported";
break;
case CBManagerStateUnauthorized:
bleStatus = #"CBManagerStateUnauthorized";
break;
case CBManagerStatePoweredOff:
bleStatus = #"CBManagerStatePoweredOff";
break;
case CBManagerStatePoweredOn:
bleStatus = #"CBManagerStatePoweredOn";
break;
default:
break;
}
}
- (void)peripheralManagerDidUpdateState:(nonnull CBPeripheralManager *)peripheral {
NSLog(#"peripheral:%ld",(long)peripheral.state);
}
But in the iPhone control center, we can change the bluetooth status to disconnect,
the turn off status is can't recognition.
When the bluetooth icon disconnect and bluetooth(go to setting) turn off, I get the status is CBManagerStaePoweredOff.
We can refer the disconnect/off info in control panel below:
https://support.apple.com/en-us/HT208086
How can I check the status is Disconnect from Bluetooth icon or Turn off bluetooth(go to setting bluetooth edit turn off) in the control panel icon?
thank you very much.
I have built an Android APP which handles the scan, return nearby devices, and connect steps on both BLE and Bluetooth A2DP, and it works well. Now I’m developing the iOS version which is with exactly the same functionalities. For the BLE part, I can use CoreBluetooth to perform what I need without any problems, yet I don’t know how to implement the steps of “scan -> return nearby discoverable devices -> connect” on iOS for Bluetooth A2DP device. The only solution I’ve found so far is to navigate to Settings page from my iOS APP and perform the connection on it. Is there any way to implement the Bluetooth A2DP connection process inside of my iOS APP programmatically?
in iOS, bluetooth works on central peripheral concept. below is the how it scan for nearby devices.
#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>
#interface MyViewController : UIViewController <CBPeripheralDelegate, CBCentralManagerDelegate>
{
CBCentralManager *mgr;
}
#property (readwrite, nonatomic) CBCentralManager *mgr;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog([NSString stringWithFormat:#"%#",[advertisementData description]]);
}
-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
NSLog(#"This is it!");
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSString *messtoshow;
switch (central.state) {
case CBCentralManagerStateUnknown:
{
messtoshow=[NSString stringWithFormat:#"State unknown, update imminent."];
break;
}
case CBCentralManagerStateResetting:
{
messtoshow=[NSString stringWithFormat:#"The connection with the system service was momentarily lost, update imminent."];
break;
}
case CBCentralManagerStateUnsupported:
{
messtoshow=[NSString stringWithFormat:#"The platform doesn't support Bluetooth Low Energy"];
break;
}
case CBCentralManagerStateUnauthorized:
{
messtoshow=[NSString stringWithFormat:#"The app is not authorized to use Bluetooth Low Energy"];
break;
}
case CBCentralManagerStatePoweredOff:
{
messtoshow=[NSString stringWithFormat:#"Bluetooth is currently powered off."];
break;
}
case CBCentralManagerStatePoweredOn:
{
messtoshow=[NSString stringWithFormat:#"Bluetooth is currently powered on and available to use."];
[mgr scanForPeripheralsWithServices:nil options:nil];
//[mgr retrieveConnectedPeripherals];
//--- it works, I Do get in this area!
break;
}
}
NSLog(messtoshow);
}
I am trying to check whether the bluetooth is turned on/off with the following code. But it returns CBManagerStatePoweredOff even-though the bluetooth is already on. I checked it in iPhone6s and iOS version 11.2.5(15D60). If i restart bluetooth manually on the settings, It returns CBManagerStatePoweredOn.
- (void)detectBluetooth
{
if(!bluetoothManager)
{
// Put on main queue so we can call UIAlertView from delegate callbacks.
// NSDictionary *options = #{CBCentralManagerOptionShowPowerAlertKey: #NO};
// bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
[bluetoothManager scanForPeripheralsWithServices:nil options:nil];
}
[self centralManagerDidUpdateState:bluetoothManager]; // Show initial state
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *stateString = nil;
switch(central.state)
{
case CBManagerStateResetting:
stateString = #"The connection with the system service was momentarily lost, update imminent.";
break;
case CBManagerStateUnsupported:
stateString = #"The platform doesn't support Bluetooth Low Energy.";
break;
case CBManagerStateUnauthorized:
stateString = #"The app is not authorized to use Bluetooth Low Energy.";
break;
case CBManagerStatePoweredOff:
stateString = #"Bluetooth is currently powered off.";
break;
case CBManagerStatePoweredOn:
[self goToSearchDevices];
break;
default: stateString = #"State unknown, update imminent."; break;
}
}
Is it possible to detect the Apple TV 4 Siri Remote from an iOS application using CoreBluetooth? I'm able to detect the Apple TV, but I'm not having any luck detecting the Siri Remote. The Siri Remote uses Bluetooth 4.0 so I'm assuming it is detectable. Ideally, I'd like to detect the Siri Remote even if it's already paired with the Apple TV.
Simply being able to detect any signal from the Siri Remote/know it's in the vicinity of the users iPhone is what I'm after.
#import "ViewController.h"
#import CoreBluetooth;
#interface ViewController () <CBPeripheralDelegate, CBCentralManagerDelegate>
#end
#implementation ViewController {
CBCentralManager *btManager;
}
-(void)viewDidLoad {
[super viewDidLoad];
btManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
#pragma mark - CBCentralManagerDelegate Methods
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(#"peripheral name: %#", peripheral.name);
NSLog(#"peripheral services: %#", peripheral.services);
NSLog(#"peripheral identifier: %#", peripheral.identifier);
NSLog(#"peripheral state: %ld", (long)peripheral.state);
NSLog(#"RSSI: %# \n\n", RSSI);
}
-(void)centralManagerDidUpdateState:(CBCentralManager *)central {
NSString *nsLogMessage;
switch (central.state) {
case CBCentralManagerStateUnknown: {
nsLogMessage = [NSString stringWithFormat:#"State unknown, update imminent."];
break;
}
case CBCentralManagerStateResetting: {
nsLogMessage = [NSString stringWithFormat:#"The connection with the system service was momentarily lost, update imminent."];
break;
}
case CBCentralManagerStateUnsupported: {
nsLogMessage = [NSString stringWithFormat:#"The platform doesn't support Bluetooth Low Energy"];
break;
}
case CBCentralManagerStateUnauthorized: {
nsLogMessage = [NSString stringWithFormat:#"The app is not authorized to use Bluetooth Low Energy"];
break;
}
case CBCentralManagerStatePoweredOff: {
nsLogMessage = [NSString stringWithFormat:#"Bluetooth is currently powered off."];
break;
}
case CBCentralManagerStatePoweredOn: {
nsLogMessage = [NSString stringWithFormat:#"Bluetooth is currently powered on and available to use."];
NSDictionary *scanningOptions = #{CBCentralManagerScanOptionAllowDuplicatesKey: #YES};
[btManager scanForPeripheralsWithServices:nil options:scanningOptions];
break;
}
}
NSLog(#"%#", nsLogMessage);
}
I am developing a module in which I need to get all bleutooth devices and connect the selected device. I am using this
blueManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString *messtoshow;
switch (central.state) {
case CBCentralManagerStateUnknown:
{
messtoshow=[NSString stringWithFormat:#"State unknown, update imminent."];
break;
}
case CBCentralManagerStateResetting:
{
messtoshow=[NSString stringWithFormat:#"The connection with the system service was momentarily lost, update imminent."];
break;
}
case CBCentralManagerStateUnsupported:
{
messtoshow=[NSString stringWithFormat:#"The platform doesn't support Bluetooth Low Energy"];
break;
}
case CBCentralManagerStateUnauthorized:
{
messtoshow=[NSString stringWithFormat:#"The app is not authorized to use Bluetooth Low Energy"];
break;
}
case CBCentralManagerStatePoweredOff:
{
messtoshow=[NSString stringWithFormat:#"Bluetooth is currently powered off."];
break;
}
case CBCentralManagerStatePoweredOn:
{
messtoshow=[NSString stringWithFormat:#"Bluetooth is currently powered on and available to use."];
[self.blueManager scanForPeripheralsWithServices:nil options:nil];
CBUUID *deviceInfoUUID = [CBUUID UUIDWithString:#"180A"];
NSLog(#"connected devices ios 8 code%# ",[blueManager retrieveConnectedPeripheralsWithServices:#[deviceInfoUUID]]);
}
and my bluetooth is on and connected to two devices which I can see from setting but I am not getting any device. I have checked in
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {}
method but it is not calling. And I know that two BLE devices is already connected to my device