我99%确定我按照说明正确设置了CoreBluetooth
。无论我做什么,当我在我的iPad mini上运行这个应用程序时,蓝牙正在说它。它说它正在扫描设备,但绝对没有找到任何设备。如果我转到设备上的蓝牙菜单,我会看到其他设备被发现。我初始化CBCentralManager
。我设置了centralManagerDidUpdateState
。当确定蓝牙准备就绪时,它会调用centralManager.scanForPeripheralsWithServices
。所有这一切都正确发生。但我的委托函数centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!)
永远不会被调用。我的代码非常简单。也许我错过了一些东西,但我能够确认我的Macbook是BTLE设备而我的ipad mini也是BTLE设备。这是我的代码。
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBCentralManagerDelegate {
var centralManager:CBCentralManager!
var blueToothReady = false
override func viewDidLoad() {
super.viewDidLoad()
startUpCentralManager()
}
func startUpCentralManager() {
println("Initializing central manager")
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func discoverDevices() {
println("discovering devices")
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
println("Discovered \(peripheral.name)")
}
func centralManagerDidUpdateState(central: CBCentralManager!) {
println("checking state")
switch (central.state) {
case .PoweredOff:
println("CoreBluetooth BLE hardware is powered off")
case .PoweredOn:
println("CoreBluetooth BLE hardware is powered on and ready")
blueToothReady = true;
case .Resetting:
println("CoreBluetooth BLE hardware is resetting")
case .Unauthorized:
println("CoreBluetooth BLE state is unauthorized")
case .Unknown:
println("CoreBluetooth BLE state is unknown");
case .Unsupported:
println("CoreBluetooth BLE hardware is unsupported on this platform");
}
if blueToothReady {
discoverDevices()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}