博客> iOS -- ibeacon的相关知识
iOS -- ibeacon的相关知识
2017-05-23 05:52 评论:0 阅读:140 OS_JJ
ios ibeacon的相关知识

第一.ibeacon的扫描实现

    1.导入头文件,调用代理和定义属性

            #import <CoreLocation>

            <CLLocationManagerDelegate>

            @property (strong, nonatomic) CLBeaconRegion *myBeaconRegion;
            @property (strong, nonatomic) CLLocationManager *locationManager;

    2配置info.plist相关参数

 0194A9E5-E0FB-4BEA-A8EE-539578841EC4.png

    3.初始化相关变量

         self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;

        //传入自己使用的ibeacon的UUID
        NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"e2c56db5-dffb-48d2-b060-d0f5a71096e0"];

        self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                                 identifier:@"com.appcoda.testregion"];

        [self.myBeaconRegion peripheralDataWithMeasuredPower:@-30];

        [_locationManager requestAlwaysAuthorization];

        [self.locationManager startMonitoringForRegion:self.myBeaconRegion];
        [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];

    4.调用相关方法

        - (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion*)region
        {
            NSLog(@"开始。。。");
        //    [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];
        }

        -(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion*)region
        {
            NSLog(@"结束。。。");
        //    [self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];
        }

        -(void)locationManager:(CLLocationManager*)manager
               didRangeBeacons:(NSArray*)beacons
                      inRegion:(CLBeaconRegion*)region
        {
        //    CLBeacon *foundBeacon = [beacons firstObject];
            NSLog(@"-----------beacons: %@",beacons);
        //    NSLog(@"----------region: %@",region);
        //    NSLog(@"---------%@",manager);

        }

        - (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
        {
            NSLog(@"Failed monitoring region: %@", error);
        }

        - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
        {
            NSLog(@"Location manager failed: %@", error);
        }

第二.相关知识链接和demo实现

知识链接

收藏
0
sina weixin mail 回到顶部