2013-06-18 102 views
3

我想用CoreBluetooth(如我的應用程序必須符合在App Store)檢查所有當前配對並連接藍牙設備。這不應該需要任何藍牙掃描,對吧?我只想看看系統配對了什麼。如果我不能這樣做,掃描是第二種選擇。(IOS)如何檢查的iPhone的配對的藍牙設備?

我試圖不工作。它表示藍牙未通電並崩潰,但CBCentralManager的狀態爲開啓!任何想法如何解決這個問題,還是我完全偏離軌道?

所有這一切都在視圖控制器現在:

- (void)viewDidLoad{ 

    [super viewDidLoad]; 
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()]; 

    [self.centralManager retrieveConnectedPeripherals]; //makes the system call didRetrieveConnectedPeripherals 
} 

- (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals{ 
    NSLog(@"didRetrieveConnectedPeripherals called"); 
    for (CBPeripheral *a in peripherals){ 
     NSLog(a.name); //just log the name for now to see if it recognized it 
    } //but it never ends up logging anything, and I have a BT keyboard paired/connected with the iPhone 5 
} //and I get an error at some point, <CBConcreteCentralManager: 0x71ab020> is not powered on 

- (void)centralManagerDidUpdateState:(CBCentralManager *)manager{ 
    if ([manager state] == CBCentralManagerStatePoweredOff) NSLog(@"CBCentralManagerStatePoweredOff"); 
    if ([manager state] == CBCentralManagerStatePoweredOn) NSLog(@"CBCentralManagerStatePoweredOn"); //this is what gets logged when I run it on an iPhone 5 
    if ([manager state] == CBCentralManagerStateResetting) NSLog(@"CBCentralManagerStateResetting"); 
    if ([manager state] == CBCentralManagerStateUnauthorized) NSLog(@"CBCentralManagerStateUnauthorized"); 
    if ([manager state] == CBCentralManagerStateUnknown) NSLog(@"CBCentralManagerStateUnknown"); 
    if ([manager state] == CBCentralManagerStateUnsupported) NSLog(@"CBCentralManagerStateUnsupported"); 
} 

回答

0

我一直在工作,這也,並希望一些我學到的東西會有所幫助。

幾件事情:

1)你可能得到,因爲你在初始化CBCentralManager後立即調用[self.central retrieveConnectedPeripherals]「藍牙不通電」的錯誤。您需要讓CBCentralManager有足夠的時間連接到藍牙硬件。

試試這個,

- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 
} 
- (void)centralManagerDidUpdateState:(CBCentralManager *)manager { 
    switch (manager.state) { 
     case CBCentralManagerStatePoweredOn: 
      NSLog(@"CBCentral Manager powered on"); 
      [self.centralManager retrieveConnectedPeripherals]; 
      break; 
     case CBCentralManagerStatePoweredOff: 
      ...etc. 
} 

而且,要知道,CBCentralManager適用於與藍牙4.0的iOS設備。目前,藍牙4安裝在iPhone 4S/5,iPod 4和iPad 3/4/mini上。因此,請注意,您的應用程序無法在iPad 1/2,iPhone 2/3和iPod 2/3上運行。

還是搞清楚了這一點,但它也有可能是你可能看不到你的鍵盤,因爲它不是一個藍牙設備4。讓我知道你發現了什麼。