iBeacon 的話題最近很熱門,但大家開發都因為買不到 iBeacon 而作罷。其實你不一定要買到實體 iBeacon,實際上可以用 iPhone4, iPad2 或 iPodtouch 來做 Transmitter 就可以了。

把 iPhone 當 Transmitter 的現成 APP 很多,在 APP Stroe 找一下就有了,我們還是來談一下 iBeacon 的基礎吧。

入們幾件事

 

1. 引用 Frameworks,iBeacon 需要  CoreBluetooth 與 CoreLocation 兩個 Franeworks,除了在專案上引用以外,也要在 ViewController 的 .h 檔加入 import

另外,為了管理位置資訊,需建立兩個 property, 一個是位置管理,一個是Beacon的位置 Reagion
最後,為了管理訊息與位置,加入了 CLLocationManagerDelegate 與 UIAlertViewDelegate 兩個代理人

//設定Beacon常數

#import <UIKit/UIKit.h>

#import <CoreBluetooth/CoreBluetooth.h>

#import <CoreLocation/CoreLocation.h>

#import <AudioToolbox/AudioToolbox.h>

 

@interface ViewController : UIViewController<CLLocationManagerDelegate,UIAlertViewDelegate>

 

@property (nonatomic, strong) CLLocationManager *locationManager;

@property (nonatomic, strong) CLBeaconRegion *beaconRegion

 

 

 

2. 設定 locationManager 與必要參數

 每一個與 CLLocation 相關的程式,都需建立管理者,所以我們在 loadView 中,建立了 locationManager, 另外也要建立一個 BeaconRegion

 

//設定Beacon常數

staticNSString * const kUUID = @"B9407F30-F5F8-466E-AFF9-25556B57FE6D";

staticNSString * const kIdentifier = @“SomeIdentifier";

 

-(void)loadView{

    [superloadView];

    

    //    新增一個 locationManager

    self.locationManager = [[CLLocationManageralloc] init];

    self.locationManager.delegate=self;

    self.locationManager.activityType = CLActivityTypeFitness;

    self.locationManager.distanceFilter = kCLDistanceFilterNone;

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    

    

    NSLog(@"Turning on ranging...");

    

    [selfcreateBeaconRegion];

    [self.locationManagerstartRangingBeaconsInRegion:self.beaconRegion];

    [self.locationManagerstartMonitoringForRegion:self.beaconRegion];

 

    NSLog(@"Ranging turned on for region: %@.", self.beaconRegion);

 

}

 

#pragma mark - Beacon ranging

 

- (void)createBeaconRegion

 

{

 

    if (self.beaconRegion)

 

        return;

 

    NSUUID *proximityUUID = [[NSUUIDalloc] initWithUUIDString:kUUID];

 

    self.beaconRegion = [[CLBeaconRegionalloc] initWithProximityUUID:proximityUUID identifier:kIdentifier];

 

    self.beaconRegion.notifyEntryStateOnDisplay= YES;

 

    

 

}

 

3.其他的就是接收了,在以下的 method 中,加入自己想加的Code

 

//Region

-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error{

    

    NSLog(@"monitoring Error");

}

//進入 Region

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{

    

    NSLog(@"Found Region");

    UILocalNotification *theLocalPush=[[UILocalNotificationalloc] init];

    theLocalPush.alertBody=@"你找到我了,想要了解更多有關FunPush的資訊嗎?";

    theLocalPush.soundName=@"yes";

    theLocalPush.fireDate=[NSDatedateWithTimeIntervalSinceNow:0.5];

    theLocalPush.applicationIconBadgeNumber=1;

    [[UIApplicationsharedApplication] scheduleLocalNotification:theLocalPush];

}

//離開 Region

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{

    NSLog(@"didExitRegion");

}

 

//在持續偵測中

 

- (void)locationManager:(CLLocationManager *)manager

 

        didRangeBeacons:(NSArray *)beacons

 

               inRegion:(CLBeaconRegion *)region {

 

    NSLog(@"%@",beacons);

 

}

 

arrow
arrow
    全站熱搜

    shenfive 發表在 痞客邦 留言(3) 人氣()