在 10.11.1 下使用 Xcode 7.1.1 运行 IOS 9.1。从本教程中剪切并粘贴了此代码;并与其他几个来源/网站仔细检查。
http://hatemfaheem.blogspot.ch/2014/12/how-would-you-scan-for-nearby-ble.html
这是我的代码...
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")
}
}
我使用此代码在 View Controller 中调用它
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var bleManager: BLEManager!
bleManager = BLEManager()
}
现在我在 9.1 版本的 iPad Air 上运行它,然后插入、拔出并重新插入 ibeacon,但控制台上没有任何显示,表明它根本无法工作。现在我知道 ibeacon 正在工作;因为我用 Radius Networks 的 ScanBeacon 工具找到了它。
好的,我知道 ibeacons 和 Core Bluetooth 不能很好地结合在一起,但是确实应该调用 didDiscoverPeripheral 吗?或者我错过了代码中的关键行?
最佳答案
您的 BLEManager
超出范围并在 viewDidLoad
结束时被释放。让它成为一个成员变量,以赋予它更长、更有用的生命周期:
var bleManager = BLEManager()
override func viewDidLoad() {
// etc
}
关于ios - IOS 9.1/Xcode 7.1.1 中的蓝牙损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34122099/