gpt4 book ai didi

ios - iBeacon:CBPeripheralManager: 0x1557b230 只能在开机状态下接受这个命令

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:25:02 24 4
gpt4 key购买 nike

我有一台装有 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

最佳答案

来自CBPeripheralManager documentation

Before you call CBPeripheralManager methods, the state of the peripheral manager object must be powered on, as indicated by the CBPeripheralManagerStatePoweredOn. This state indicates that the peripheral device (your iPhone or iPad, for instance) supports Bluetooth low energy and that its Bluetooth is on and available to use.

为了确定外设管理器何时准备就绪,您需要实现 didUpdateState外设管理器委托(delegate)方法并在您获得开机状态后开始广告,您已经完成了,但是在您分配了 CBPeripheralManager 之后,您还可以直接调用 startAdvertising,这会给你错误信息,因为它还没有处于开机状态

关于ios - iBeacon:CBPeripheralManager: 0x1557b230 只能在开机状态下接受这个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35377555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com