【问题标题】:iOS - check if bluetooth is on without system alert popup to useriOS - 检查蓝牙是否打开而没有向用户弹出系统警报
【发布时间】:2012-09-14 00:54:40
【问题描述】:

此代码允许确定当前的蓝牙状态:

CBCentralManager* testBluetooth = [[CBCentralManager alloc] initWithDelegate:nil queue: nil];


switch ([testBluetooth state]) {....}

但是,当 [[CBCentralManager alloc] init...] 发生时,如果蓝牙关闭,系统会向用户弹出警报。

有什么方法可以在不打扰我的用户的情况下检查蓝牙状态?

【问题讨论】:

    标签: ios bluetooth alert status


    【解决方案1】:

    我从一位苹果开发者那里得到以下回复:在 iOS7 中,CBCentralManagerOptionShowPowerAlertKey 选项可让您禁用此警报。

    如果你初始化的时候有一个CBCentralManager,可以使用initWithDelegate:queue:options方法

    例子:

    在我的 .h 文件中,我有一个 CBCentralManager * manager

    在 .m 文件中:

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerOptionShowPowerAlertKey, nil];
    
    _manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
    
    [_manager scanForPeripheralsWithServices:nil options:nil];
    

    使用此代码,警告不再出现,希望对您有所帮助!

    【讨论】:

    • 周边扫描的选项有:CBCentralManagerScanOptionAllowDuplicatesKey; CBCentralManagerScanOptionSolicitedServiceUUIDsKey; link 因此,发送您要发送的选项字典不会对 scanForPeripheralWithServices 产生影响。只需要在初始化中就可以了
    • @shim 你说得对,我不应该将选项添加到 scanForPeripheralsWithServices
    【解决方案2】:

    在 swift 中,您可以在 func 内的应用程序委托中编写这两行:didFinishLaunchingWithOptions launchOptions

        self.bCentralManger = CBCentralManager(delegate: self, queue: dispatch_get_main_queue(), options: [CBCentralManagerOptionShowPowerAlertKey: false])
        self.bCentralManger.scanForPeripheralsWithServices(nil, options: nil)
    

    您的 bCentralManger 应声明为:

    私有变量 bCentralManger: CBCentralManager!

    【讨论】:

      【解决方案3】:

      我已使用以下代码禁用 iOS 8 及更高版本

      的警报
      self.bluetoothManager = [[CBCentralManager alloc]
                                            initWithDelegate:self 
                                            queue:dispatch_get_main_queue() 
                                            options:@{CBCentralManagerOptionShowPowerAlertKey: @(NO)}];
      
      [self.bluetoothManager scanForPeripheralsWithServices:nil options:nil];
      

      【讨论】:

        【解决方案4】:

        通过结合BadPirate'sAnas'的答案,你可以获得蓝牙状态而不显示系统警报。

        #import <CoreBluetooth/CoreBluetooth.h>
        
        @interface ShopVC () <CBCentralManagerDelegate>
        
        @property (nonatomic, strong) CBCentralManager *bluetoothManager;
        
        @end
        
        @implementation ShopVC
        
        - (void)viewDidLoad {
            [super viewDidLoad];
        
            if(!self.bluetoothManager)
            {
                NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey: @NO};
                self.bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
            }
        }
        
        #pragma mark - CBCentralManagerDelegate
        
        - (void)centralManagerDidUpdateState:(CBCentralManager *)central
        {
            NSString *stateString = nil;
            switch(self.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:@"ok" otherButtonTitles: nil];
            [alert show];
        }
        

        【讨论】:

          【解决方案5】:

          我只在 iOS 9 上测试过这个,所以也许有人可以测试这个旧操作系统设备。

          除了一件事之外,我们通常做所有事情,而不是在viewDidLoad 中设置CBCentralManager 代表,我们将其保留到我们需要它的那一刻,在下面的示例中,一旦我的WKWebView 完成加载,我就会调用它,因为我的网页视图的每个页面都可能需要使用蓝牙,所以我把它放在WKWebView didFinishNavigation 中。

          斯威夫特

          var managerBLE: CBCentralManager?
          
          func bluetoothStatus() {
              managerBLE = CBCentralManager(delegate: self, queue: nil, options: nil)
          }
          
          func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
              bluetoothStatus()
          }
          
          func centralManagerDidUpdateState(central: CBCentralManager) {
              switch managerBLE!.state
              {
              case CBCentralManagerState.PoweredOff:
                  print("Powered Off")
              case CBCentralManagerState.PoweredOn:
                  print("Powered On")
              case CBCentralManagerState.Unsupported:
                  print("Unsupported")
              case CBCentralManagerState.Resetting:
                  print("Resetting")
                  fallthrough
              case CBCentralManagerState.Unauthorized:
                  print("Unauthorized")
              case CBCentralManagerState.Unknown:
                  print("Unknown")
              default:
                  break;
              }
          }
          

          bluetoothStatus() 中设置委托的那一刻,您将看到状态更改触发。

          开启蓝牙的通知似乎只想在您的应用程序初始加载时被调用,这样做意味着您只需从centralManagerDidUpdateState 获得您想要的东西

          【讨论】:

          • 另外我知道这种方法可能会影响能源消耗,因为我们正在设置和重置委托,所以在我们每次调用功能?
          【解决方案6】:

          当您的应用程序在支持蓝牙 LE 且蓝牙被禁用的 iOS 设备上运行时,目前无法禁用此警报。提供一种禁用警报的方法将是一个增强请求。因此,Apple 收到的有关此增强功能的请求越多越好。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-05-01
            • 2018-09-23
            • 1970-01-01
            • 2012-07-17
            • 1970-01-01
            • 2020-04-27
            • 1970-01-01
            相关资源
            最近更新 更多