BeerBoxiOS/BeerBox/ViewController.m

189 lines
5.0 KiB
Objective-C
Executable File

//
// 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;
- (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;
[_nameEntry setDelegate:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (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 *value = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
[[[UIAlertView alloc] initWithTitle:@"RCV!" message:value delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil] show];
}
- (IBAction)sendMsgToArduino:(id)sender {
NSString *sendString = [NSString stringWithFormat:@"!%@;",_nameEntry.text];
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)removeUser:(id)sender {
}
- (IBAction)listUsers:(id)sender {
}
- (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];
}
}
- (IBAction)revokeFinger:(id)sender {
}
- (IBAction)checkCount:(id)sender {
NSString *sendString = [NSString stringWithFormat:@"!%@;",_nameEntry.text];
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];
}
}
-(void)setConnect
{
[[[UIAlertView alloc] initWithTitle:@"Found!" message:@"BeerBox found!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
status.text=@"Connected!";
}
-(void)setDisconnect
{
[[[UIAlertView alloc] initWithTitle:@"Lost!" message:@"BeerBox lost :-(" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
status.text=@"not Connected";
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
@end