2016-04-27 170 views
0

我试图跟踪蓝牙设备是否连接到iOS设备上。我已经使用以下代码:didConnectPeripheral永远不会被调用

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

当我激活/ diactivate从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

这表示设备已连接。但是,无论何时我连接/断开蓝牙设备,我都希望通过代理来执行代码的和平。

感谢

+0

CoreBluetooth仅适用于蓝牙低功耗,而三星HM1700不是蓝牙低功耗设备。 – Larme

+0

IC ......是否有可能拥有蓝牙耳机的代表? –

+0

http://stackoverflow.com/questions/20896528/how-to-find-bluetooth-audio-devices-in-ios – Larme

回答

0

我可以看到它是多么容易的事情混淆起来)

你在做什么: 基本上在内存中创建蓝牙实例。 然后你通过设置的另一个蓝牙设备 结果连接: 的两件事情是不相关的每个 - 其他 - >你的“码”不知道的耳机,你连接形式另一个地方什么...

如果您正试图弄清楚是否有其他蓝牙设备已连接: 可能您需要成为MFI计划的成员,然后才能连接到您自己生产的耳机。

否则,就我所知,iOS上没有办法仅仅“列出”连接的“古典”蓝牙设备。

相关问题