[英]Bluetooth broken in IOS 9.1/Xcode 7.1.1
在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不能很好地結合在一起,但是可以確定應該調用DiscoverPeripheral嗎? 還是我錯過了代碼中的關鍵行?
您的BLEManager
超出范圍,並在viewDidLoad
的末尾釋放。 使其成為成員變量,以使其具有更長,更有用的壽命:
var bleManager = BLEManager()
override func viewDidLoad() {
// etc
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.