0

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

Symeon Mattes
  • 1,169
  • 16
  • 40

1 Answers1

0

I can see how easy it is to mix things up ;)

What you are doing: Basically creating the bluetooth instance in memory. Then you connect through the settings another bluetooth device Result: The 2 things are not related to each-other -> your "code" does not know anything about the headset you connected form another place...

If you are trying to figure out if another bluetooth device is connected: Probably you would need to become a member of the MFI program and then you would be able to connect to your own manufactured headset.

Otherwise, and as much as i know, there is no way on iOS to just "list" connected "classical" bluetooth devices.

p2pkit
  • 1,159
  • 8
  • 11