最近做一個店鋪簽到獲取積分的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