最近做一个店铺签到获取积分的App,用到了iBeacon,蛮好玩的一个小玩意,简单来说iBeacon这个小设备,可以被手机通过蓝牙搜索到,并能比较精确的显示距离,和拿到该iBeacon的uuid,major,minor。其中uuid 是一个区域内的唯一标识符,用它可以区别一个公司的iBeacon,而用major和minor 来区别店铺和具体哪台设备。
客户端代码实现
//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
{
//发现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
网友评论