2016-10-11 5 views
1

私は迅速なプログラミングに新しいです、私はビーコンを検出するアプリケーションを開発しようとしていますが、まだビーコンは検出できません。スピーディーなビーコンを検出するシンプルなコード

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

ビーコンごビーコンUUID and Major and Minor値 変更する必要がありますデバイスですか? iBeaconまたはEstimote? –

+0

iBeacon @Sharpkits –

+0

もっとコードを表示できますか? 'locationManager.notifier = ...'と 'didRangeBeacons'コールバックメソッドの定義を設定する部分です。あなたがまだしていない場合、これらを持っている必要があります。 – davidgyoung

答えて

3

SWIFT 3:

すべての

まず、あなたは適切な文字列

でキー/文字列 NSLocationAlwaysUsageDescriptionを追加するの.plistファイルで CoreLocation.Framework

  • を追加する必要があります

  • あなたのオブジェクトには、CLLocationManagerDelegate

  • はlocationManager

    var locationManager : CLLocationManager = CLLocationManager()

  • 0を私はちょうど

func locationManager(_ manager: CLLocationManager, didRangeBeacons 
beacons: [CLBeacon], in region: CLBeaconRegion) { 
      print(beacons) } 
  • が作成didRangeBeaconsメソッドを追加します。この例ではCLLocationManagerDelegateデリゲートメソッドを追加し、初期化

  • CLBeaconRegion

    ​​
  • があなたのオブジェクトlocationManager.delegate = self

  • 要求

locationManagerを持つユーザーからの場所の許可にデリゲートを追加作成します。requestAlwaysAuthorization()

今、これは自動的に

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

    } 

デリゲートメソッドを呼び出しますのは、

locationManager.startRangingBeacons(in: beaconRegion) 

範囲

を始めましょう注:あなたは、ビーコンの入口/出口の状態を監視する場合、あなたがする必要がありますadd

locationManager.startMonitoring(for: beaconRegion) 

Fi情報中1つの変更点 - :ナリー:D

2

簡単な例:

あなた*.plistファイルにNSLocationAlwaysUsageDescriptionを追加します。

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 +メジャー+マイナー。

+0

努力のおかげで@ Rags93私はあなたが言ったことを行ったが、まだビーコンを検出することはできません...私は何かが不足している? –

+0

"11111111-1111-1111-1111-111111111111"はあなたのビーコンUUIDですか? – Larme

+0

同じ質問をするでしょう:あなたはあなたの正しいビーコンのUUID、メジャー&マイナーを入力しましたか? また、アプリを新しくインストールしたときに、場所の許可アラートが表示されますか? – Rags93

5

ビーコンは、両方のフォアグラウンドで検出されただけでなく、ときに、背景にある

ステップすることができます:あなたのビーコンがONになっているとあなたがiBeaconフレームをお楽しみくださいテストしていることを確認します。 plist

info.plistでは、「使用方法の説明」を変更する必要があります。 の場合は、NSLocationAlwaysUsageDescriptionに文字列型のメッセージを追加してください。

ステップ: - バックグラウンドモード

2は、App代理人に次のコードを追加します。

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

ステップ: - の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) 

    } 
} 

注意以下のコードを追加します。フォアグラウンドまたはアクティブモード

の場合3: - あなたはそれに応じ

関連する問題