2016年5月19日 星期四

Beacon相關

好吧,其實小弟目前是在beacon公司,

至於拖到現在才寫.....xd



總之呢,這篇會分成兩部分,一個正常版,一個則是當app退到背景後,去做掃描

會把一些比較特殊的部分來做個分享

首先:設置

1.先把背景給打開




2.到plist檔案,這邊有兩種,一種是wheninuse表示說,當app打開才可以掃描,下面這一種就是讓app可以一直掃(背景)



3.最後別忘記加入framework





注意事項:

配合上面第二點,別忘了給locationManager配合設置:

if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [locationManager requestAlwaysAuthorization];

    }


說明:

ios這邊在beacon這部份,是依照uuid去做掃描,

主要可以分成Monitoring和ranging



Monitoring:


讓 app 知道是不是進到 beacon 的範圍內, 以及一些狀態的變動, 這邊配合上面的plist檔的設定

當呼叫

[locationManager startMonitoringForRegion:beaconRegion1];

會在delegate方法內callback:

didEnterRegion
didExitRegion
didDetermineState


didStartMonitoringForRegion

ranging:

這個方法主要是拿來測距離的,當然,可以想成說,測這個距離其實也頗重要,但是!最重要的是,我們可以在這callback中,拿到我要的beacon阿!

呼叫

[locationManager startRangingBeaconsInRegion:(CLBeaconRegion*)region];


會進入delegate方法內callback:

didRangeBeacons


照慣例,先附上代碼:

https://github.com/web8246/TestIBeacon.git

ps.這份碼內還附加了LocalNotification


下面則是在背景掃描:
https://github.com/web8246/IBeaconInBackground

奇怪的是,網路很多大神說無法在背景時拿到didRangeBeacons的callback,

但是我用4s測試,卻會順利拿到,希望有人能解答,感激!


ㄐㄧ經過實測當app退到背景,可以接收到beacon,甚至app被kill掉,只要方法正確,一ㄧ樣可以收到beacon的callback,下面是實作:
https://github.com/web8246/TestScanBeaconBackground

沒有留言:

張貼留言