Как я могу получить несколько iBeacon, используя UUID динамически? - PullRequest
1 голос
/ 04 мая 2020

Я работаю над iBeacon, чтобы получить несколько iBeacon. Мое требование - использовать iBeacon. Мне нужно получить расстояние между несколькими iPhone. Например у меня 50 айфонов. Я хочу получить расстояние между устройствами. А также я хочу получить UUID динамически. Я могу получить расстояние между одним iPhone и другим iPhone, но не могу определить расстояние между несколькими iPhone. Я пишу код так:

- (void)viewDidLoad
{
    accArr = [[NSMutableArray alloc]init];
    distanceArr = [[NSMutableArray alloc]init];
    tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height)];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];

    self.locationManager.delegate = self;

    NSArray *uuids = [NSArray arrayWithObjects:@"5827E334-2C2A-486C-9982-562B189F153D", @"65D47EEF-403A-417F-9DA9-0272F9C22437",@"BA20BEB3-85B7-493D-A5B8-629BDE8666B7",@"5734243E-8ECC-4216-BF22-1530B6740214", nil];

    for (NSString *uuidString in uuids) {
        // [[NSUUID alloc] initWithUUIDString:uuidString] identifier:identifier];
        self.beaconRegion =[[CLBeaconRegion alloc]initWithUUID: [[NSUUID alloc] initWithUUIDString:uuidString] identifier:@"net.learn2develop.myRegion"];
        [self.locationManager requestAlwaysAuthorization];
        [self.locationManager startMonitoringForRegion:self.beaconRegion];
    }
}

- (void)locationManager:(CLLocationManager *)manager
         didEnterRegion:(CLRegion *)region {
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

-(void)locationManager:(CLLocationManager *)manager
         didExitRegion:(CLRegion *)region {
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"HistoryCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        accuracyLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 2, 180, 20)];
        accuracyLabel.text = [accArr objectAtIndex:indexPath.row];
        [cell.contentView addSubview:accuracyLabel];
        distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(190, 2, 80, 20)];
        distanceLabel.text = [distanceArr objectAtIndex:indexPath.row];
        [cell.contentView addSubview:distanceLabel];
    }

    return cell;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *uuids = [NSArray arrayWithObjects:@"8B41A13E-DCF5-4DF5-8E34-CCEB3BABE1CA", @"CA4D59DC-200D-4035-AF93-1942FBA64F1F",@"66C57B14-C509-4449-AB5C-0D149DC0058B",@"58EFC1A0-FB45-472A-9D60-5EABB3B3300C", nil];

    for (NSString *uuidString in uuids) {
        self.beaconRegion = [[CLBeaconRegion alloc] initWithUUID: [[NSUUID alloc] initWithUUIDString:uuidString] major:1 minor:1 identifier:@"com.devfright.myRegion"];
        self.beaconPeripheralData =[self.beaconRegion peripheralDataWithMeasuredPower:nil];
        self.peripheralManager =[[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
    }
}

- (IBAction)transmitBeacon:(UIButton *)sender {
    [self.peripheralManager startAdvertising:self.beaconPeripheralData];
}

-(void) peripheralManagerDidUpdateState:
(CBPeripheralManager *)peripheral {
    NSLog(@"peripheralManagerDidUpdateState:");

    switch (peripheral.state) {
        case CBManagerStatePoweredOff:
            NSLog(@"CBManagerStatePoweredOff");
            break;

        case CBManagerStateResetting:
            NSLog(@"CBManagerStateResetting");
            break;

        case CBManagerStatePoweredOn:
            NSLog(@"CBManagerStatePoweredOn");
            break;

        case CBManagerStateUnauthorized:
            NSLog(@"CBManagerStateUnauthorized");
            break;

        case CBManagerStateUnsupported:
            NSLog(@"CBManagerStateUnsupported");
            break;

        default:
            NSLog(@"CBPeripheralStateUnknown");
            break;
    }
}
...