BleSearchResultTableViewController.m 4.2キロバイト
//
//  BleSearchResultTableViewController.m
//  jacket_ios
//
//  Created by ドラッサル 亜嵐 on 2016/04/27.
//  Copyright © 2016年 ドラッサル 亜嵐. All rights reserved.
//

#import "BleSearchResultTableViewController.h"
#import "BleSearchResultTableViewCell.h"

@interface BleSearchResultTableViewController ()

@end

@implementation BleSearchResultTableViewController

@synthesize ble;
@synthesize BLEDevicesRssi;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.BLEDevicesRssi = [NSMutableArray arrayWithArray:ble.peripheralsRssi];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) connectionTimer:(NSTimer *)timer
{
    [self.BLEDevices removeAllObjects];
    [self.BLEDevicesRssi removeAllObjects];
    [self.BLEDevicesName removeAllObjects];
    
    if (ble.peripherals.count > 0)
    {
        for (int i = 0; i < ble.peripherals.count; i++)
        {
            CBPeripheral *p = [ble.peripherals objectAtIndex:i];
            NSNumber *n = [ble.peripheralsRssi objectAtIndex:i];
            NSString *name = [[ble.peripherals objectAtIndex:i] name];
            
            if (p.identifier.UUIDString != NULL)
            {
                [self.BLEDevices insertObject:p.identifier.UUIDString atIndex:i];
                [self.BLEDevicesRssi insertObject:n atIndex:i];
                
                if (name != nil)
                {
                    [self.BLEDevicesName insertObject:name atIndex:i];
                }
                else
                {
                    [self.BLEDevicesName insertObject:@"RedBear Device" atIndex:i];
                }
            }
            else
            {
                [self.BLEDevices insertObject:@"NULL" atIndex:i];
                [self.BLEDevicesRssi insertObject:@"0" atIndex:i];
                [self.BLEDevicesName insertObject:@"RedBear Device" atIndex:i];
            }
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:@"No BLE Device(s) found."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
    
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.BLEDevices.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *tableIdentifier = @"cell_uuid";
    BleSearchResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    UIFont *newFont = [UIFont fontWithName:@"Arial" size:13.5];
    
    cell.lblUuid.font = newFont;
    cell.lblUuid.text = [self.BLEDevices objectAtIndex:indexPath.row];
    
    newFont = [UIFont fontWithName:@"Arial" size:11.0];
    cell.lblRssi.font = newFont;
    NSMutableString *rssiString = [NSMutableString stringWithFormat:@"RSSI : %@", [self.BLEDevicesRssi objectAtIndex:indexPath.row]];
    cell.lblRssi.text = rssiString;
    
    newFont = [UIFont fontWithName:@"Arial" size:13.0];
    cell.lblName.font = newFont;
    cell.lblName.text = [self.BLEDevicesName objectAtIndex:indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(ble.isConnected) {
        [self.protocol sendDisconnect];
    }
    [ble connectPeripheral:[ble.peripherals objectAtIndex:indexPath.row]];
}

@end