// // ViewController.m // BeerBox // // Created by Lukas on 24/07/14. // Copyright (c) 2014 LBSFilm. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize btControl, box, status, connected; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. btControl = [[SerialGATT alloc] init]; [btControl setup]; btControl.delegate = self; [_dataEntry setDelegate:self]; [_fileEntry setDelegate:self]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //############ Connection Handling - (IBAction)connect:(id)sender { [btControl findHMSoftPeripherals:8]; [NSTimer scheduledTimerWithTimeInterval:8 target:self selector:@selector(scanTimer:) userInfo:nil repeats:NO]; } -(void) scanTimer:(NSTimer *)timer { if(box == nil) [[[UIAlertView alloc] initWithTitle:@"Error" message:@"BeerBox not found!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] show]; } -(void) peripheralFound:(CBPeripheral *)peripheral { if([peripheral.name isEqual: @"LBsKeys"]) { box = peripheral; [btControl connect:box]; } } -(void) serialGATTCharValueUpdated:(NSString *)UUID value:(NSData *)data { NSString *retstring = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; //[[[UIAlertView alloc] initWithTitle:@"RCV!" message:retstring delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] show]; //parseValue here _output.text = [_output.text stringByAppendingString:retstring]; /* NSData *retdata = [retstring dataUsingEncoding:[NSString defaultCStringEncoding]]; NSError *e = nil; NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: retdata options: NSJSONReadingMutableContainers error: &e]; if (!jsonArray) { NSLog(@"Error parsing JSON: %@", e); } else { for(NSDictionary *item in jsonArray) { NSLog(@"Item: %@", item); } } */ } - (IBAction)clearOutput:(id)sender{ _output.text = @""; } - (IBAction)listDrinks:(id)sender { if(self.connected){ //NSString *sendString = [NSString stringWithFormat:@"!%@;",_nameEntry.text]; [self sendString:@"#1;"]; }else{ [[[UIAlertView alloc] initWithTitle:@"Error" message:@"BeerBox not connected!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] show]; } } - (IBAction)listUsers:(id)sender { if(self.connected){ //NSString *sendString = [NSString stringWithFormat:@"!%@;",_nameEntry.text]; [self sendString:@"#2;"]; }else{ [[[UIAlertView alloc] initWithTitle:@"Error" message:@"BeerBox not connected!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] show]; } /* UITableViewController* tab =[self.storyboard instantiateViewControllerWithIdentifier:@"tableView"]; //tab.anotherString = self.originalString; [tab.tableView setDelegate:self]; [tab.tableView setDataSource:self]; [self presentViewController:tab animated:YES completion:nil]; */ } - (IBAction)startScan:(id)sender{ if(self.connected){ //NSString *sendString = [NSString stringWithFormat:@"!%@;",_nameEntry.text]; [self sendString:@"!3;"]; }else{ [[[UIAlertView alloc] initWithTitle:@"Error" message:@"BeerBox not connected!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] show]; } } - (IBAction)getUser:(id)sender { NSString *sendString = [NSString stringWithFormat:@"!2%@;",_fileEntry.text]; [self sendString:sendString]; } - (IBAction)createUser:(id)sender { NSString *sendString = [NSString stringWithFormat:@"!1%@;",_dataEntry.text]; [self sendString:sendString]; } - (IBAction)removeUser:(id)sender { NSString *sendString = [NSString stringWithFormat:@"!6%@;",_fileEntry.text]; [self sendString:sendString]; } - (IBAction)setCredits:(id)sender { NSString *sendString = [NSString stringWithFormat:@"!7%@;%@;",_fileEntry.text,_dataEntry.text]; [self sendString:sendString]; } - (IBAction)setName:(id)sender { NSString *sendString = [NSString stringWithFormat:@"!8%@;%@;",_fileEntry.text,_dataEntry.text]; [self sendString:sendString]; } - (IBAction)revokeRFID:(id)sender { NSString *sendString = [NSString stringWithFormat:@"!5%@;",_fileEntry.text]; [self sendString:sendString]; } - (IBAction)revokeFinger:(id)sender { NSString *sendString = [NSString stringWithFormat:@"!4%@;",_fileEntry.text]; [self sendString:sendString]; } - (void) sendString:(NSString*)sendString{ NSData *data = [sendString dataUsingEncoding:[NSString defaultCStringEncoding]]; if(data.length > 20) { int i = 0; int nIndex = 0; while ((i + 1) * 20 <= data.length) { nIndex = i * 20; if(nIndex > 0) nIndex--; NSData *dataSend = [data subdataWithRange:NSMakeRange(nIndex, 20)]; [btControl write:box data:dataSend]; i++; } i = data.length % 20; if(i > 0) { NSData *dataSend = [data subdataWithRange:NSMakeRange(data.length - i, i)]; [btControl write:box data:dataSend]; } }else { //NSData *data = [MsgToArduino.text dataUsingEncoding:[NSString defaultCStringEncoding]]; [btControl write:box data:data]; } } /* - (IBAction)revokeRFID:(id)sender { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Revoke RFID" message:@"Enter an ID" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; [alert show]; [alert setDelegate:self]; [[alert textFieldAtIndex:0] setDelegate:self]; [[alert textFieldAtIndex:0] resignFirstResponder]; [[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypePhonePad]; [[alert textFieldAtIndex:0] becomeFirstResponder]; } */ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ // The user created a new item, add it NSLog(@"Recall!"); if (buttonIndex == 1) { // Get the input text NSString *newItem = [[alertView textFieldAtIndex:0] text]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Number" message:newItem delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:@"Add", nil]; [alert show]; } } -(void)setConnect { [[[UIAlertView alloc] initWithTitle:@"Found!" message:@"BeerBox found!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; status.text=@"Connected!"; self.connected = true; } -(void)setDisconnect { [[[UIAlertView alloc] initWithTitle:@"Lost!" message:@"BeerBox lost :-(" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; status.text=@"not Connected"; self.connected = false; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; } //####### Table View stuff - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *protoID = @"userCell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:protoID]; cell.textLabel.text = @"Lol"; return cell; } @end