一个简单的代码,用于检测swift中的任何信标

我是快速编程的新手,我正在尝试开发app检测信标,但我仍然无法检测到任何信标。

let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "11111111-1111-1111-1111-111111111111")!,major: 1, minor: 1, identifier: "MyBeacon") locationManager.startMonitoringForRegion(region) locationManager.startRangingBeaconsInRegion(region) 

SWIFT 3:

首先,您应该添加CoreLocation.Framework

  • 在.Plist文件中,使用适当的字符串添加键/字符串NSLocationAlwaysUsageDescription

  • 在您的Object中添加CLLocationManagerDelegate

  • 在此示例中添加CLLocationManagerDelegate委托方法我将添加didRangeBeacons方法

 func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) { print(beacons) } 
  • 创建并初始化locationManager

    var locationManager : CLLocationManager = CLLocationManager()

  • 创建CLBeaconRegion

     let beaconRegion : CLBeaconRegion = CLBeaconRegion( proximityUUID: NSUUID.init(uuidString:"****-****-****-****-******") as! UUID, identifier: "my beacon") 
  • 将委托添加到对象locationManager.delegate = self

  • 请求用户的位置授权

locationManager.requestAlwaysAuthorization()

现在让我们开始范围

  locationManager.startRangingBeacons(in: beaconRegion) 

这将自动调用委托方法

  func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) { print(beacons) } 

注意:如果要监视beacon的进入/退出状态,则需要添加

 locationManager.startMonitoring(for: beaconRegion) 

最后:确保您的信标已打开并且您正在测试iBeacon Frame Enjoy:D

可以在前景和背景中检测到信标

步骤: – 1。info.plist中的更改

在info.plist中,您需要更改“使用说明”。 为此,添加带有您的消息的字符串类型的NSLocationAlwaysUsageDescription

步骤: – 2用于背景模式

在App Delegate中添加以下代码:

 var window: UIWindow? var locationManager:CLLocationManager = CLLocationManager() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. locationManager.delegate = self locationManager.requestAlwaysAuthorization() // Request permission to send notifications let center = UNUserNotificationCenter.current() center.requestAuthorization(options:[.alert, .sound]) { (granted, error) in } return true } func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { rangeBeacons() } func rangeBeacons(){ let uuid = UUID(uuidString: "3e1d7817-4eac-4b27-b809-deee2f246c46") //let uuid = UUID(uuidString: "8492E75F-4FD6-469D-B132-043FE94921D8") let major:CLBeaconMajorValue = 1 let minor:CLBeaconMinorValue = 2 let identifier = "myBeacon" let region = CLBeaconRegion(proximityUUID: uuid!, major: major, minor: minor, identifier: identifier) region.notifyOnEntry = true region.notifyEntryStateOnDisplay = true region.notifyOnExit = true locationManager.startRangingBeacons(in: region) locationManager.startMonitoring(for: region) } func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { let content = UNMutableNotificationContent() content.title = "Hello!!!" content.body = "You Are Back in the Office" content.sound = .default() let request = UNNotificationRequest(identifier: "SufalamTech", content: content, trigger: nil) UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) } func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { let content = UNMutableNotificationContent() content.title = "Alert!!!" content.body = "You are Out of the Office" content.sound = .default() let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: nil) UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) } 

另外,不要忘记添加CLLocationManagerDelegate并导入CoreLocationsUserNotifications

步骤: – 3用于ForeGround或活动模式

在ViewController中添加以下代码

 func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) { guard let discoveredbeaconProximity = beacons.first?.proximity else {print("Beacons Cannot be located"); return } if ((discoveredbeaconProximity == .far) || (discoveredbeaconProximity == .near) || (discoveredbeaconProximity == .immediate)) { let alert = UIAlertController(title: "Alert", message: "You are in the Beacon Region", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) self.window?.rootViewController?.present(alert, animated: true, completion: nil) } if discoveredbeaconProximity == .unknown{ let alert = UIAlertController(title: "Alert", message: "You are out of the Beacon Region!!!", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) self.window?.rootViewController?.present(alert, animated: true, completion: nil) } } 

注意: – 您需要相应地更改Beacon UUID and Major and Minor

简单示例:

NSLocationAlwaysUsageDescription添加到*.plist文件中。

在ViewController中创建locationManager和beaconRegion。 设置locationManager委托,请求用户授权,然后开始监视/测距。

 let locationManager : CLLocationManager = CLLocationManager() let beaconRegion : CLBeaconRegion = CLBeaconRegion( proximityUUID: NSUUID.init(uuidString:"11111111-1111-1111-1111-111111111111") as! UUID, major: 1, minor: 1, identifier: "my beacon") locationManager.delegate = self locationManager.requestAlwaysAuthorization() locationManager.startMonitoring(for: beaconRegion) 

添加CLLocationManagerDelegate的扩展:

 extension ViewController : CLLocationManagerDelegate { func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) { print(beacons) } func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) { let beaconRegion = region as! CLBeaconRegion print("Did enter region: " + (beaconRegion.major?.stringValue)!) } func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) { let beaconRegion = region as! CLBeaconRegion print("Did exit region: " + (beaconRegion.major?.stringValue)!) } } 

注意:激活蓝牙,检查您的设备上是否激活了位置服务(不支持模拟器),使用此区域,您只能找到具有此UUID + major + minor的信标。