我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.
}
}
我得给我的macbook做广告。一旦我使用https://github.com/mttrb/BeaconOSX这样做了,它就完全像我写的那样工作了。