-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScannerViewController.m
More file actions
263 lines (217 loc) · 8.07 KB
/
ScannerViewController.m
File metadata and controls
263 lines (217 loc) · 8.07 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
//
// ScannerViewController.m
// nRF Toolbox
//
// Created by Aleksander Nowakowski on 16/12/13.
// Copyright (c) 2013 Nordic Semiconductor. All rights reserved.
//
#import "ViewController.h"
#import "ScannerTableViewCell.h"
#import "ScannedPeripheral.h"
#import "UARTPeripheralDelegate.h"
@interface ScannerViewController ()
@property (retain, nonatomic) NSMutableArray *peripherals;
@property (retain, nonatomic) NSTimer *timer;
@property (strong, nonatomic) UARTPeripheral *currentPeripheral;
- (void)timerFireMethod:(NSTimer *)timer;
@end
@implementation ScannerViewController
@synthesize devicesTable;
@synthesize filterUUID;
@synthesize peripherals;
@synthesize timer;
@synthesize closeButton;
@synthesize m_parent;
@synthesize m_connector;
@synthesize currentPeripheral;
@synthesize m_preferences;
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil parent:(id<CBCentralManagerDelegate>) parent connector:(CBConnector *)connector prefManager:(PreferenceManager *)preferences {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.filterUUID = nil;
self.m_parent = parent;
self.m_connector = connector;
connector.m_delegate = self;
self.m_preferences = preferences;
}
return self;
}
- (void) viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.peripherals = [[NSMutableArray alloc] init];
self.devicesTable.delegate = self;
self.devicesTable.dataSource = self;
}
- (void) viewWillAppear:(BOOL)animated {
if (self.m_connector.m_cm.state == CBCentralManagerStatePoweredOn) {
[self scanForPeripherals:YES];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[self scanForPeripherals:NO];
}
- (void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction) didCancelClicked:(id)sender {
[self.m_connector stopScan];
ViewController *vc = (ViewController *)self.m_parent;
[vc cancelledScan];
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark Central Manager delegate methods
-(void) centralManagerDidUpdateState:(CBCentralManager *)central {
if (self.m_parent != nil)
[self.m_parent centralManagerDidUpdateState:central];
}
- (int) scanForPeripherals:(BOOL)enable {
if (![self.m_connector poweredOn]) return -1;
// Scanner uses other queue to send events. We must edit UI in the main queue
dispatch_async(dispatch_get_main_queue(), ^{
if (enable) {
[self.m_connector startScan];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
}
else {
[timer invalidate];
timer = nil;
[self.m_connector stopScan];
}
});
return 0;
}
-(void)timerFireMethod:(NSTimer *)timer {
[devicesTable reloadData];
}
-(void)switchViews:(UIViewController *)parent_vc {
[parent_vc presentViewController:self animated:YES completion:nil];
}
-(BOOL)hasValidName:(CBPeripheral *)peripheral {
BOOL named = NO;
if (peripheral.name != nil && peripheral.name.length > 0 ) {
named = ([peripheral.name isEqualToString:@"No name"] == NO) &&
([peripheral.name isEqualToString:@"Apple TV"] == NO);
}
return named;
}
-(BOOL)peripheralMatchesAutoJoin {
BOOL matched = NO;
ViewController *vc = (ViewController *)self.m_parent;
NSString *name = [vc.m_preferences getPreference:@"DEFAULT_JOIN"];
@synchronized(self) {
for(int i=0;name != nil && name.length > 0 && i<self.peripherals.count && !matched;++i) {
ScannedPeripheral *peripheral = (ScannedPeripheral *)[self.peripherals objectAtIndex:i];
if ([name isEqualToString:peripheral.name]) {
[vc.m_preferences setPreference:@"DEFAULT_JOIN_INDEX" withIntValue:i];
matched = YES;
}
}
}
return matched;
}
-(void)bindToPeripheral:(int)index {
id<UARTPeripheralDelegate> parent = (id<UARTPeripheralDelegate>)self.m_parent;
[self dismissViewControllerAnimated:NO completion:nil];
[parent beginConnection:index UARTDelegate:parent withPeripherals:peripherals];
}
-(int)getAutoJoinIndex {
ViewController *vc = (ViewController *)self.m_parent;
return [vc.m_preferences getIntPreference:@"DEFAULT_JOIN_INDEX"];
}
-(void)reset {
[peripherals removeAllObjects];
ViewController *vc = (ViewController *)self.m_parent;
[vc disconnect];
if (self.m_connector.m_cm.state == CBCentralManagerStatePoweredOn) {
[self scanForPeripherals:YES];
}
}
#pragma mark CBCentralManagerDelegate methods
- (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
// Scanner uses other queue to send events. We must edit UI in the main queue
// Add the sensor to the list and reload deta set
ScannedPeripheral *sensor = [[ScannedPeripheral alloc] initWithPeripheral:peripheral rssi:RSSI.intValue advertisement:advertisementData];
if (![peripherals containsObject:sensor])
{
@synchronized(self) {
if ([self hasValidName:peripheral])
[peripherals addObject:sensor];
}
}
else
{
@synchronized(self) {
sensor = [peripherals objectAtIndex:[peripherals indexOfObject:sensor]];
sensor.advertisements = advertisementData;
sensor.RSSI = RSSI.intValue;
}
}
// if we find an auto-join defaulted, go ahead and bind
if ([self peripheralMatchesAutoJoin]) {
@synchronized(self) {
[self bindToPeripheral:[self getAutoJoinIndex]];
}
}
}
- (void) centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
// do nothing
}
- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
// do nothing
;
}
- (void) centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
// do nothing
;
}
#pragma mark UARTPeripheralDelegate methods
- (void) didReceiveData:(NSString *) string {
// do nothing
;
}
#pragma mark Table View delegate methods
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self bindToPeripheral:(int)indexPath.row];
}
#pragma mark Table View Data Source delegate methods
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return peripherals.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ScannerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"ScannerCellView" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.preferences = self.m_preferences;
cell.indexPath = indexPath;
}
// Update sensor name
if (peripherals != nil && peripherals.count > indexPath.row) {
@synchronized(self) {
ScannedPeripheral *peripheral = [peripherals objectAtIndex:indexPath.row];
cell.title.text = [peripheral name];
[cell setAutoJoinStatus];
// Update RSSI indicator
int RSSI = peripheral.RSSI;
UIImage* image;
if (RSSI < -90) {
image = [UIImage imageNamed: @"Signal_0"];
}
else if (RSSI < -70) {
image = [UIImage imageNamed: @"Signal_1"];
}
else if (RSSI < -50) {
image = [UIImage imageNamed: @"Signal_2"];
}
else {
image = [UIImage imageNamed: @"Signal_3"];
}
cell.signal.image = image;
}
}
return cell;
}
@end