我正在开发一个 native 应用程序以使用苹果的 iBeacon 框架并尝试将我附近的所有蓝牙设备的列表记录到控制台。我一直在关注this tutorial但是当我运行应用程序时,控制台上没有打印任何内容。我的错误在哪里?
下面是我编写的 View Controller 文件的完整细节:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: NSUUID(uuidString: "852c0828-fe67-4dd7-b8ff-52852a66851e")! as UUID, major: 8008, minor: 1337, identifier: "Testing")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.authorizedWhenInUse) {
locationManager.requestWhenInUseAuthorization()
}
locationManager.startRangingBeacons(in: region)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
print(beacons)
print("hello")
}
}
运行时没有任何内容打印到控制台,包括“hello”。
最佳答案
代码看起来不错。您确定您知道您的信标标识符并且它正在传输吗?
我会尝试使用现成的信标检测器应用程序,例如 Locate验证它可以看到你的信标。请注意,您必须在应用程序中配置您的信标 UUID 以进行检测。
其他一些提示:
确保您的 Info.plist 包含这样的条目,否则它将无法提示您进行位置访问:
<key>NSLocationWhenInUseUsageDescription</key><string>This app needs to access your location so it can tell when you are near a beacon.</string>
确保系统提示您访问位置信息并且您已实际授予它。检查设置 -> [您的应用程序名称] -> 位置,并确认它显示“允许位置访问”有一个不在从不旁边的复选标记。
确保蓝牙已开启。
关于ios - 显示来自信标的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41785375/