ibeacon tips(potatotips27)

26
iBeacon Tips potatotips-27 佐佐 佐 iOS App Developer

Upload: -

Post on 13-Jan-2017

538 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: iBeacon tips(potatotips27)

iBeacon Tipspotatotips-27佐藤 光iOS App Developer

Page 2: iBeacon tips(potatotips27)

佐藤 光@SatoHikaruDeviOS アプリのプログラミングと、少しのサーバーサイド java や Android アプリのプログラミングをしています。

自己紹介

Page 3: iBeacon tips(potatotips27)

目次• iBeacon とは• iBeacon 利用時に必要な条件• 実装• よいと思ったところ• 悪いと思ったところ

Page 4: iBeacon tips(potatotips27)

iBeacon とは ?

Page 5: iBeacon tips(potatotips27)

• Beacon と呼ばれる発信機から出ている Bluetooth Low Energy ( BLE )の信号を受信する機能。

Page 6: iBeacon tips(potatotips27)

• 近くのお店のクーポンや商品情報を通知にするのに利用されている。

Page 7: iBeacon tips(potatotips27)

• iOS7 に搭載されて、2014年ぐらいに話題になったが、いまだあまり多くに浸透はしていないように感じている(似たような技術の NFC に比べて)。

Page 8: iBeacon tips(potatotips27)

• だが、アイデア次第では今後ブレイクの可能性はまだまだあるのでは?と思って、今回の題材にしました。

Page 9: iBeacon tips(potatotips27)

iBeacon 利用時に必要な条件

Page 10: iBeacon tips(potatotips27)

• Beacon 機器( aplix 、 estimote 、 Gimbal など)✓ 1 個300円〜3000円くらい✓ iPhone や Mac でも OK

• iOS7 以降• Bluetooth が搭載された iPhone/iPad/iPod など

Page 11: iBeacon tips(potatotips27)

実装

Page 12: iBeacon tips(potatotips27)

Beacon 監視開始 locationManager = CLLocationManager() locationManager.delegate = self //iBeacon は位置情報「常に利用」の許可が必要。 info.plist にも NSLocationAlwaysUsageDescription を追加しておく。 if locationManager.respondsToSelector("requestAlwaysAuthorization") { locationManager.requestAlwaysAuthorization() }

// ビーコン領域を生成 let uuidString = "EBEFD083-70A2-47C8-9837-E7B5634DF524" let beaconIdentifier = "sample_iBeacon" let beaconUUID = NSUUID(UUIDString: uuidString)! let beaconRegion = CLBeaconRegion(proximityUUID: beaconUUID, identifier: beaconIdentifier) //let beaconRegion = CLBeaconRegion(proximityUUID: beaconUUID, major: 3, minor: 1, identifier: beaconIdentifier) //beacon 領域に入ったときの delegate からの通知がいらないときは false 。 default は true //beaconRegion.notifyOnEntry = false //beacon 領域に入ったときの delegate からの通知がいらないときは false 。 default は true //beaconRegion.notifyOnExit = false //iPhone のロック解除ボタンが押されて、ロック中画面が表示されたときに Beacon の状態(内側にいるか外側にいるか)を確認するかどうか。 default は false beaconRegion.notifyEntryStateOnDisplay = true

//Beacon の領域 IN/OUT の監視開始 // これで、 locationManager:didEnterRegion や //locationManager:didExitRegion が通知される ( アプリが起動していない時でも ) //20 個 (uuidString/major/minor の組み合わせ ) の beaconRegion まで監視可能 locationManager.startMonitoringForRegion(beaconRegion)

Page 13: iBeacon tips(potatotips27)

Beacon 領域 IN/OUT を検知 (delegate) // 以下の delegete はアプリが起動していなくてもコールされる(その際、 AppDelegate の didFinishLaunchingWithOptionsがコールされた後に、以下の delegate メソッドがコールされる) /** * beacon の領域に入った */ func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) { manager.startRangingBeaconsInRegion(region as! CLBeaconRegion) manager.startUpdatingLocation() // 結構感度が良い } /** * beacon の領域から出た */ func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) { //didEnterRegion に比べて感度が悪い } /** * beaconRegion.notifyEntryStateOnDisplay = true 時に、 iPhone のロック解除ボタンが押されて、ロック中画面が表示された時、コールされる。 */ func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) { // 現在、対象 Beacon の内側 / 外側 / どちらかかわらないかが、 state で取得できる }

Page 14: iBeacon tips(potatotips27)

Beacon 領域 IN/OUT のエラー検知(delegate)

/** * beacon 監視でエラー。 */ func locationManager(manager: CLLocationManager, monitoringDidFailForRegion region: CLRegion?, withError error: NSError) { //ex) 監視する beacon が無効、 beacon を21種類以上登録した場合など }

Page 15: iBeacon tips(potatotips27)

Beacon データの受信開始 locationManager.delegate = self //Beacon データの受信開始。 Beacon データ( CLBeacon )には //uuid/major/minor/CLProximity( すぐ近く / 近い / 遠い )/rssi( 信号強度 ) が入っている locationManager.startRangingBeaconsInRegion(beaconRegion)

Page 16: iBeacon tips(potatotips27)

Beacon データ受信を検知 (delegate) /** * Beacon データ受信処理 */ func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) { //Beacon データ受信時は何度もコールされる。 CLBeacon には uuid/major/minor/CLProximity( すぐ近く / 近い / 遠い )/rssi( 信号強度 ) が入っている //Beacon 範囲に入っている時は beacons リストに CLBeacon オブジェクトが入っているが、 //Beacon 範囲から出てもしばらく beacons リストが空でコールされる。 }

Page 17: iBeacon tips(potatotips27)

Beacon データ受信のエラー検知(delegate)

func locationManager(manager: CLLocationManager, rangingBeaconsDidFailForRegion region: CLBeaconRegion, withError error: NSError) { //beacon データ受信でエラー。 //ex) 監視する beacon が無効など }

Page 18: iBeacon tips(potatotips27)

よいと思ったところ

Page 19: iBeacon tips(potatotips27)

• iPhone のロック解除ボタンを押した時にもBeacon 検知タイミングがあるところ。

✓ ローカル通知したいときに、ユーザーの目につきそう

Page 20: iBeacon tips(potatotips27)

• Beacon 検知している時、ロック画面左下にアプリのアイコンが表示される。

by スマートフォン EC ラボ (http://smartphone-ec.net/)

Page 21: iBeacon tips(potatotips27)

• 省電力✓ iBeacon の方が、 GPS を利用した領域観測

(iOS の別機能 ) に比べて電池が長持ちしそう✓ Beacon 機器は乾電池2本でほとんどのものが1年以上は持つ。

Page 22: iBeacon tips(potatotips27)

悪いと思ったところ

Page 23: iBeacon tips(potatotips27)

• iPhone の電源 ON にした直後1〜5分くらい Beacon を検知できない時間がある。

Page 24: iBeacon tips(potatotips27)

• Beacon 領域 OUT の検知が遅いので、それを早めに検知したい時は工夫が必要そう。✓ Beacon データ受信検知で受信データが空になった時と組み合わるなど

Page 25: iBeacon tips(potatotips27)

まとめ• 実装はかなり簡単。• アプリを起動していなくても、起動できるトリガーが多い( Beacon IN/OUT 検知、ロック画面表示時)。• まだ流行ってはいないが、日本でも徐々に

iBeacon サービスが出てきているので、今後普及するかも?

Page 26: iBeacon tips(potatotips27)

佐藤 光@SatoHikaruDevhttp://qiita.com/HIkaruSato

ご静聴ありがとうございました。