草庐IT

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

coder 2024-01-15 原文

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

有关ios - iBeacon:CBPeripheralManager: 0x1557b230 只能在开机状态下接受这个命令的更多相关文章

  1. ruby - 这个 ruby​​ 注入(inject)魔术是如何工作的? - 2

    我今天看到了一个ruby​​代码片段。[1,2,3,4,5,6,7].inject(:+)=>28[1,2,3,4,5,6,7].inject(:*)=>5040这里的注入(inject)和之前看到的完全不一样,比如[1,2,3,4,5,6,7].inject{|sum,x|sum+x}请解释一下它是如何工作的? 最佳答案 没有魔法,符号(方法)只是可能的参数之一。这是来自文档:#enum.inject(initial,sym)=>obj#enum.inject(sym)=>obj#enum.inject(initial){|mem

  2. ruby - 在好的 Ruby 代码中没有注释是否被认为是可以接受的? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭5年前。Improvethisquestion我审查了一些用Ruby编写的专业代码,没有发现任何评论。代码读起来相当清晰,但没有self记录。我应该期望专业编写的Ruby代码有注释吗?或者,是否有一些Ruby原则认为注释不是必需的?

  3. ruby-on-rails - Ruby rand() 不能接受变量? - 2

    我对此有点困惑。我在RoR项目中的最终目标是从我的数据库中获取单个随机配置文件。我想它应该是这样的:@profile=Profile.find_by_user_id(rand(User.count))它一直抛出错误,因为user_id0不存在,所以我把它的一部分拿出来检查发生了什么:@r=rand(User.count)每次都返回0。发生什么了?我注册了5个假用户和5个相关配置文件来测试这个。如果我将Profile.find_by_user_id(rand(User.count))重写为Profile.find_by_user_id(3)它工作得很好。User.count也在工作。所以

  4. ruby-on-rails - ruby 新手,有人可以帮我从控制台破译这个错误吗? - 2

    我真的只是不确定这意味着什么或我应该做什么才能让网页在我的本地主机上运行。现在它只是显示一个错误,上面写着“我们很抱歉,但出了点问题。”当我运行railsserver并在chrome中打开localhost:3000时。这是控制台输出:StartedGET"/users/sign_in"for127.0.0.1at2013-07-0512:07:07-0400ProcessingbyDevise::SessionsController#newasHTMLCompleted500InternalServerErrorin55msNoMethodError(undefinedmethod`

  5. Ruby gsub 方法 - 接受散列? - 2

    Ruby的gsub字符串方法应该接受散列。如此处所写:http://www.ruby-doc.org/core/classes/String.html#M001185“如果第二个参数是一个哈希,并且匹配的文本是它的键之一,则对应的值是替换字符串。”他们举了一个例子:'hello'.gsub(/[eo]/,'e'=>3,'o'=>'*')#=>"h3ll*"问题是,它对我不起作用(ruby1.8.7):in`gsub':can'tconvertHashintoString(TypeError)这发生在完全相同的行上。为什么? 最佳答案

  6. ruby - ruby 中的同一个程序如何接受来自用户的输入以及命令行参数 - 2

    我的ruby​​脚本从命令行参数获取某些输入。它检查是否缺少任何命令行参数,然后提示用户输入。但是我无法使用gets从用户那里获得输入。示例代码:test.rbname=""ARGV.eachdo|a|ifa.include?('-n')name=aputs"Argument:#{a}"endendifname==""puts"entername:"name=getsputsnameend运行脚本:rubytest.rbraghav-k错误结果:test.rb:6:in`gets':Nosuchfileordirectory-raghav-k(Errno::ENOENT)fromtes

  7. ruby - 为什么这个救援语法有效? - 2

    好的,所以我有了我正在使用的应用程序的这种方法,它可以在生产中使用。我的问题为什么这行得通?这是新的Ruby语法吗?defeditload_elements(current_user)unlesscurrent_user.role?(:admin)respond_todo|format|format.json{render:json=>@user}format.xml{render:xml=>@user}format.htmlendrescueActiveRecord::RecordNotFoundrespond_to_not_found(:json,:xml,:html)end

  8. ruby - 为什么这个 eval 在 Ruby 中不起作用 - 2

    你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva

  9. ruby-on-rails - 使用 Rspec 测试 rake 任务不接受参数 - 2

    根据thispostbyStephenHagemann,我正在尝试为我的一个rake任务编写Rspec测试.lib/tasks/retry.rake:namespace:retrydotask:message,[:message_id]=>[:environment]do|t,args|TextMessage.new.resend!(args[:message_id])endendspec/tasks/retry_spec.rb:require'rails_helper'require'rake'describe'retrynamespaceraketask'dodescribe're

  10. Ruby:我怎样才能复制这个数组? - 2

    (跟进我之前的问题,Ruby:howcanIcopyavariablewithoutpointingtothesameobject?)我正在编写一个简单的Ruby程序来在.svg文件中进行一些替换。第一步是从文件中提取信息并将其放入数组中。为了避免每次调用此函数时都从磁盘读取文件,我尝试使用memoize设计模式-在第一次调用后的每次调用中都使用缓存结果。为此,我使用了一个在函数之前定义的全局变量。但是,即使我在返回局部变量之前将该变量.dup为局部变量,调用该变量的函数仍在修改全局变量。这是我的实际代码:#memoizetokeepfromhavingtoreadoriginalfi

随机推荐