在IOS 9.1 / X code 7.1.1蓝牙破 [英] Bluetooth broken in IOS 9.1/Xcode 7.1.1
问题描述
10.11.1环境下使用X code 7.1.1运行IOS 9.1。 Cut'n'pasted从本教程这个code;与其他几个来源/位的双层检查。
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
这是code我有...
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")
}
}
这我在调用视图控制器与此code
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()
}
现在我在iPad Air采用9.1运行它,并插入并拔出并重新插入一个iBeacon显示,但没有出现在控制台上,这表明它根本不工作。现在我知道了iBeacon显示工作正常;导致我与半径网络ScanBeacon工具找到它。
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.
好吧,我明白,ibeacons和Core蓝牙不要一起去这么好,但肯定didDiscoverPeripheral应该叫什么名字?或有我错过了一个临界线在我的code?
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?
推荐答案
您 BLEManager
是走出去的范围,并在年底被释放viewDidLoad中
。使它成为一个成员变量,给它一个更长,更有效寿命:
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
}
这篇关于在IOS 9.1 / X code 7.1.1蓝牙破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!