2013-03-04 30 views
0

我似乎無法獲得在我的iPad上工作的核心藍牙。似乎無法讓核心藍牙工作

ViewController.h

@interface ViewController : UIViewController <CBCentralManagerDelegate, CBPeripheralDelegate> 
{ 
    CBCentralManager *manager; 
} 
@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@property (strong, nonatomic) IBOutlet UITextView *textField; 

@end 

@implementation ViewController 

@synthesize textField; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)action:(id)sender { 
    textField.text = @""; 

    if (manager.state == CBCentralManagerStatePoweredOn) { 
     textField.text = @"Scanning..."; 
     NSLog(@"scanning"); 
     [manager scanForPeripheralsWithServices:nil options:nil]; 
    } else { 
     textField.text = @"Error"; 
    } 
} 

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { 
    NSLog(@"2"); 
    textField.text = [NSString stringWithFormat:@"%@%@\n", textField.text, peripheral.name]; 
} 

- (void)centralManagerDidUpdateState:(CBCentralManager *)central { 
    NSLog(@"d"); 
} 

@end 

2永遠不會被記錄,並且從不檢測設備。我確保在我的設置中啓用了藍牙。

代碼有什麼問題?難道僅僅是沒有發現適用的設備?我可以在藍牙設置中發現我的iMac。

此外,核心藍牙(運行在具有藍牙LE的設備上)是否能檢測到非藍牙LE設備?如無線耳機?

回答

2

核心藍牙僅適用於藍牙低功耗,您所擁有的代碼僅會發現外圍BLE設備,如BLE標籤,心率監測器,傳感器或iOS 6 devices in peripheral mode

所以不,你不能通過這種方式檢測你的iMac(OS X仍然沒有通過核心藍牙的外設模式)或無線耳機。或者有官方SDK的任何其他方式。

+0

你確定你不能用Mac OS X做這件事嗎?我的MacBookAir有一個BLE芯片。看來CoreBluetooth與Peripheral&Central至少在10.7 ... – Larme 2013-03-04 14:13:50

+0

在Mac OS X 10.7和iOS 5上的核心藍牙只支持中央模式。 iOS 6支持外設模式,但10.8仍然不支持。因此,您可以使用iOS 5或更高版本或OS X 10.7或更高版本連接到Bluetooth低能耗外設,但只能用作使用iOS 6的外圍設備。 – 2013-03-04 15:40:54

+0

確認此問題的最簡單方法是使用NS_CLASS_AVAILABLE(NA,6_0)'在'CBPeripheralManager'頭文件中(第一個參數是OS X,第二個是iOS版本 - >'NA'表示它在Mac上不支持)。另外,你可以在上面鏈接的鏈接中找到WWDC視頻,因爲文檔在這方面很糟糕,所以在這裏詳細描述。 – 2013-03-04 15:43:23