Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathRfduinoManager.m
More file actions
391 lines (297 loc) · 10.6 KB
/
Copy pathRfduinoManager.m
File metadata and controls
391 lines (297 loc) · 10.6 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
Copyright (c) 2013 OpenSourceRF.com. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <objc/message.h>
#import "RFduinoManager.h"
#import "RFduino.h"
static CBUUID *service_uuid;
@interface RFduinoManager()
{
NSTimer *rangeTimer;
int rangeTimerCount;
bool didUpdateDiscoveredRFduinoFlag;
void (^cancelBlock)(void);
bool isScanning;
}
@end
@implementation RFduinoManager
@synthesize delegate;
@synthesize rfduinos;
+ (RFduinoManager *)sharedRFduinoManager
{
static RFduinoManager *rfduinoManager;
if (! rfduinoManager) {
rfduinoManager = [[RFduinoManager alloc] init];
}
return rfduinoManager;
}
- (id)init
{
NSLog(@"init");
self = [super init];
if (self) {
service_uuid = [CBUUID UUIDWithString:(customUUID ? customUUID : @"2220")];
rfduinos = [[NSMutableArray alloc] init];
central = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
return self;
}
- (bool)isBluetoothLESupported
{
if ([central state] == CBCentralManagerStatePoweredOn)
return YES;
NSString *message;
switch ([central state])
{
case CBCentralManagerStateUnsupported:
message = @"This hardware doesn't support Bluetooth Low Energy.";
break;
case CBCentralManagerStateUnauthorized:
message = @"This app is not authorized to use Bluetooth Low Energy.";
break;
case CBCentralManagerStatePoweredOff:
message = @"Bluetooth is currently powered off.";
break;
case CBCentralManagerStateUnknown:
// fall through
default:
message = @"Bluetooth state is unknown.";
}
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bluetooth LE Support"
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
#endif
return NO;
}
- (void)startRangeTimer
{
rangeTimerCount = 0;
rangeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(rangeTick:)
userInfo:nil
repeats:YES];
}
- (void)stopRangeTimer
{
[rangeTimer invalidate];
}
- (void) rangeTick:(NSTimer*)timer
{
bool update = false;
rangeTimerCount++;
if ((rangeTimerCount % 60) == 0) {
// NSLog(@"restarting scanning");
[central stopScan];
NSDictionary *options = nil;
options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[central scanForPeripheralsWithServices:[NSArray arrayWithObject:service_uuid] options:options];
}
NSDate *date = [NSDate date];
for (RFduino *rfduino in rfduinos) {
if (!rfduino.outOfRange
&& rfduino.lastAdvertisement != NULL
&& [date timeIntervalSinceDate:rfduino.lastAdvertisement] > 2)
{
rfduino.outOfRange = true;
update = true;
}
}
if (update) {
if (didUpdateDiscoveredRFduinoFlag) {
[delegate didUpdateDiscoveredRFduino:nil];
}
}
}
#pragma mark - CentralManagerDelegate methods
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"didConnectPeripheral");
RFduino *rfduino = [self rfduinoForPeripheral:peripheral];
if (rfduino) {
[rfduino connected];
[delegate didConnectRFduino:rfduino];
}
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"didDisconnectPeripheral");
void (^block)(void) = ^{
if ([delegate respondsToSelector:@selector(didDisconnectRFduino:)]) {
RFduino *rfduino = [self rfduinoForPeripheral:peripheral];
if (rfduino) {
[delegate didDisconnectRFduino:rfduino];
}
}
};
if (error.code) {
cancelBlock = block;
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Peripheral Disconnected with Error"
message:error.description
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
#endif
}
else
block();
if (peripheral) {
[peripheral setDelegate:nil];
peripheral = nil;
}
}
- (RFduino *)rfduinoForPeripheral:(CBPeripheral *)peripheral
{
for (RFduino *rfduino in rfduinos) {
if ([peripheral isEqual:rfduino.peripheral]) {
return rfduino;
}
}
return nil;
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
// NSLog(@"didDiscoverPeripheral");
NSString *uuid = NULL;
if (peripheral.UUID) {
// only returned if you have connected to the device before
uuid = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, peripheral.UUID);
} else {
uuid = @"";
}
bool added = false;
RFduino *rfduino = [self rfduinoForPeripheral:peripheral];
if (! rfduino) {
rfduino = [[RFduino alloc] init];
rfduino.rfduinoManager = self;
rfduino.name = peripheral.name;
rfduino.UUID = uuid;
rfduino.peripheral = peripheral;
added = true;
[rfduinos addObject:rfduino];
}
rfduino.advertisementData = nil;
id manufacturerData = [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];
if (manufacturerData) {
const uint8_t *bytes = [manufacturerData bytes];
int len = [manufacturerData length];
// skip manufacturer uuid
NSData *data = [NSData dataWithBytes:bytes+2 length:len-2];
rfduino.advertisementData = data;
}
rfduino.advertisementRSSI = RSSI;
rfduino.advertisementPackets++;
rfduino.lastAdvertisement = [NSDate date];
rfduino.outOfRange = false;
if (added) {
[delegate didDiscoverRFduino:rfduino];
} else {
if (didUpdateDiscoveredRFduinoFlag) {
[delegate didUpdateDiscoveredRFduino:rfduino];
}
}
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"didFailToConnectPeripheral");
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connect Failed"
message:error.description
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
#endif
}
- (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals
{
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)aCentral
{
NSLog(@"central manager state = %d", [central state]);
bool success = [self isBluetoothLESupported];
if (success) {
[self startScan];
}
}
#pragma mark - UIAlertViewDelegate methods
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger) buttonIndex
{
if (buttonIndex == 0) {
cancelBlock();
}
}
#endif
#pragma mark - Rfduino methods
- (bool)isScanning
{
return isScanning;
}
- (void)startScan
{
NSLog(@"startScan");
isScanning = true;
NSDictionary *options = nil;
didUpdateDiscoveredRFduinoFlag = [delegate respondsToSelector:@selector(didUpdateDiscoveredRFduino:)];
if (didUpdateDiscoveredRFduinoFlag) {
options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
}
[rfduinos removeAllObjects];
[central scanForPeripheralsWithServices:[NSArray arrayWithObject:service_uuid] options:options];
if (didUpdateDiscoveredRFduinoFlag) {
[self startRangeTimer];
}
}
- (void)stopScan
{
NSLog(@"stopScan");
if (didUpdateDiscoveredRFduinoFlag) {
[self stopRangeTimer];
}
[central stopScan];
isScanning = false;
}
- (void)connectRFduino:(RFduino *)rfduino
{
NSLog(@"connectRFduino");
[central connectPeripheral:[rfduino peripheral] options:nil];
}
- (void)disconnectRFduino:(RFduino *)rfduino
{
NSLog(@"rfduinoManager disconnectPeripheral");
[central cancelPeripheralConnection:rfduino.peripheral];
}
- (void)loadedServiceRFduino:(id)rfduino
{
if ([delegate respondsToSelector:@selector(didLoadServiceRFduino:)]) {
[delegate didLoadServiceRFduino:rfduino];
}
}
@end
You can’t perform that action at this time.
