简体   繁体   English

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

[英]iOS 8 Core Bluetooth not discovering peripherals

I'm having trouble getting Core Bluetooth to discover peripherals on iOS 8. Same code works fine on iOS 7 device. 我无法让Core Bluetooth在iOS 8上发现外设。相同的代码在iOS 7设备上运行良好。 Initially I thought it would be a permissions issue since I had been doing some iBeacon work and there are some changes in Core Location permissions on iOS 8. I couldn't find anything online that helped with that however. 最初我认为这将是一个权限问题,因为我一直在做一些iBeacon工作,并且在iOS 8上的核心位置权限有一些变化。但我在网上找不到任何帮助。 Here is a link to a sample project that works fine for me on iOS 7 but not on iOS 8: 这是一个示例项目的链接,适用于iOS 7但不适用于iOS 8:

https://github.com/elgreco84/PeripheralScanning https://github.com/elgreco84/PeripheralScanning

If I run this project on an iOS 7 device it will log advertisement data for a number of devices around me. 如果我在iOS 7设备上运行此项目,它将记录我周围的许多设备的广告数据。 On iOS 8 the only output I see is that the Central Manager state is "Powered On". 在iOS 8上,我看到的唯一输出是Central Manager状态为“Powered On”。

It isn't valid to start scanning for peripherals until you are in the 'powered on' state. 在处于“开机”状态之前,开始扫描外围设备是无效的。 Perhaps on your iOS7 device you are lucky with timing, but the code is still incorrect. 也许在你的iOS7设备上你很幸运,但是代码仍然不正确。 Your centralManagerDidUpdateState: should be 你的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:
        {
        }

    }
}

And remove the call to scanForPeripheralsWithServices from didFinishLaunchingWithOptions 并从didFinishLaunchingWithOptions删除对scanForPeripheralsWithServices的调用

I ran into the same issue while building a very basic BLE scanner app. 我在构建一个非常基本的BLE扫描仪应用程序时遇到了同样的问题。 The required method "centralManagerDidUpdateState" was added. 添加了所需的方法“centralManagerDidUpdateState”。 But nothing worked. 但没有任何效果。

I believe the problem is related to queue . 我相信问题与queue有关。 Put the CBCentralManager instance in a dispatch_get_main_queue CBCentralManager实例放在dispatch_get_main_queue

This code snippet does that: 这段代码片段可以做到:

       // 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())   

          }

Using the Default Single View xCode start app. 使用默认单一视图xCode启动应用程序。 You can put this into the ViewController.swift file: 您可以将其放入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")
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM