iOS iBeacon 使用

最近做一個店鋪簽到獲取積分的App甸赃,用到了iBeacon嘴瓤,蠻好玩的一個小玩意舷嗡,簡單來說iBeacon這個小設備轴猎,可以被手機通過藍牙搜索到,并能比較精確的顯示距離进萄,和拿到該iBeacon的uuid捻脖,major,minor中鼠。其中uuid 是一個區(qū)域內的唯一標識符可婶,用它可以區(qū)別一個公司的iBeacon,而用major和minor 來區(qū)別店鋪和具體哪臺設備援雇。

客戶端代碼實現(xiàn)

//BeaconClient.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface BeaconClient : NSObject<CLLocationManagerDelegate>
{
    CLLocationManager * _locationManager;
    CLBeaconRegion * _region;
    BOOL _isInsideRegion;
}

- (BOOL)openClient;
- (void)closeClient;

@end

//BeaconClient.m
#import "BeaconClient.h"
@implementation BeaconClient

- (id)init
{
    if (self = [super init]) {
        _isInsideRegion = NO;
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        
        NSUUID *estimoteUUID = [[NSUUID alloc] initWithUUIDString:kUUID];
        _region = [[CLBeaconRegion alloc] initWithProximityUUID:estimoteUUID identifier:kIndetifier];
        // launch app when display is turned on and inside region
        _region.notifyEntryStateOnDisplay = YES;
    }
    return self;
}

- (BOOL)openClient
{
    if ([CLLocationManager locationServicesEnabled]) {
        //開始定位用戶的位置
        [_locationManager startUpdatingLocation];
        [_locationManager requestAlwaysAuthorization];
        //每隔多少米定位一次(這里的設置為任何的移動)
        _locationManager.distanceFilter=kCLDistanceFilterNone;
        _locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
    }else{//不能定位用戶的位置
        //1.提醒用戶檢查當前的網絡狀況
        //2.提醒用戶打開定位開關
    }
    
    if ([CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]])
    {
        [_locationManager startMonitoringForRegion:_region];
        [_locationManager startRangingBeaconsInRegion:_region];
        [_locationManager requestStateForRegion:_region];
        return YES;
    }else{
        [self showAlertView:nil message:@"This device does not support monitoring beacon regions"];
        return NO;
    }
}

- (void)closeClient
{
    [_locationManager stopMonitoringForRegion:_region];
    [_locationManager stopRangingBeaconsInRegion:_region];
    
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"!!");
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
      didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    if(state == CLRegionStateInside)
    {
        _isInsideRegion = YES;
    }else if(state == CLRegionStateOutside){
        _isInsideRegion = NO;
    }else{
        _isInsideRegion = NO;
    }
}

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    //發(fā)現(xiàn)iBeacon Server
    for (CLBeacon* beacon in beacons) {
        
        NSString * info_1 = [NSString stringWithFormat:@"UUIDString:%@",beacon.proximityUUID.UUIDString];
        NSString * info_2 = [NSString stringWithFormat:@"major:%@",beacon.major];
        NSString * info_3 = [NSString stringWithFormat:@"minor:%@",beacon.minor];
        NSString * info_4 = [NSString stringWithFormat:@"accuracy:%0.4f米",beacon.accuracy];
        NSString * info_5 = [NSString stringWithFormat:@"proximity:%ld",beacon.proximity];
        NSString * info_6 = [NSString stringWithFormat:@"rssi:%ld",(long)beacon.rssi];
        
        NSString * debugInfo = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@\n%@\n",info_1,info_2,info_3,info_4,info_5,info_6];
        NSLog(@"%@",debugInfo);
    }
}

- (void)locationManager:(CLLocationManager *)manager
         didEnterRegion:(CLRegion *)region
{
    NSLog(@"didEnterRegion");
    if (_isInsideRegion) return;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        [self sendEnterLocalNotification];
    }else{
        [self showAlertView:nil message:@"Hi矛渴,你已經進入 iSS iBeacon region"];
    }
}


- (void)locationManager:(CLLocationManager *)manager
          didExitRegion:(CLRegion *)region
{
    NSLog(@"didExitRegion");
    if (!_isInsideRegion) return;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        [self sendExitLocalNotification];
    }else{
        [self showAlertView:nil message:@"sorry,你離開了 iSS iBeacon region"];
    }
}

- (void)showAlertView:(NSString *)title message:(NSString *)msg
{
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:title
                                                         message:msg
                                                        delegate:nil
                                               cancelButtonTitle:@"確定"
                                               otherButtonTitles:nil];
    [alertView show];
}

- (void)sendEnterLocalNotification
{
    UILocalNotification *notice = [[UILocalNotification alloc] init];
    notice.alertBody = @"Hi惫搏,你已經進入 iSS iBeacon region具温,打開應用獲取最新信息";
    notice.alertAction = @"Open";
    notice.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:notice];
}

- (void)sendExitLocalNotification
{
    UILocalNotification *notice = [[UILocalNotification alloc] init];
    notice.alertBody = @"sorry,你離開了 iSS iBeacon region";
    notice.alertAction = @"Open";
    notice.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:notice];
}
@end

項目源碼地址 感謝作者的分享
同時可以參考蘋果官方源碼筐赔,里面有設計多個uuid同時檢測
蘋果官方提供demo

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末铣猩,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子茴丰,更是在濱河造成了極大的恐慌达皿,老刑警劉巖天吓,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異峦椰,居然都是意外死亡龄寞,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門汤功,熙熙樓的掌柜王于貴愁眉苦臉地迎上來物邑,“玉大人,你說我怎么就攤上這事冤竹》鞣猓” “怎么了?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵鹦蠕,是天一觀的道長冒签。 經常有香客問我,道長钟病,這世上最難降的妖魔是什么萧恕? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮肠阱,結果婚禮上票唆,老公的妹妹穿的比我還像新娘。我一直安慰自己屹徘,他們只是感情好走趋,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著噪伊,像睡著了一般簿煌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鉴吹,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天姨伟,我揣著相機與錄音,去河邊找鬼豆励。 笑死夺荒,一個胖子當著我的面吹牛,可吹牛的內容都是我干的良蒸。 我是一名探鬼主播技扼,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼嫩痰!你這毒婦竟也來了淮摔?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤始赎,失蹤者是張志新(化名)和其女友劉穎和橙,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體造垛,經...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡魔招,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了五辽。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片办斑。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖杆逗,靈堂內的尸體忽然破棺而出乡翅,到底是詐尸還是另有隱情,我是刑警寧澤罪郊,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布蠕蚜,位于F島的核電站,受9級特大地震影響悔橄,放射性物質發(fā)生泄漏靶累。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一癣疟、第九天 我趴在偏房一處隱蔽的房頂上張望挣柬。 院中可真熱鬧,春花似錦睛挚、人聲如沸邪蛔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽侧到。三九已至,卻和暖如春委乌,著一層夾襖步出監(jiān)牢的瞬間床牧,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工遭贸, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留戈咳,地道東北人。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓壕吹,卻偏偏與公主長得像著蛙,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子耳贬,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內容