Ios 未能使用Swift发布和发现BLE
我正试图用以下内容为BLE做广告Ios 未能使用Swift发布和发现BLE,ios,swift,bluetooth-lowenergy,cbcentralmanager,cbperipheral,Ios,Swift,Bluetooth Lowenergy,Cbcentralmanager,Cbperipheral,我正试图用以下内容为BLE做广告 var perMan: CBPeripheralManager! let myCustomServiceUUID: CBUUID = CBUUID(string: "109F17E4-EF68-43FC-957D-502BB0EFCF46") var myService: CBMutableService! override func viewDidLoad() { super.viewDidLoad() perMan = CBPeripher
var perMan: CBPeripheralManager!
let myCustomServiceUUID: CBUUID = CBUUID(string: "109F17E4-EF68-43FC-957D-502BB0EFCF46")
var myService: CBMutableService!
override func viewDidLoad() {
super.viewDidLoad()
perMan = CBPeripheralManager(delegate: self, queue: nil)
myService = CBMutableService(type: myCustomServiceUUID, primary: true)
perMan.addService(myService)
}
// Broadcast when Bluetooth is on
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager!) {
if peripheral.state == CBPeripheralManagerState.PoweredOn {
// Start advertising over BLE
self.perMan.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [myService.UUID]])
println("Adertising")
} else if peripheral.state == CBPeripheralManagerState.PoweredOff {
self.perMan.stopAdvertising()
}
}
并且想用它来发现外设
import UIKit
import CoreBluetooth
class SearchViewController: UIViewController, CBCentralManagerDelegate {
var centralManager: CBCentralManager!
@IBOutlet weak var beaconsTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
// Listen to BLE of IPhones
let services: NSArray = ["7521105F-8937-48B7-A875-66E6FE21D714"]
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}
// Found IPhone
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
println("Hallo:")
println(RSSI)
}
// CBCentralManagerDelegate
func centralManagerDidUpdateState(central: CBCentralManager!) {
}
但是,从未调用DidDiscoveryPeripheral方法。我正在测试两部IPhone5。
有人能告诉我我做错了什么吗?使用应用程序LightBlue(无关联)收听您的BTLE广播。如果LightBlue找到您的服务,您知道问题在于检测;如果没有,您就知道您没有完全/正确地设置广播。在扫描之前,您不是在等待CBcentral处于开机状态,而是需要类似于外围设备中的代码,以便在开机后开始扫描