diff options
| author | Tim Redfern <tim@Adaptics.local> | 2013-09-18 17:28:16 +0100 |
|---|---|---|
| committer | Tim Redfern <tim@Adaptics.local> | 2013-09-18 17:28:16 +0100 |
| commit | 31b03c8a1234b966ec56f5da316d93a9259c5d71 (patch) | |
| tree | 765de64c5cb766703beac87bdccf61a739cd3be1 /sampleclient/ScanController.m | |
initial commit
Diffstat (limited to 'sampleclient/ScanController.m')
| -rwxr-xr-x | sampleclient/ScanController.m | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/sampleclient/ScanController.m b/sampleclient/ScanController.m new file mode 100755 index 0000000..7d59c2e --- /dev/null +++ b/sampleclient/ScanController.m @@ -0,0 +1,158 @@ +// +// ScanController.m +// sampleterm +// +// Created by Michael Testa on 11/1/12. +// Copyright (c) Blueradios, Inc. All rights reserved. +// + +#import "ScanController.h" + +@implementation ScanController + +@synthesize deviceTableView = _deviceTableView; + +- (void)didReceiveMemoryWarning +{ + [super didReceiveMemoryWarning]; +} + +#pragma mark - View lifecycle + +- (void)viewDidLoad +{ + [super viewDidLoad]; + _peripherals = [NSMutableArray new]; + self.navigationItem.title = @"Select Device"; + [self disableButton:_scanButton]; + [AppDelegate app].cbCentral = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; +} + +- (void)viewDidUnload +{ + [super viewDidUnload]; +} + +- (void)viewWillAppear:(BOOL)animated +{ + [AppDelegate app].cbCentral.delegate = self; + [super viewWillAppear:animated]; +} + +- (void)viewDidAppear:(BOOL)animated +{ + [super viewDidAppear:animated]; +} + +- (void)viewWillDisappear:(BOOL)animated +{ + [super viewWillDisappear:animated]; +} + +- (void)viewDidDisappear:(BOOL)animated +{ + [super viewDidDisappear:animated]; +} + +#pragma mark - Table options + +//********************************************************************************************************************************************************** +//Table Options +//********************************************************************************************************************************************************** +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { + return 1; +} + +- (NSInteger)tableView:(UITableView *)TableView numberOfRowsInSection:(NSInteger)section { + return _peripherals.count; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + + static NSString *CellIdentifier = @"Cell"; + + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; + if (cell == nil) { + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; + } + // Configure the cell. + + CBPeripheral *peripheral = [_peripherals objectAtIndex:indexPath.row]; + + cell.textLabel.text = peripheral.name; + cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; + + return cell; +} + +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { + [self performSegueWithIdentifier:@"cellSegue" sender:self]; +} + +- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ + if ([[segue identifier] isEqualToString:@"cellSegue"]) { + [self stopScanButton:nil]; + NSIndexPath *indexPath = [self.deviceTableView indexPathForSelectedRow]; + + [AppDelegate app].activePeripheral = [_peripherals objectAtIndex:indexPath.row]; + [self.deviceTableView deselectRowAtIndexPath:indexPath animated:YES]; + } +} +- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {return NO;} + +#pragma mark - UI + +- (void) startScanButton:(id)sender { + [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; + [_peripherals removeAllObjects]; + [self.deviceTableView reloadData]; + [self disableButton:_scanButton]; + [[AppDelegate app].cbCentral scanForPeripheralsWithServices:[NSArray arrayWithObject:[Waydio brspServiceUUID]] options:nil]; +} + +- (void) stopScanButton:(id)sender { + [[AppDelegate app].cbCentral stopScan]; + [self enableButton:_scanButton]; + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; +} + +- (void)enableButton:(UIButton*)butt { + butt.enabled = YES; + butt.alpha = 1.0; +} + +- (void)disableButton:(UIButton*)butt { + butt.enabled = NO; + butt.alpha = 0.5; +} + +#pragma mark - CBCentralManagerDelegate + +- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { +} +- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { +} +- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { +} +- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { + if (![_peripherals containsObject:peripheral]) { + [_peripherals addObject:peripheral]; + [self.deviceTableView reloadData]; + } +} +-(void)retrieveConnectedPeripherals { +} + +- (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripheralslist { +} + +- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals { +} + +- (void)centralManagerDidUpdateState:(CBCentralManager *)central { + printf("Status of CoreBluetooth central manager changed %d \r\n",central.state); + if (central.state==CBCentralManagerStatePoweredOn) { + [self enableButton:_scanButton]; + } +} +@end |
