我正在尝试使用CoreBluetooth框架扫描所有可用的外围设备。
@implementation ViewController
@synthesize myCentralManager;
-(id)init{
self=[super init];
if(self){
myCentralManager=[[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
[myCentralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@YES}];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
myCentralManager=[[CBCentralManager alloc]initWithDelegate:self queue:nil];
//[self scanForPeripherals];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
+ (NSString*)UUIDString:(CFUUIDRef)uuid {
CFStringRef string = CFUUIDCreateString(NULL, uuid);
return (__bridge_transfer NSString*)string;
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
//NSLog(@"DidUpdateState");
if(central.state==CBCentralManagerStateUnknown)
NSLog(@"Unknown State");
else
if(central.state==CBCentralManagerStateUnsupported)
NSLog(@"State Unsupported");
else
if (central.state == CBCentralManagerStatePoweredOn) {
[self scanForPeripherals];
} else
NSLog(@"Turn on Bluetooth");
}
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {
NSLog(@"Received peripheral : \n%@", peripheral);
NSLog(@"Adv data : %@", advertisementData);
[peripheral setDelegate:self];
[central connectPeripheral:peripheral options:nil];
[peripheral readRSSI];
}
- (int)scanForPeripherals {
NSLog(@"Scanning");
if(self.myCentralManager.state!=CBCentralManagerStatePoweredOn)
NSLog(@"Turn on Bluetooth");
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey,
nil];
NSLog(@"Scanning");
[myCentralManager scanForPeripheralsWithServices:nil options:options];
return 0;
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"didConnectPeripheral");
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"didDisconnectPeripheral");
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"failed to connect");
}
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error {
NSLog(@"didReadRSSI");
}
@end
即使我检查了我的系统支持蓝牙4.0 LMP 0x6,我的代码总是返回State Unsupported。任何帮助将非常感谢! 感谢。