我一直收到一个错误,说我的CBPeripheralManager没有开机,但在我的代码中,我觉得我实现了这一点.这是我的代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view,typically from a nib. // Start up the CBPeripheralManager _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; // Start up the CBCentralManager // And somewhere to store the incoming data _data = [[NSMutableData alloc] init]; } /** required protocol method. A full app should take care of all the possible states,* but we're just waiting for to know when the CBPeripheralManager is ready */ - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { if (peripheral.state == CBPeripheralManagerStatePoweredOn) { // 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按钮做广告:
- (IBAction)advertise:(id)sender { [self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] }]; [self.peripheralManager startAdvertising:@{ CBAdvertisementDataTxPowerLevelKey : @(YES)}]; }