Objective C how to find out the distance between two iPhones indoor using iBeacon

旧时模样 提交于 2020-06-08 14:23:39

问题


I am using iBeacon for calculating distance between one iPhone to another iPhone indoor. I am using UUID and paste in code. But i did not get the distance from one iPhone to another iPhone indoor. i am writing the code like this.Can any body help me please. I got the error like this: CBManagerStateUnsupported. and one more issue is: [CoreBluetooth] XPC connection invalid

#import <UIKit/UIKit.h>

#import <CoreLocation/CoreLocation.h>

#import <CoreBluetooth/CoreBluetooth.h>

@interface ConfigViewController : UIViewController <CBPeripheralManagerDelegate,CBPeripheralDelegate,CBCentralManagerDelegate>

{

    CBCentralManager *manager;

}

@property (strong, nonatomic) CLBeaconRegion *beaconRegion;

@property (weak, nonatomic) IBOutlet UILabel *uuidLabel;

@property (weak, nonatomic) IBOutlet UILabel *majorLabel;

@property (weak, nonatomic) IBOutlet UILabel *minorLabel;

@property (weak, nonatomic) IBOutlet UILabel *identityLabel;

@property (strong, nonatomic) NSDictionary *beaconPeripheralData;

@property (strong, nonatomic) CBPeripheralManager *peripheralManager;

@end

- (void)viewDidLoad
{

[super viewDidLoad];

// Do any additional setup after loading the view.

[self initBeacon];

[self setLabels];

}

- (void)initBeacon {

NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"EC1309B2-9736-40FF-B255-7EE047F2166B"];

self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
major:1 minor:1 identifier:@"com.devfright.myRegion"];

self.beaconPeripheralData =[self.beaconRegion peripheralDataWithMeasuredPower:nil];

self.peripheralManager =[[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];

}

- (void)setLabels {

self.uuidLabel.text = self.beaconRegion.proximityUUID.UUIDString;

self.majorLabel.text = [NSString stringWithFormat:@"%@", self.beaconRegion.major];

self.minorLabel.text = [NSString stringWithFormat:@"%@", self.beaconRegion.minor];

self.identityLabel.text = self.beaconRegion.identifier;

}

- (IBAction)transmitBeacon:(UIButton *)sender {

self.beaconPeripheralData = [self.beaconRegion peripheralDataWithMeasuredPower:nil];

self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];

}

-(void) peripheralManagerDidUpdateState:
(CBPeripheralManager *)peripheral {

NSLog(@"peripheralManagerDidUpdateState:");

switch (peripheral.state) {

case CBManagerStatePoweredOff:

NSLog(@"CBManagerStatePoweredOff");

break;

case CBManagerStateResetting:

NSLog(@"CBManagerStateResetting");

break;

case CBManagerStatePoweredOn:

NSLog(@"CBManagerStatePoweredOn");

//---start advertising the beacon data---

[self.peripheralManager startAdvertising:self.beaconPeripheralData];

break;

case CBManagerStateUnauthorized:

NSLog(@"CBManagerStateUnauthorized");

break;

case CBManagerStateUnsupported:

NSLog(@"CBManagerStateUnsupported");

break;

default:

NSLog(@"CBPeripheralStateUnknown");

break;

}

}

TrackViewController.h

@interface TrackViewController : UIViewController <CLLocationManagerDelegate,CBPeripheralDelegate,CBCentralManagerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *beaconFoundLabel;

@property (weak, nonatomic) IBOutlet UILabel *proximityUUIDLabel;

@property (weak, nonatomic) IBOutlet UILabel *majorLabel;

@property (weak, nonatomic) IBOutlet UILabel *minorLabel;

@property (weak, nonatomic) IBOutlet UILabel *accuracyLabel;

@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;

@property (weak, nonatomic) IBOutlet UILabel *rssiLabel;

@property (strong, nonatomic) CLBeaconRegion *beaconRegion;

@property (strong, nonatomic) CLLocationManager *locationManager;

TrackViewController.m

- (void)viewDidLoad
{

[super viewDidLoad];

// Do any additional setup after loading the view.

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

self.locationManager.delegate = self;

//---create a NSUUID object---

NSUUID *proximityUUID =[[NSUUID alloc] initWithUUIDString:@"EC1309B2-9736-40FF-B255-7EE047F2166B"];

self.beaconRegion =[[CLBeaconRegion alloc]initWithProximityUUID:proximityUUID
                  identifier:@"net.learn2develop.myRegion"];

//---start monitoring a region---

[self.locationManager startMonitoringForRegion:self.beaconRegion];

}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {

//---start ranging for iBeacons---

[self.locationManager startRangingBeaconsInRegion:self.beaconRegion];

// self.lblStatus.text = @"Entered region";

UILocalNotification *localNotification =[[UILocalNotification alloc] init];

//---the message to display for the alert---

localNotification.alertBody =@"You have entered the region you are monitoring";

//---uses the default sound---

localNotification.soundName =UILocalNotificationDefaultSoundName;

//---title for the button to display---

localNotification.alertAction = @"Details";

//---schedule the notification---

[[UIApplication sharedApplication]presentLocalNotificationNow:localNotification];

}

-(void)locationManager:(CLLocationManager *)manager
         didExitRegion:(CLRegion *)region {

//---stop ranging for iBeacons---

[self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];

}

//---when iBeacons are found---
-(void)locationManager:(CLLocationManager *)manager
       didRangeBeacons:(NSArray *)beacons
              inRegion:(CLBeaconRegion *)region {

CLBeacon *beacon = [[CLBeacon alloc] init];

beacon = [beacons lastObject];

self.beaconFoundLabel.text = @"Yes";

self.proximityUUIDLabel.text = beacon.proximityUUID.UUIDString;

self.majorLabel.text =[NSString stringWithFormat:@"%@", beacon.major];

self.minorLabel.text =

[NSString stringWithFormat:@"%@", beacon.minor];

self.accuracyLabel.text =[NSString stringWithFormat:@"%f meters", beacon.accuracy];

switch (beacon.proximity) {

case CLProximityUnknown:

self.distanceLabel.text = @"Unknown Proximity";

break;

case CLProximityImmediate:

self.distanceLabel.text = @"Immediate";

break;

case CLProximityNear:

self.distanceLabel.text = @"Near";

break;

case CLProximityFar:

self.distanceLabel.text = @"Far";

break;

}

self.rssiLabel.text =[NSString stringWithFormat:@"%li decibels",
            (long)beacon.rssi];

}

来源:https://stackoverflow.com/questions/61538048/objective-c-how-to-find-out-the-distance-between-two-iphones-indoor-using-ibeaco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!