【问题标题】:Any way to turn on bluetooth programmatically on iOS7+在 iOS7+ 上以编程方式打开蓝牙的任何方式
【发布时间】:2014-05-23 16:01:48
【问题描述】:

我听说 iOS7 使用 CBCentralManager 引入了此功能,但找不到具体方法。有可能吗? widthout 使用 GKPeerPickerController 还有另一种方式吗?

【问题讨论】:

    标签: ios


    【解决方案1】:

    您还可以在将CBCentralManager 实例化为true 时设置CBCentralManagerOptionShowPowerAlertKey 键。然后 iOS 会显示“打开蓝牙以允许“您的应用”连接到附件”的警报。此警报会将您直接带到蓝牙设置页面。

    快速输入示例代码:

    centralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey: true])
    

    Objective-C 中的示例代码:

    centralManager = [[CBCentralManager alloc]
                                          initWithDelegate:self 
                                          queue:dispatch_get_main_queue() 
                                          options:@{CBCentralManagerOptionShowPowerAlertKey: @(YES)}];
    

    编码愉快.. :)

    【讨论】:

      【解决方案2】:

      不,如果用户关闭了蓝牙,您所能做的就是显示一条警报或消息,要求他们打开它。

      - (void)centralManagerDidUpdateState:(CBCentralManager *)central {
      
          if (central.state == CBCentralManagerStatePoweredOff) {
              UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Error" message: @"Please turn on Bluetooth in Settings" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
             [alert show]; 
          }
       }
      

      【讨论】:

      • 只需检查 CBCentralManager state developer.apple.com/Library/ios/documentation/CoreBluetooth/… 的“关机”状态并显示警报
      • 我已经完成了,但我不明白你能提供我的示例代码
      • 谢谢 Paulw11。为你+1。答案是正确的。
      • 是否有任何代码可以从警报按钮打开 iphone 设置
      • 自从非官方的 URL 方案在 iOS 5 中被删除后没有
      【解决方案3】:

      使用蓝牙管理器框架,

      导入蓝牙管理器框架,创建蓝牙管理器框架的对象,作为btManager,

      在蓝牙开启按钮目标中编写以下代码

      [btManager setPowered:YES];
      [btManager setEnabled:YES];
      

      一切顺利……

      【讨论】:

      • 这是一个私有 API,会让您的应用在 App Store 上被拒绝。
      • 我们如何在 swift 中使用它?
      猜你喜欢
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 2012-12-04
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多