iOS 8核心蓝牙无法发现外围设备

14

我在iOS 8上使用Core Bluetooth发现外设时遇到了问题。相同的代码在iOS 7设备上运行良好。起初我以为这可能是权限问题,因为我一直在进行iBeacon开发,并且在iOS 8中Core Location权限发生了一些变化。但是,我在网上找不到任何有用的信息。以下是一个示例项目的链接,在iOS 7上可以很好地运行,但在iOS 8上无法工作:

https://github.com/elgreco84/PeripheralScanning

如果我在iOS 7设备上运行此项目,它将记录周围多个设备的广告数据。在iOS 8上,我只能看到Central Manager状态为“已启动”。


2个回答

33

在进入“已上电”状态之前,开始扫描外围设备是无效的。 也许在您的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:
        {
        }

    }
}

didFinishLaunchingWithOptions中删除对scanForPeripheralsWithServices的调用。


2
非常感谢。你救了我一命,让我不再想把我的人力资源管理软件扔出窗外。 - bmeulmeester
我确实这样做了,但有时候,并不是每次都会调用didDiscoverPeripheral方法,真的很困惑... - Zennichimaro
但对于我来说,在iOS 8中它调用didDiscoverPeripheral方法非常晚,如何修复? - Anilkumar iOS - ReactNative

4

我在构建一个非常基础的BLE扫描器应用程序时遇到了同样的问题。所需的方法“centralManagerDidUpdateState”已添加,但什么也没有发生。

我认为问题与队列有关。将实例放入。

这段代码片段就是这样做的:

       // 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")
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接