2013-03-04 61 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