I'm trying to track whether a bluetooth device is connected on an iOS device or not. I have used the following code:
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface scDeviceManager:NSObject <CBCentralManagerDelegate>
@property (nonatomic, strong) CBCentralManager *centralManager;
+ (scDeviceManager *) sharedInstance;
@end
#import "scDeviceManager.h"
@implementation scDeviceManager
@synthesize centralManager = _centralManager;
+ (scDeviceManager *) sharedInstance {
static scDeviceManager *_sharedInstance=nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
- (id)init {
if (self = [super init]) { // equivalent to "self does not equal nil"
_centralManager=[[CBCentralManager alloc] initWithDelegate:self queue:nil];
//also tried
//_centralManager=[[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
}
return self;
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Peripheral connected");
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
_log=[[Logger alloc] initWithClassName:NSStringFromClass([self class])];
switch (central.state) {
case CBCentralManagerStatePoweredOff:
NSLog(@"CoreBluetooth BLE hardware is powered off.");
break;
case CBCentralManagerStatePoweredOn:
NSLog(@"CoreBluetooth BLE hardware is powered on and ready.");
[_centralManager scanForPeripheralsWithServices:nil options:nil];
break;
case CBCentralManagerStateResetting:
NSLog(@"CoreBluetooth BLE hardware is resetting.");
break;
case CBCentralManagerStateUnauthorized:
NSLog(@"CoreBluetooth BLE state is unauthorized.");
break;
case CBCentralManagerStateUnknown:
NSLog(@"CoreBluetooth BLE state is unknown.");
break;
case CBCentralManagerStateUnsupported:
NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform.");
break;
default:
NSLog([NSString stringWithFormat:@"Nothing to do: state (%ld).",(long)central.state]);
break;
}
}
@end;
At some point I call the object scDeviceManager:
scDeviceManager *deviceManager = [scDeviceManager sharedInstance];
When I activate/diactivate the bluetooth from iOS I receive the message:
2016-04-27 13:33:21.940 CoreBluetooth BLE hardware is powered off.
2016-04-27 13:33:24.046 CoreBluetooth BLE hardware is powered on and ready.
Which indicates that the bluetooth has been activated and that I scan for new bluetooth devices.
However when I connect a bluetooth device (samsung HM1700, bluetooth headset) the didConnectPeripheral is not called. For sure the bluetooth device is connected as I can listen music.
In a custom iOS button I call the following function
-(void) scanConnectedDevices{
NSArray *inputs = [[AVAudioSession sharedInstance] availableInputs];
for (AVAudioSessionPortDescription *port in inputs)
{
connectedAudioDevices[port.portType]=port;
NSLog(@"type:%@, name:%@",port.portyType,port.portName)
}
}
And what I get is:
MicrophoneBuiltIn:iPhone Microphone
BluetoothHFP:HM1700
This indicates that the device is connected. However I would like through a delegate to execute a peace of code whenever I connect/disconnect the bluetooth device.
Thanks