CoreBluetooth didDiscoverPeripheral不会被调用斯威夫特 [英] CoreBluetooth didDiscoverPeripheral not being called in Swift
问题描述
我99%肯定我 CoreBluetooth
遵循正确设置的说明。无论我做什么,当我在我的iPad迷你运行这个程序,蓝牙是说其上。它说它正在扫描设备,但它是绝对没有找到任何设备。如果我去设备上的蓝牙菜单我看到被发现的其他设备。我初始化 CBCentralManager
。我设置 centralManagerDidUpdateState
。当那是肯定的蓝牙准备调用 centralManager.scanForPeripheralsWithServices
。这一切都被正确发生。但我的委托函数 centralManager(中央:CBCentralManager!didDiscoverPeripheral外设:CBPeripheral!advertisementData:[NSObject的:AnyObject]!RSSI:NSNumber的)
永远不会被调用。我的code是非常简单的。也许我失去了一些东西,但我可以证实,我的MacBook是BTLE设备和我的iPad mini是一款BTLE设备也是如此。这里是我的code。
进口的UIKit
进口CoreBluetooth一流的ViewController:UIViewController的,CBCentralManagerDelegate { VAR centralManager:CBCentralManager!
VAR blueToothReady = FALSE 覆盖FUNC viewDidLoad中(){
super.viewDidLoad()
startUpCentralManager()
} FUNC startUpCentralManager(){
的println(初始化中央管理器)
centralManager = CBCentralManager(代表:自我,队列:无)
} FUNC discoverDevices(){
的println(发现设备)
centralManager.scanForPeripheralsWithServices(无,选择:无)
} FUNC centralManager(中央:CBCentralManager!didDiscoverPeripheral外设:CBPeripheral!advertisementData:[NSObject的:AnyObject]!RSSI:NSNumber的){
的println(发现\\(peripheral.name))
} FUNC centralManagerDidUpdateState(中央:CBCentralManager){
的println(检查状态)
开关(central.state){
案例.PoweredOff:
的println(CoreBluetooth BLE硬件关机) 案例.PoweredOn:
的println(CoreBluetooth BLE硬件上电,并准备)
blueToothReady = TRUE; 案例.Resetting:
的println(CoreBluetooth BLE硬件复位) 案例.Unauthorized:
的println(CoreBluetooth BLE状态是未经授权) 案例.Unknown:
的println(CoreBluetooth BLE状态未知); 案例.Unsupported:
的println(CoreBluetooth BLE硬件是此平台上不支持); }
如果blueToothReady {
discoverDevices()
}
} 覆盖FUNC didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
可以重新创建任何资源的处置//。
}}
我必须得到我的MacBook广告。有一次,我这样做,使用 https://github.com/mttrb/BeaconOSX 它的工作完全按照我写的它。
I am 99% sure I followed the instructions to setup CoreBluetooth
correctly. No matter what I do, when I run this app on my iPad mini, the Bluetooth is saying its on. It's saying it is scanning for devices, but it is absolutely not finding any devices. If I go to the Bluetooth menu on the device I do see other devices being discovered. I initialize the CBCentralManager
. I setup centralManagerDidUpdateState
. When that is sure the bluetooth is ready it calls centralManager.scanForPeripheralsWithServices
. All this is happening correctly. But my delegate function centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!)
never ever gets called. My code is very simple. Maybe I am missing something but I was able to confirm that my Macbook is a BTLE device and my ipad mini is a BTLE device as well. Here is my code.
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.
}
}
I had to get my macbook advertising. Once I did that using https://github.com/mttrb/BeaconOSX it worked exactly as I had written it.
这篇关于CoreBluetooth didDiscoverPeripheral不会被调用斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!