永远不会调用didConnectPeripheral

时间:2016-04-27 10:46:38

标签: ios objective-c delegates ios-bluetooth

我正在尝试跟踪是否在iOS设备上连接了蓝牙设备。我使用了以下代码:

#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;

在某些时候,我调用对象scDeviceManager:

scDeviceManager *deviceManager = [scDeviceManager sharedInstance];

当我从iOS激活/停用蓝牙时,我收到消息:

  

2016-04-27 13:33:21.940 CoreBluetooth BLE硬件已关机。

     

2016-04-27 13:33:24.046 CoreBluetooth BLE硬件已启动并且已启动   准备好了。

这表示蓝牙已被激活,我扫描新的蓝牙设备。

然而,当我连接蓝牙设备(三星HM1700,蓝牙耳机)时,不会调用didConnectPeripheral。确保蓝牙设备已连接,因为我可以听音乐。

在自定义iOS按钮中,我调用以下函数

-(void) scanConnectedDevices{

    NSArray *inputs = [[AVAudioSession sharedInstance] availableInputs];
    for (AVAudioSessionPortDescription *port in inputs)
    {
        connectedAudioDevices[port.portType]=port;
        NSLog(@"type:%@, name:%@",port.portyType,port.portName)
    }

}

我得到的是:

  

MicrophoneBuiltIn:iPhone麦克风

     

BluetoothHFP:HM1700

这表示设备已连接。但是,每当我连接/断开蓝牙设备时,我都希望代理人能够执行代码的和平。

由于

1 个答案:

答案 0 :(得分:0)

我可以看出混合起来是多么容易;)

你在做什么: 基本上在内存中创建蓝牙实例。 然后你通过设置连接另一个蓝牙设备 结果: 这2件事与彼此无关 - &gt;你的代码&#34;我不知道你在另一个地方连接的耳机......

如果您想弄清楚是否连接了另一个蓝牙设备: 您可能需要成为MFI计划的成员,然后您就可以连接到自己制造的耳机。

否则,据我所知,iOS上没有任何方法只能&#34; list&#34;连接&#34;经典&#34;蓝牙设备。

相关问题