iOS 8核心蓝牙没有发现外围设备

时间:2014-08-30 20:35:36

标签: ios iphone bluetooth core-bluetooth

我无法让Core Bluetooth在iOS 8上发现外围设备。相同的代码在iOS 7设备上运行良好。最初我认为这将是一个权限问题,因为我一直在做一些iBeacon工作,并且iOS 8上的核心位置权限有一些变化。但我无法在网上找到任何有助于此的内容。这是一个示例项目的链接,适用于iOS 7但不适用于iOS 8:

https://github.com/elgreco84/PeripheralScanning

如果我在iOS 7设备上运行此项目,它将记录我周围的许多设备的广告数据。在iOS 8上,我看到的唯一输出是中央管理器状态是"通电"。

2 个答案:

答案 0 :(得分:32)

开始扫描外围设备是不合法的,直到您打开电源为止'州。也许在你的iOS7设备上你很幸运,但是代码仍然不正确。您的centralManagerDidUpdateState:应为

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    switch (central.state)
    {
        case CBCentralManagerStateUnsupported:
        {
            NSLog(@"State: Unsupported");
        } break;

        case CBCentralManagerStateUnauthorized:
        {
            NSLog(@"State: Unauthorized");
        } break;

        case CBCentralManagerStatePoweredOff:
        {
            NSLog(@"State: Powered Off");
        } break;

        case CBCentralManagerStatePoweredOn:
        {
            NSLog(@"State: Powered On");
            [self.manager scanForPeripheralsWithServices:nil options:nil];
        } break;

        case CBCentralManagerStateUnknown:
        {
            NSLog(@"State: Unknown");
        } break;

        default:
        {
        }

    }
}

scanForPeripheralsWithServices

中移除对didFinishLaunchingWithOptions的来电

答案 1 :(得分:4)

我在构建一个非常基本的BLE扫描仪应用程序时遇到了同样的问题。所需方法" centralManagerDidUpdateState"加入。但没有任何效果。

我认为问题与queue有关。 将CBCentralManager实例放在dispatch_get_main_queue

此代码段执行此操作:

       // BLE Stuff
            let myCentralManager = CBCentralManager()

        // Put CentralManager in the main queue
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            myCentralManager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())   

          }

使用默认单一视图xCode启动应用。您可以将其放入ViewController.swift文件中:

   import UIKit
   import CoreBluetooth    

   class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {


        // BLE Stuff
        let myCentralManager = CBCentralManager()
        var peripheralArray = [CBPeripheral]() // create now empty array.


        // Put CentralManager in the main queue
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            myCentralManager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())

        }



        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.

        }


        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

        // Mark   CBCentralManager Methods

        func centralManagerDidUpdateState(central: CBCentralManager!) {

            updateStatusLabel("centralManagerDidUpdateState")


            switch central.state{
            case .PoweredOn:
                updateStatusLabel("poweredOn")


            case .PoweredOff:
                updateStatusLabel("Central State PoweredOFF")

            case .Resetting:
                updateStatusLabel("Central State Resetting")

            case .Unauthorized:
                updateStatusLabel("Central State Unauthorized")

            case .Unknown:
                updateStatusLabel("Central State Unknown")

            case .Unsupported:
                println("Central State Unsupported")

            default:
                println("Central State None Of The Above")

            }

        }

        func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
                  println("Did Discover Peripheral")
            }
        }