2014-02-11 95 views
2

我在我的應用程序中使用了Core Bluetooth Framework。在iOS IOS7中檢測啓用了藍牙的iPhone設備

我知道如何掃描從它的外圍設備和獲取值。(如心率監視器)

但我要的是檢索支持BLE 4.0和藍牙啓用那些周圍的iPhone設備列表中。

我提到下面的鏈接..

Uses IOBluetooth Framework

Uses CoreBluetooth For Getting Peripherals not the Devices List

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { 
    // I'm not sure how to make this work 

    NSLog (@"Discovered peripheral: %@", [peripheral name]); 
    [self.list addObject:peripheral.name]; // add peripheral name to foundarray 
    NSLog (@"UUID peripheral: %@", [peripheral UUID]); 

    NSLog (@"peripheral services before connected: %@", [peripheral services]); 

    NSLog(@"adversting data %@",[NSString stringWithFormat:@"%@",[advertisementData description]]); 

    NSLog(@"foundArray is %@", self.list); 
} 

- (void)centralManagerDidUpdateState:(CBCentralManager *)central { 
    NSLog(@"Central manager's state is updated to: %@", central); 
    if(central.state == CBCentralManagerStatePoweredOn) 
    { 
     //okay your good to go and can now scan 
    } 
    else 
    { 
     //Unable to use CentralManager methods so print out the central.state and find out why 
    } 
} 

我不知道蘋果是否提供這種或不..

任何建議或想法可以理解的。 。

提前致謝。

+0

BLE被支撐因爲iPhone 4S以上,ipad公司3及以上。您不會通過代碼獲取列表。無論如何,看到CBCentralManager狀態應該給你,如果設備支持BLE或不(CBCentralManagerStateUnsupported)。 – Larme

回答

1

如果只能檢索支持藍牙4.0的周邊iOS設備,並且它們也在宣傳特定服務以識別它們,您不能只看到所有打開電源和在附近的iOS設備。如果他們是廣告,你可以掃描零,這將返回被看到的廣告數據包。

注意:如果您關心的是檢索當前從其他應用程序連接的BLE設備,則可以使用retrieveConnectedPeripheralsWithServices:方法。

+1

如果我去設置/藍牙我可以找到設備,但是當我掃描使用: scanForPeripharlsWithServices:無 我沒有找到任何。爲什麼? – iosMentalist

2

嘗試以下代碼checkBluetoothAccessrequestBluetoothAccess方法

- (void)checkBluetoothAccess { 

      if(!self.cbManager) { 
       self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 
      } 

     /* 
      We can ask the bluetooth manager ahead of time what the authorization status is for our bundle and take the appropriate action. 
     */ 

     CBCentralManagerState state = [self.cbManager state]; 

     if(state == CBCentralManagerStateUnknown) { 
       [self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@"UNKNOWN", @"")]; 
     } 
     else if(state == CBCentralManagerStateUnauthorized) { 
       [self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@"DENIED", @"")]; 
     } 
    else { 
       [self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@"GRANTED", @"")]; 
     } 
} 


- (void)requestBluetoothAccess { 

     if(!self.cbManager) { 
       self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 
     } 

    /* 
      When the application requests to start scanning for bluetooth devices that is when the user is presented with a consent dialog. 
    */ 

    [self.cbManager scanForPeripheralsWithServices:nil options:nil]; 

    } 
+0

@ bhavya..Getting error at this line .. [self alertViewWithDataClass:Bluetooth status:NSLocalizedString(@「GRANTED」,@「」)]; } 這是什麼藍牙..? – Vidhyanand

+0

@Vidhyanand - 只是刪除該類型的行 - 你可以把一個簡單的UIAlertView或NSLog –