Sha*_*Dev 2 objective-c ios 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
Run Code Online (Sandbox Code Playgroud)
在调用CBPeripheralManager方法之前,必须打开外围管理器对象的状态,如下所示
CBPeripheralManagerStatePoweredOn.此状态表示外围设备(例如,您的iPhone或iPad)支持低功耗蓝牙,并且其蓝牙已打开并可供使用.
为了确定外围设备管理器何时就绪,您需要实现didUpdateState外围设备管理器委托方法,并在获得已启动状态后开始做广告,但是startAdvertising在分配完成后,您也可以直接进行呼叫CBPeripheralManager,它会给你错误信息,因为它还没有处于开机状态
| 归档时间: |
|
| 查看次数: |
2463 次 |
| 最近记录: |