简体   繁体   中英

Bluetooth broken in IOS 9.1/Xcode 7.1.1

Running IOS 9.1 with Xcode 7.1.1 under 10.11.1. Cut'n'pasted this code from this tutorial; and double check it with several other sources/sites.

http://hatemfaheem.blogspot.ch/2014/12/how-would-you-scan-for-nearby-ble.html

This is the code I have ...

import Foundation
import CoreBluetooth

class BLEManager  {
var centralManager : CBCentralManager!
var bleHandler : BLEHandler // delegate
init() {
    self.bleHandler = BLEHandler()
    self.centralManager = CBCentralManager(delegate: self.bleHandler, queue: nil)
}
}

class BLEHandler : NSObject, CBCentralManagerDelegate {
override init() {
    super.init()
}

func centralManagerDidUpdateState(central: CBCentralManager) {
    switch (central.state)
    {
    case .Unsupported:
        print("BLE is unsupported")
    case .Unauthorized:
        print("BLE is unauthorized")
    case .Unknown:
        print("BLE is unknown")
    case .Resetting:
        print("BLE is reseting")
    case .PoweredOff:
        print("BLE is powered off")
    case .PoweredOn:
        print("BLE is powered on")
        central.scanForPeripheralsWithServices(nil, options: nil)
    default:
        print("BLE default")
    }
}

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
    print("didConnectPeripheral")
}

func centralManager(central: CBCentralManager!,
    didDiscoverPeripheral peripheral: CBPeripheral!,
    adverismentData: [NSObject : AnyObject]!,
    RSSI: NSNumber!)
{
    print("\(peripheral.name) : \(RSSI) dBm")
}
}

Which I invoke in the View Controller with this code

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var bleManager: BLEManager!
    bleManager = BLEManager()
}

Now I run it on an iPad Air with 9.1 and plug in and unplug and replug an ibeacon but nothing appears on the console, suggesting it simply isn't working. Now I know the ibeacon is working; cause I find it with the ScanBeacon tool by Radius Networks.

OK I understand that ibeacons and Core Bluetooth don't go together so well, but surely didDiscoverPeripheral should be called ? Or have I missed a critical line in my code?

Your BLEManager is going out of scope and being deallocated at the end of viewDidLoad . Make it a member variable to give it a longer, more useful lifetime:

var bleManager = BLEManager()

override func viewDidLoad() {
    // etc
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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