【发布时间】:2020-07-04 12:45:27
【问题描述】:
我正在构建一个应用程序,该应用程序使用 iPhone 6 中的定位信标应用程序和 iPhone 7 中的我的应用程序检测信标设备接近度,但应用程序无法检测到信标发射器。我输入了正确的 UUID 和主要次要编号,但它不起作用。我正在运行 iOS 13.5 的 iPhone 7 和未检测到 iOS 12 的 iPhone 6。
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var distancestatuslbl: UILabel!
var locationmanager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationmanager = CLLocationManager()
locationmanager.delegate = self
locationmanager.requestAlwaysAuthorization()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedAlways
{
print("Allowed any time")
startScanning()
}
else if status == .authorizedWhenInUse
{
print("Allowed only during run time")
startScanning()
}
}
func startScanning() {
if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self)
{
let uuid = UUID(uuidString: "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6")
let majornumber: CLBeaconMajorValue = 123
let minornumber: CLBeaconMinorValue = 456
let beaconregion = CLBeaconRegion(uuid: uuid!, major: majornumber, minor: minornumber, identifier: "com.mybeacon")
locationmanager.startMonitoring(for: beaconregion)
print("Started scanning")
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if region is CLBeaconRegion
{
if CLLocationManager.isRangingAvailable()
{
locationmanager.startRangingBeacons(in: region as! CLBeaconRegion)
print("Starting ranging...")
}
}
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
let nearestbeacon = beacons.first!
switch nearestbeacon.proximity {
case .near:
distancestatuslbl.text = "Near"
break
case .immediate:
distancestatuslbl.text = "Immediate"
break
case .far:
distancestatuslbl.text = "Far"
break
default:
print("sdf")
}
}
}
【问题讨论】:
-
运行代码时,会打印哪些调试行?
-
随时允许并开始扫描
-
Didenterregion() 未被调用
标签: ios swift core-location ibeacon