当我调用if语句检查外设状态时,我的代码一直卡住。当我将操作从!=更改为==时,它会通过并执行NSLog,但调试器表示外围管理器未打开。我该如何纠正这个?
我首先在viewDidLoad方法中启动外围设备管理器:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Start up the CBPeripheralManager
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
NSLog(@"viewDidLoad");
}
然后我调用peripheralManagerDidUpdateState方法:
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheralManager
{
// Opt out from any other state
if (self.peripheralManager.state != CBPeripheralManagerStatePoweredOn)
{
return;
}
// We're in CBPeripheralManagerStatePoweredOn state...
NSLog(@"self.peripheralManager powered on.");
// ... so build our service.
// Then the service
CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID] primary:YES];
// And add it to the peripheral manager
[self.peripheralManager addService:transferService];
}
由于推送了IBAction /按钮,我在程序中稍后调用了startAdvertising命令:
- (IBAction)advertise:(id)sender
{
if(self.peripheralManager.state == CBPeripheralManagerStatePoweredOn)
{
[self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] }];
[self.peripheralManager startAdvertising:@{ CBAdvertisementDataTxPowerLevelKey : @(YES)}];
}
}
通过调用startAdvertising来检查外围设备状态而不是BEFORE与此错误有关吗?