[英]Bluetooth broken in IOS 9.1/Xcode 7.1.1
Running IOS 9.1 with Xcode 7.1.1 under 10.11.1. 在10.11.1下使用Xcode 7.1.1运行IOS 9.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 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 我用以下代码在View Controller中调用的
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. 现在,我在装有9.1的iPad Air上运行它,插入并拔出并重新插入ibeacon,但控制台上没有任何显示,这表明它根本无法正常工作。 Now I know the ibeacon is working; 现在我知道ibeacon正在运行; cause I find it with the ScanBeacon tool by Radius Networks. 因为我使用Radius Networks的ScanBeacon工具找到了它。
OK I understand that ibeacons and Core Bluetooth don't go together so well, but surely didDiscoverPeripheral should be called ? 好吧,我知道ibeacons和Core Bluetooth不能很好地结合在一起,但是可以确定应该调用DiscoverPeripheral吗? 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
. 您的BLEManager
超出范围,并在viewDidLoad
的末尾释放。 Make it a member variable to give it a longer, more useful lifetime: 使其成为成员变量,以使其具有更长,更有用的寿命:
var bleManager = BLEManager()
override func viewDidLoad() {
// etc
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.