首页 > 移动开发 > IOS > 聊聊Apple的iBeacon技术
前言
约10厘米(immediate)
1米以内(near)
1米以外(far)
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; NSArray *myBeacon = @[UUID_BEACON_O, UUID_BEACON_T, UUID_BEACON_H]; for (NSString *uuid in myBeacon) { NSString *identifier = @""; if ([uuid isEqualToString:UUID_BEACON_O]) { identifier = @"左后"; }else if ([uuid isEqualToString:UUID_BEACON_T]) { identifier = @"右后"; }else if ([uuid isEqualToString:UUID_BEACON_H]) { identifier = @"左前"; } CLBeaconRegion *beacon = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:uuid] identifier:identifier]; beacon.notifyOnExit = YES; beacon.notifyOnEntry = YES; beacon.notifyEntryStateOnDisplay = YES; [self.locationManager startMonitoringForRegion:beacon]; [self.locationManager startRangingBeaconsInRegion:beacon]; } [self.locationManager requestAlwaysAuthorization];
滑 动显示更 多
关于位置的Delegate部分
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { if ([region isKindOfClass:[CLBeaconRegion class]]) { UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertBody = [NSString stringWithFormat:@"你已进入到 %@ 范围内", region.identifier]; notification.soundName = @"Default"; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; NSLog(@"%@", [NSString stringWithFormat:@"你已进入到 %@ 范围内", region.identifier]); }}- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { if ([region isKindOfClass:[CLBeaconRegion class]]) { UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.alertBody = [NSString stringWithFormat:@"你已离开 %@ 的范围", region.identifier]; notification.soundName = @"Default"; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; NSLog(@"%@", [NSString stringWithFormat:@"你已离开 %@ 的范围", region.identifier]); }}- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray<CLBeacon *> *)beacons inRegion:(CLBeaconRegion *)region { for (CLBeacon *beacon in beacons) { // NSLog(@"beacon Info : rssi is %ld, major is %@, minor is %@, accuracy is %f, proximity is %ld.", beacon.rssi, beacon.major, beacon.minor, beacon.accuracy, beacon.proximity); if (self.beaconDataSource.count == 3) { if ([[beacon.proximityUUID UUIDString] isEqualToString:UUID_BEACON_O]) { [self.beaconDataSource replaceObjectAtIndex:0 withObject:beacon]; [self.wait1 addObject:beacon]; //采集数据 左后 } if ([[beacon.proximityUUID UUIDString]isEqualToString:UUID_BEACON_T]) { [self.beaconDataSource replaceObjectAtIndex:1 withObject:beacon]; [self.wait2 addObject:beacon]; //采集数据 右后 } if ([[beacon.proximityUUID UUIDString]isEqualToString:UUID_BEACON_H]) { [self.beaconDataSource replaceObjectAtIndex:2 withObject:beacon]; [self.wait3 addObject:beacon]; //采集数据 左前 } if (!self.testButton.selected) { //测试按钮没有选中 //实时刷新位置信息 if (self.wait1.count == 20 && self.wait2.count == 20 && self.wait3.count == 20) { // NSArray *result = [DealModel dealWithBaseData:@[@[@"0", @"0"], @[@"220", @"0"], @[@"110", @"110"]] andThreeAuxData:@[self.wait1, self.wait2, self.wait3] andReal:@[self.textX.text, self.textY.text]]; DataProcessing *data = [[DataProcessing alloc] init]; NSArray *result2 = [data dealWithBaseData:@[@[@"0", @"0"], @[@"220", @"0"], @[@"110", @"110"]] andThreeAuxData:@[self.wait1, self.wait2, self.wait3] andReal:@[@"",@""]]; self.result1.text = self.result2.text; self.result2.text = [NSString stringWithFormat:@"X : %@\nY : %@", result2[0], result2[1]]; NSLog(@"%@", data); // self.xyView.center = CGPointMake([result[0] floatValue], [result[1] floatValue]); [self.wait1 removeAllObjects]; [self.wait2 removeAllObjects]; [self.wait3 removeAllObjects]; self.sureButton.selected = NO; self.sureButton.backgroundColor = [UIColor blackColor]; } }else { //开始测试单个设备误差 if (self.wait1.count == 20) { if (![self.textX.text isEqualToString:@""]) [DataProcessing testErrorWithUUID:UUID_BEACON_O andRealValue:self.textX.text andTestArray:self.wait1]; [self.wait1 removeAllObjects]; [self.wait2 removeAllObjects]; [self.wait3 removeAllObjects]; }else if (self.wait2.count == 20) { if (![self.textX.text isEqualToString:@""]) [DataProcessing testErrorWithUUID:UUID_BEACON_T andRealValue:self.textX.text andTestArray:self.wait2]; [self.wait1 removeAllObjects]; [self.wait2 removeAllObjects]; [self.wait3 removeAllObjects]; }else if (self.wait3.count == 20) { if (![self.textX.text isEqualToString:@""]) [DataProcessing testErrorWithUUID:UUID_BEACON_H andRealValue:self.textX.text andTestArray:self.wait3]; [self.wait1 removeAllObjects]; [self.wait2 removeAllObjects]; [self.wait3 removeAllObjects]; } } [self.localView refreshWithArray:self.beaconDataSource]; }else { if ([[beacon.proximityUUID UUIDString] isEqualToString:UUID_BEACON_O] || [[beacon.proximityUUID UUIDString]isEqualToString:UUID_BEACON_T] || [[beacon.proximityUUID UUIDString]isEqualToString:UUID_BEACON_H]) { [self.beaconDataSource addObject:beacon]; } } } [self.beaconTableView reloadData];}
滑 动显示更 多
总结
转载:掘金 作者:阿华12年
链接:https://juejin.cn/post/6995857698747056142
你好,这里是逻辑iOS技术号:一个让知识变得感性,让学习变得轻松!活跃的技术小站,希望给你的生活与技术带来意思不一样!关注公众号,回复“ 面试题”,即可领取更多大厂面试题型哦~ 小逻辑相信我们的生活不止眼前的苟且,还有我们向往的诗和大厂高薪工作~

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70005484/viewspace-2789704/ ,如需转载,请注明出处,否则将追究法律责任。