iBeacon:CBPeripheralManager:0x1557b230只能在通电状态下接受此命令

时间:2016-02-13 08:02:43

标签: ios objective-c ibeacon

我有一台配备iOS 7.1的4s设备。我正在尝试实现一些代码,这些代码可以帮助我的设备像信标设备那样运行,但我收到错误“只能在启动状态下接受此命令”。

我正在实施这段代码:

@implementation ViewController


-(void)viewDidLoad 
{


    [super viewDidLoad];
    beaconPeripheralData=[[NSDictionary alloc]init];
     peripheralManager.delegate=self;
    _locationManager.delegate=self;
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    [self initWithBeacons];

}

-(void)initWithBeacons

{


    NSNumber * power = [NSNumber numberWithInt:-63];
    NSUUID *uuid=[[NSUUID alloc]initWithUUIDString:@"F24BDBE3-EB98-4A04-A621-91C088DC32D2"];

    CLBeaconRegion *beaconReason=[[CLBeaconRegion alloc]initWithProximityUUID:uuid major:1 identifier:@"blackbean.com"];

    beaconPeripheralData=[beaconReason peripheralDataWithMeasuredPower:power];

peripheralManager=[[CBPeripheralManager alloc]initWithDelegate:self queue:nil];

 [peripheralManager startAdvertising:beaconPeripheralData];

    if ([peripheralManager isAdvertising])
    {

        NSLog(@"peripeheralMAnager is advertising");
    }
    else
    {
        NSLog(@"peripeheralMAnager is not  advertising");
    }

}

-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {


    switch (peripheral.state) {
        case CBPeripheralManagerStatePoweredOn:
            NSLog(@"Powered on");
            [peripheralManager startAdvertising:beaconPeripheralData];
            break;
        case CBPeripheralManagerStatePoweredOff:
            NSLog(@"Powered Off");
            [peripheralManager stopAdvertising];
            break;
        case CBPeripheralManagerStateUnsupported:
            NSLog(@"Device not supported");
            break;
        default:
            break;
    }

}

@end

2 个答案:

答案 0 :(得分:5)

来自CBPeripheralManager documentation

  

在调用CBPeripheralManager方法之前,状态为   外围管理器对象必须打开电源,如下所示   CBPeripheralManagerStatePoweredOn。这种状态表明了   外围设备(例如,您的iPhone或iPad)支持   蓝牙功耗低,蓝牙功能正常   使用

为了确定外围设备管理器何时准备就绪,您需要实施didUpdateState外围设备管理器委托方法,并在获得已启动状态后开始做广告,但您也有呼叫分配startAdvertising之后直接CBPeripheralManager,这会给你错误信息,因为它还没有处于开机状态

答案 1 :(得分:0)

我一直在关注Apple的Turning an iOS Device into an iBeacon官方文章,并指出要使用以下代码:

func advertiseDevice(region : CLBeaconRegion) {
    let peripheral = CBPeripheralManager(delegate: self, queue: nil)
    let peripheralData = region.peripheralData(withMeasuredPower: nil)

    peripheral.startAdvertising(((peripheralData as NSDictionary) as! [String : Any]))
}

但是,最后一行导致“只有在开机状态下才能接受此命令”错误。

要修复它,我必须:

  • 存储对CBPeripheralManager的强大引用,以免被释放。
  • 收听didUpdateState方法,仅在poweredOn时调用startAdvertising方法。
  • 使用真实的iPhone进行广告。如果尝试使用模拟器,则会得到CBManagerState unsupported

以下是广告代码看上去需要避免该错误的示例,而不是苹果公司的示例:

class Foo: NSObject {
  var manager: CBPeripheralManager?

  func advertiseDevice() {
    self.manager = CBPeripheralManager(delegate: self, queue: nil)
  }
}

extension Foo: CBPeripheralManagerDelegate {

  func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
    notifyOfStatusChange("iBeacon advertiser state change: \(peripheral.state.rawValue)")
    if peripheral.state == .poweredOn {
      let region = createBeaconRegion()!
      let peripheralData = region.peripheralData(withMeasuredPower: nil)
      peripheral.startAdvertising(((peripheralData as NSDictionary) as! [String : Any]))
    }
  }
}