我似乎无法让核心蓝牙在我的 iPad 上工作。
视图控制器.h
@interface ViewController : UIViewController <CBCentralManagerDelegate, CBPeripheralDelegate>
{
CBCentralManager *manager;
}
@end
视图控制器.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 很好。
此外,Core Bluetooth(在具有蓝牙 LE 的设备上运行)可以检测到非蓝牙 LE 设备吗?比如无线耳机?