This is the 13th day of my participation in the August More Text Challenge. For details, see:August is more challenging
My column
- Exploring the underlying principles of iOS
- Summary of the phase of iOS underlying principle exploration
preface
According to online sources, apple announced new features for iOS7 at WWDC 2013. Before, using iBeacon equipment to do a positioning algorithm research, so to summarize, but also hope to communicate with you.
What is a iBeacon
First of all, iBeacon’s logo is the picture below
Now, iBeacon is a technology that Apple built on low power Bluetooth technology. Specifically, using a broadcast frame called an “announcement frame” in BLE. Notification frames are emitted periodically by devices equipped with BLE, and can be received by any terminal that supports BLE. The payload portion of the notification frame, which writes the Data defined by Apple.
The data of an iBeacon base station consists of roughly four parts of information:
- 1, UUID (universally Unique Identifier) : a 128-bit unique identifier of one or more Beacon base stations for a specific type or specific organization.
- 2. Major: a 16-bit unsigned integer that can connect Beacon base stations with the same proximity UUID. (User can customize)
- 3. Minor: Ditto.
- 4. Measured Power: refers to the reference value of signal strength (RSSI) when the distance between the transmitting module and the receiver of iBeacon is 1 meter.
The relative distance between Beacon base station and device is determined by sending specific identification information through Bluetooth low power technology (BLE). And this distance is not exactly calculated, but it is divided into three levels:
- About 10 cm (immediate)
- It is near one meter.
- 1 meter away (far)
This is because when the distance between the transmitting and receiving devices is within 1 meter, the RSSI value basically decreases in accordance with the theoretical value; while when the distance is outside 1 meter, RSSI does not decrease obviously with the increase of the distance, but fluctuates up and down under the influence of the transmitting wave. In other words, the distance of 1 meter is not very precise and can be summarized as far.
Next, get to the point:
Now, with an iBeacon we can measure the distance between the Beacon device and the scanning Beacon device (the distance value is given in Apple’s API). So, guess, there are three beacons, and then know their coordinate information, so we take the phone to constantly scan the RSSI information of the three beacons can use the location algorithm to locate.
Now, the code
The part about starting the positioning
Here, I set three datum points as coordinate points to detect the position of the handheld device.
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 = @beacon_l; }else if ([uuid isEqualToString:UUID_BEACON_T]) {identifier = @"; }else if ([uuid isEqualToString:UUID_BEACON_H]) {identifier = @" left front "; } 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];Copy the code
The Delegate part about the location
Processing received signals sent by the system, in this case, mainly to do the local message push. In the continuous detection of signal strength, according to the last reference article, the principle of triangulation can be implemented to realize the positioning of equipment.
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { if ([region isKindOfClass:[CLBeaconRegion class]]) { UILocalNotification *notification = [[UILocalNotification alloc] init]; Notification. alertBody = [NSString stringWithFormat:@" you are in %@ range ", region.identifier]; notification.soundName = @"Default"; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; NSLog(@"%@", [NSString stringWithFormat:@" you are in the %@ range ", region.identifier]); } } - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { if ([region isKindOfClass:[CLBeaconRegion class]]) { UILocalNotification *notification = [[UILocalNotification alloc] init]; Notification. alertBody = [NSString stringWithFormat:@" you are out of %@ range ", region.identifier]; notification.soundName = @"Default"; [[UIApplication sharedApplication] presentLocalNotificationNow:notification]; NSLog(@"%@", [NSString stringWithFormat:@" you are out of %@ range ", 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 (! If (self.wait1.count == 20 && self.wait2.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]; 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]; }Copy the code
conclusion
IOS devices can not only detect the surrounding iBeacon signal, but also can use itself as an iBeacon to send signal content to the outside world (the specific implementation is very simple, we consult the data).
Finally, in the actual use, the triangulation algorithm was realized by referring to the last article. However, the signal fluctuation was still very large, which did not meet the ideal, so the research was not continued. If you are interested, we can discuss it together.
reference
Research on Indoor Positioning Technology based on iBeacon Base Station