#import "ConfigViewController.h"@interfaceConfigViewController()@end@implementationConfigViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.[selfinitBeacon];[selfsetBeacon];[selfupdateText];[selfupdateLabels];}-(void)initBeacon{uuid=[NSMutableStringstringWithString:@"23542266-18D1-4FE4-B4A1-23F8195B9D39"];major=[NSMutableStringstringWithString:@"11"];minor=[NSMutableStringstringWithString:@"12"];identity=[NSMutableStringstringWithString:@"com.BoXer.Test"];}-(void)setBeacon{NSUUID*nsuuid=[[NSUUIDalloc]initWithUUIDString:uuid];self.beaconRegion=[[CLBeaconRegionalloc]initWithProximityUUID:nsuuidmajor:[majorintegerValue]minor:[minorintegerValue]identifier:identity];}-(void)updateText{self.uuidText.text=uuid;self.majorText.text=major;self.minorText.text=minor;self.identityText.text=identity;}-(void)setBeaconInfo{[uuidsetString:self.uuidText.text];[majorsetString:self.majorText.text];[minorsetString:self.minorText.text];[identitysetString:self.identityText.text];}-(void)updateLabels{self.uuidLabel.text=uuid;self.majorLabel.text=major;self.minorLabel.text=minor;self.identityLabel.text=identity;}-(IBAction)transmitBeacon:(UIButton*)sender{self.beaconPeripheralData=[self.beaconRegionperipheralDataWithMeasuredPower:nil];self.peripheralManager=[[CBPeripheralManageralloc]initWithDelegate:selfqueue:niloptions:nil];[selfsetBeaconInfo];[selfupdateLabels];[selfsetBeacon];}-(void)peripheralManagerDidUpdateState:(CBPeripheralManager*)peripheral{if(peripheral.state==CBPeripheralManagerStatePoweredOn){NSLog(@"Powered On");[self.peripheralManagerstartAdvertising:self.beaconPeripheralData];}elseif(peripheral.state==CBPeripheralManagerStatePoweredOff){NSLog(@"Powered Off");[self.peripheralManagerstopAdvertising];}}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}-(IBAction)ViewTouchDown:(id)sender{// 发送resignFirstResponder// 这样使得点击空白处可收回键盘[[UIApplicationsharedApplication]sendAction:@selector(resignFirstResponder)to:nilfrom:nilforEvent:nil];}@end
#import "TrackViewController.h"@interfaceTrackViewController()@end@implementationTrackViewController-(void)viewDidLoad{[superviewDidLoad];tv.delegate=self;tv.dataSource=self;[tvsetBackgroundColor:[UIColorgrayColor]];self.locationManager=[[CLLocationManageralloc]init];self.locationManager.delegate=self;[selfinitRegion];[selflocationManager:self.locationManagerdidStartMonitoringForRegion:self.beaconRegion];}-(void)initRegion{NSUUID*uuid=[[NSUUIDalloc]initWithUUIDString:@"23542266-18D1-4FE4-B4A1-23F8195B9D39"];self.beaconRegion=[[CLBeaconRegionalloc]initWithProximityUUID:uuididentifier:@"123"];[self.locationManagerstartMonitoringForRegion:self.beaconRegion];if(iBeaconsInfo==nil){iBeaconsInfo=[[NSMutableArrayalloc]init];}}-(void)locationManager:(CLLocationManager*)managerdidStartMonitoringForRegion:(CLRegion*)region{[self.locationManagerstartRangingBeaconsInRegion:self.beaconRegion];}-(void)locationManager:(CLLocationManager*)managerdidEnterRegion:(CLRegion*)region{NSLog(@"Beacon Found");[self.locationManagerstartRangingBeaconsInRegion:self.beaconRegion];}-(void)locationManager:(CLLocationManager*)managerdidExitRegion:(CLRegion*)region{NSLog(@"Left Region");[self.locationManagerstopRangingBeaconsInRegion:self.beaconRegion];[tvreloadData];}-(void)locationManager:(CLLocationManager*)managerdidRangeBeacons:(NSArray*)beaconsinRegion:(CLBeaconRegion*)region{if([beaconscount]>0){[iBeaconsInfosetArray:beacons];[tvreloadData];}}#pragma mark - UITableView-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{return1;}-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{return[iBeaconsInfocount];}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{staticNSString*cellIdentifier=@"iBeaconsCell";UITableViewCell*cell=[tvdequeueReusableCellWithIdentifier:cellIdentifier];if(cell==nil){cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellIdentifier];}return[selfprepareCellForBeaconInfo:cellindex:indexPath];}-(UITableViewCell*)prepareCellForBeaconInfo:(UITableViewCell*)cellindex:(NSIndexPath*)indexPath{CLBeacon*beacon=[iBeaconsInfoobjectAtIndex:indexPath.row];NSString*distanceText=[[NSStringalloc]init];if(beacon.proximity==CLProximityUnknown){distanceText=@"未知距离";}elseif(beacon.proximity==CLProximityImmediate){distanceText=@"非常近";}elseif(beacon.proximity==CLProximityNear){distanceText=@"近";}elseif(beacon.proximity==CLProximityFar){distanceText=@"远";}cell.textLabel.numberOfLines=0;cell.detailTextLabel.numberOfLines=0;cell.textLabel.text=[NSStringstringWithFormat:@"%@",beacon.proximityUUID.UUIDString];cell.detailTextLabel.text=[NSStringstringWithFormat:@"主要值:%@ 次要值:%@\r\n感应距离:%f\r\n大概距离:%@ 信号强度:%ld",beacon.major,beacon.minor,beacon.accuracy,distanceText,(long)beacon.rssi];[cellsizeToFit];returncell;}-(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{return100;}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@end