CBManager状态始终未知

问题描述 投票:1回答:3

我试图检查设备的蓝牙是打开还是关闭。这是我到目前为止编写的代码

   CBCentralManager *cbManager = [[CBCentralManager alloc] initWithDelegate:self
                                                                       queue:nil
                                                                     options:
                                   [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
                                                               forKey:CBCentralManagerOptionShowPowerAlertKey]];


    [cbManager scanForPeripheralsWithServices:nil options:
                                    [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0]
                                                                forKey:CBCentralManagerOptionShowPowerAlertKey]];

    if (cbManager.state==CBCentralManagerStatePoweredOff)
    {
        //do stuff
    }

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    NSString *stateString = nil;
    switch(bluetoothManager.state)
    {
        case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break;
        case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break;
        case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break;
        case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break;
        case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; break;
        default: stateString = @"State unknown, update imminent."; break;
    }
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Bluetooth state"
                                                     message:stateString
                                                    delegate:nil
                                           cancelButtonTitle:@"Okay" otherButtonTitleArray:nil] autorelease];
    [alert show];
}

问题是,无论设备上的蓝牙是否打开,管理器的状态始终为“未知”。关于为什么会这样的想法?

ios objective-c core-bluetooth
3个回答
3
投票

两件事情 -

  1. 你的CBCentralManager应该是一个属性,否则它将在你退出时初始化它的方法被释放
  2. 在处于通电状态之前,不应该调用scanForPeripheralsWithServices@property (strong,nonatomic) CBCentralManager *cbManager; self.cbManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options: [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:CBCentralManagerOptionShowPowerAlertKey]]; - (void)centralManagerDidUpdateState:(CBCentralManager *)central { NSString *stateString = nil; switch(central.state) { case CBCentralManagerStateResetting: stateString = @"The connection with the system service was momentarily lost, update imminent."; break; case CBCentralManagerStateUnsupported: stateString = @"The platform doesn't support Bluetooth Low Energy."; break; case CBCentralManagerStateUnauthorized: stateString = @"The app is not authorized to use Bluetooth Low Energy."; break; case CBCentralManagerStatePoweredOff: stateString = @"Bluetooth is currently powered off."; break; case CBCentralManagerStatePoweredOn: stateString = @"Bluetooth is currently powered on and available to use."; [central scanForPeripheralsWithServices:nil options: [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:CBCentralManagerOptionShowPowerAlertKey]]; break; default: stateString = @"State unknown, update imminent."; break; } UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Bluetooth state" message:stateString delegate:nil cancelButtonTitle:@"Okay" otherButtonTitleArray:nil] autorelease]; [alert show]; }

-1
投票

方法

- (无效)centralManagerDidUpdateState(CBCentralManager *)中央

叫做。试着实现这个

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
  // Possible states that the central manager can return.
  char *managerStrings[] = {"Unknown Error",            "Resetting",
                            "Unsupported Device",       "Unauthorized",
                            "Bluetooth is deactivated", "PoweredOn"};

  NSString *managerString =
      [NSString stringWithFormat:@"%s", managerStrings[central.state]];

  // the search is enabled if everything is ok
  if ([managerString isEqualToString:@"PoweredOn"]) {

      //Do something

  }
}

-1
投票

从Sierra,Xcode 8,Swift 3升级到High Sierra,Xcode 9,Swift 4后,我遇到了同样的问题。

  • 解决方案1 ​​:(仅用于测试)在init和扫描开始之间等待第二位 var scanner = BLESensorScanner(sensorName: sensorName); sleep(1); DispatchQueue.global(qos: .userInteractive).async { if (scanner.isPoweredOn()) { DBG("Bluetooth is powered on"); scanner.startScan(); } else { DBG("Bluetooth is not powered"); exit(-1); } }

注意:BLESensorScanner实现NSObjectCBCentralManagerDelegateCBPeripheralDelegatesensorName是蓝牙设备的名称

  • 解决方案2:使用事件处理程序等待状态变为poweredOn public func centralManagerDidUpdateState(_ central: CBCentralManager) { DBG("CBCentralManager.centralManagerDidUpdateState"); DBGI("state=\(toString(central.state))"); if (state == CBManagerState.poweredOn) { scanner.startScan(); } }
© www.soinside.com 2019 - 2024. All rights reserved.