2016-04-27 54 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上沒有辦法僅僅「列出」連接的「古典」藍牙設備。

相關問題