-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathIndoorViewController.m
228 lines (145 loc) · 7.58 KB
/
IndoorViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// IndoorViewController.m
// IndoorGPS
//
// Created by Alex on 2/3/14.
// Copyright (c) 2014 Alex Wellnitz. All rights reserved.
//
#import "IndoorViewController.h"
#import "ESTBeaconManager.h"
//Animation (Test Ball)//
#import <QuartzCore/QuartzCore.h>
//Animation (Test Ball)//
@interface IndoorViewController () <ESTBeaconManagerDelegate>
@property (nonatomic, strong) ESTBeaconManager* beaconManager;
//Calculating
@property (nonatomic, strong) NSMutableDictionary* beaconDictionary;
@property(nonatomic) NSMutableDictionary* radiusDict;
//Moving Image (Map)
@property(nonatomic) UIImage* ballImage;
@property(nonatomic) UIImageView* ball;
@property(nonatomic, assign) CGPoint position;
@end
@implementation IndoorViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Animation (Test Ball)
UIImage *ballImage = [UIImage imageNamed:@"positionPoint.png"];
self.ball = [[UIImageView alloc] initWithImage:ballImage];
[self.view addSubview:self.ball];
///////////Beacon /////////////
self.beaconDictionary = [NSMutableDictionary dictionary];
// setup Estimote beacon manager
// craete manager instance
self.beaconManager = [[ESTBeaconManager alloc] init];
self.beaconManager.delegate = self;
self.beaconManager.avoidUnknownStateBeacons = YES;
// create sample region object (you can additionaly pass major / minor values)
ESTBeaconRegion* region = [[ESTBeaconRegion alloc] initRegionWithIdentifier:@"EstimoteSampleRegion"];
// start looking for estimote beacons in region
// when beacon ranged beaconManager:didRangeBeacons:inRegion: invoked
[self.beaconManager startRangingBeaconsInRegion:region];
CGPoint position = CGPointMake(1,1);
[self ballAnimation:position];
}
- (void)ballAnimation:(CGPoint)position{
position.x = 0;
position.y = 1000;
self.ball.center = position;
}
-(void)beaconManager:(ESTBeaconManager *)manager
didRangeBeacons:(NSArray *)beacons
inRegion:(ESTBeaconRegion *)region
{
NSMutableArray* activeBeaconIds = [NSMutableArray array];
for (ESTBeacon* beacon in beacons)
{
NSString* majorMinor = [NSString stringWithFormat:
@"%i-%i",
[beacon.major unsignedShortValue],
[beacon.minor unsignedShortValue]];
//The working entry
NSString* beaconDistance = [NSString stringWithFormat:@"%f",[beacon.distance floatValue]];
//Checking if Key exist
NSMutableArray* distanceArray = [NSMutableArray array];
if ([self.beaconDictionary objectForKey:majorMinor])
{
distanceArray = [self.beaconDictionary objectForKey:majorMinor];
}
[distanceArray addObject:beaconDistance];
//if more than 9 values in activeBeaconIDs than...
if ([distanceArray count] > 9 ){
[activeBeaconIds addObject:majorMinor];
}
[self.beaconDictionary setObject:distanceArray forKey:majorMinor];
}
NSLog(@"Beacon Distanzen: %@", self.beaconDictionary);
//Checking if more than 2 Beacons alife.
if([activeBeaconIds count] > 2)
{
for (int i=0; i<[activeBeaconIds count] ; i++)
{
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:[self.beaconDictionary objectForKey:activeBeaconIds[i]]];
NSInteger maxCount = 0;
NSNumber *maxNumber;
for (NSNumber *number in countedSet) {
if ([countedSet countForObject:number] > maxCount) {
maxCount = [countedSet countForObject:number];
maxNumber = number;
if (!self.radiusDict) {
self.radiusDict = [NSMutableDictionary dictionary];
}
[self.radiusDict setObject:maxNumber forKey:activeBeaconIds[i]];
}
}
}
NSLog(@"Mein DIC= %@", self.radiusDict[@"42634-3111"]);
float BeaconDistanceOne = [self.radiusDict[@"9576-49222"] floatValue];
float BeaconDistanceTwo = [self.radiusDict[@"42634-3111"] floatValue];
float BeaconDistanceThree = [self.radiusDict[@"30219-54563"] floatValue];
//BeaconID = 9576-49222
float beaconOneCoordinateX = 0;
float beaconOneCoordinateY = 0;
//BeaconID = 42634-3111
float beaconTwoCoordinateX = 320;
float beaconTwoCoordinateY = 0;
// BeaconID = 30219-54563
float beaconThreeCoordinateX = 160;
float beaconThreeCoordinateY = 480;
//Calculating Distances with Factor (cm to Pixel) *1 = Factor cm to Pixel
BeaconDistanceOne = (BeaconDistanceOne * 100) *1;
BeaconDistanceTwo = (BeaconDistanceTwo * 100) *1;
BeaconDistanceThree = (BeaconDistanceThree * 100) *1;
//Calculating Delta Alpha Beta
float Delta = 4 * ((beaconOneCoordinateX - beaconTwoCoordinateX) * (beaconOneCoordinateY - beaconThreeCoordinateY) - (beaconOneCoordinateX - beaconThreeCoordinateX) * (beaconOneCoordinateY - beaconTwoCoordinateY));
float Alpha = (BeaconDistanceTwo * BeaconDistanceTwo) - (BeaconDistanceOne * BeaconDistanceOne) - (beaconTwoCoordinateX * beaconTwoCoordinateX) + (beaconOneCoordinateX * beaconOneCoordinateX) - (beaconTwoCoordinateY * beaconTwoCoordinateY) + (beaconOneCoordinateY * beaconOneCoordinateY);
float Beta = (BeaconDistanceThree * BeaconDistanceThree) - (BeaconDistanceOne * BeaconDistanceOne) - (beaconThreeCoordinateX * beaconThreeCoordinateX) + (beaconOneCoordinateX * beaconOneCoordinateX) - (beaconThreeCoordinateY * beaconThreeCoordinateY) + (beaconOneCoordinateY * beaconOneCoordinateY);
//Real Calculating the Position (Triletaration
float PositionX = (1/Delta) * (2 * Alpha * (beaconOneCoordinateY - beaconThreeCoordinateY) - 2 * Beta * (beaconOneCoordinateY - beaconTwoCoordinateY));
float PositionY = (1/Delta) * (2 * Beta * (beaconOneCoordinateX - beaconTwoCoordinateX) - 2 * Alpha * (beaconOneCoordinateX - beaconThreeCoordinateX));
NSLog(@"PositionX = %f", PositionX);
NSLog(@"PositionX = %f", PositionY);
NSString* PositionStringX = [NSString stringWithFormat:
@"%f",
PositionX];
NSString* PositionStringY = [NSString stringWithFormat:
@"%f",
PositionY];
self.beaconDistanceOne.text = PositionStringX;
self.distanceLabel.text = PositionStringY;
self.position = CGPointMake(PositionX, PositionY);
self.ball.center = self.position;
/*
Deleting Values from Dictionary
*/
[self.beaconDictionary removeAllObjects];
NSLog(@"Dictionary Release");
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end