信标未发现 iOS
Beacon is not discovering iOS
我正在尝试发现 ibeacon。我是 运行 应用程序 iPhone 4s。设备蓝牙已打开。 beacon 也在其他 iOS 应用程序中发现。
当我在代理 "centralManagerDidUpdateState" 被调用并且状态为 "On" 之后搜索服务时,什么也没有发生。预计应该调用 "didDiscoverPeripheral" 但什么也没有发生。我在这里做错了什么?
Hi,
import UIKit
import FBSDKLoginKit
import SwiftQRCode
import CoreBluetooth
@objc
class DashboardViewController: UIViewController {
var myCentralManager:CBCentralManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myCentralManager = CBCentralManager(delegate: self, queue: nil)
// let options = [CBCentralManagerScanOptionAllowDuplicatesKey:true]
myCentralManager.scanForPeripheralsWithServices(nil, options: nil)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.hidden = true
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
// MARK: CBCentralManagerDelegate
extension DashboardViewController: CBCentralManagerDelegate {
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
peripheral.delegate = self
// if peripheral.state == CBPeripheralState.Disconnected {
//
// central.connectPeripheral(peripheral, options: nil)
// }
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices(nil)
}
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
}
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
}
}
// MARK: CBCentralManagerDelegate
extension DashboardViewController: CBPeripheralDelegate {
func centralManagerDidUpdateState(central: CBCentralManager) {
switch central.state{
case CBCentralManagerState.PoweredOn:
print("On.")
break
case CBCentralManagerState.PoweredOff:
print("Off.")
break
case CBCentralManagerState.Resetting:
print("Resetting.")
break
case CBCentralManagerState.Unauthorized:
print("Unauthorized.")
break
case CBCentralManagerState.Unknown:
print("Unknown.")
break
case CBCentralManagerState.Unsupported:
print("Unsupported.")
break
}
}
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
for service in peripheral.services!{
peripheral.discoverCharacteristics(nil, forService: service)
}
}
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
for charactristics in service.characteristics!{
peripheral.setNotifyValue(true, forCharacteristic: charactristics)
}
}
func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
if characteristic.isNotifying {
print("Notifying...")
}
}
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
print(characteristic.value)
}
}
无法使用 CoreBluetooth
API 和 CBCentralManager
class 检测 iBeacon,如代码中所示。虽然 iBeacon 是蓝牙 LE 设备,Apple has security blocks that prevent iOS devices from reading the raw advertisement data 具有 didConnectPeripheral
回调。
如果您想检测 iOS 上的 iBeacon,您必须使用 CoreLocation
API。您可以阅读有关如何执行此操作的更多信息 here.
我正在尝试发现 ibeacon。我是 运行 应用程序 iPhone 4s。设备蓝牙已打开。 beacon 也在其他 iOS 应用程序中发现。 当我在代理 "centralManagerDidUpdateState" 被调用并且状态为 "On" 之后搜索服务时,什么也没有发生。预计应该调用 "didDiscoverPeripheral" 但什么也没有发生。我在这里做错了什么?
Hi,
import UIKit
import FBSDKLoginKit
import SwiftQRCode
import CoreBluetooth
@objc
class DashboardViewController: UIViewController {
var myCentralManager:CBCentralManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myCentralManager = CBCentralManager(delegate: self, queue: nil)
// let options = [CBCentralManagerScanOptionAllowDuplicatesKey:true]
myCentralManager.scanForPeripheralsWithServices(nil, options: nil)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.hidden = true
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
// MARK: CBCentralManagerDelegate
extension DashboardViewController: CBCentralManagerDelegate {
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
peripheral.delegate = self
// if peripheral.state == CBPeripheralState.Disconnected {
//
// central.connectPeripheral(peripheral, options: nil)
// }
}
func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices(nil)
}
func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
}
func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
}
}
// MARK: CBCentralManagerDelegate
extension DashboardViewController: CBPeripheralDelegate {
func centralManagerDidUpdateState(central: CBCentralManager) {
switch central.state{
case CBCentralManagerState.PoweredOn:
print("On.")
break
case CBCentralManagerState.PoweredOff:
print("Off.")
break
case CBCentralManagerState.Resetting:
print("Resetting.")
break
case CBCentralManagerState.Unauthorized:
print("Unauthorized.")
break
case CBCentralManagerState.Unknown:
print("Unknown.")
break
case CBCentralManagerState.Unsupported:
print("Unsupported.")
break
}
}
func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
for service in peripheral.services!{
peripheral.discoverCharacteristics(nil, forService: service)
}
}
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {
for charactristics in service.characteristics!{
peripheral.setNotifyValue(true, forCharacteristic: charactristics)
}
}
func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
if characteristic.isNotifying {
print("Notifying...")
}
}
func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {
print(characteristic.value)
}
}
无法使用 CoreBluetooth
API 和 CBCentralManager
class 检测 iBeacon,如代码中所示。虽然 iBeacon 是蓝牙 LE 设备,Apple has security blocks that prevent iOS devices from reading the raw advertisement data 具有 didConnectPeripheral
回调。
如果您想检测 iOS 上的 iBeacon,您必须使用 CoreLocation
API。您可以阅读有关如何执行此操作的更多信息 here.