Skip to content

Commit

Permalink
Error code fix and code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnchain committed Jul 26, 2013
1 parent 7a69356 commit 32285d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 37 deletions.
42 changes: 6 additions & 36 deletions BLESerial/BLESerialService.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ @interface BLESerialService()<CBCentralManagerDelegate, CBPeripheralDelegate>{
#define DEFAULT_NOTIFY_CHARACTERISTIC_UUID @"FFC2"

NSString *kBLESerialServiceErrorDomain = @"BLESerialServiceError";
const int kBLESerialServiceErrorCodeScanTimeout = 1;

const int kBLESerialServiceErrorCodeTimeout = 1;
const int kBLESerialServiceErrorCodeUserCancled = 2;
const int kBLESerialServiceErrorCodeNotFound = 3;
const int kBLESerialServiceErrorCodePoweredOff = 4;
Expand Down Expand Up @@ -166,7 +167,7 @@ -(void)send:(NSData*)data{
-(void) connectTimerRoutin:(NSTimer *)timer{
NSError *cause = nil;
if(_state != CONNECTED){
cause = [NSError errorWithDomain:kBLESerialServiceErrorDomain code:kBLESerialServiceErrorCodeScanTimeout userInfo:nil];
cause = [NSError errorWithDomain:kBLESerialServiceErrorDomain code:kBLESerialServiceErrorCodeTimeout userInfo:nil];
}
NSLog(@"connect time out");
[self doDisconnect:cause];
Expand Down Expand Up @@ -214,15 +215,6 @@ -(void) callAndClearDisConnectCompleteBlock:(NSError*)error{
self.disconnectCompleteBlock = nil;
}

- (void) discoveryStatePoweredOff
{
NSString *title = @"Bluetooth Power";
NSString *message = @"You must turn on Bluetooth in Settings in order to use LE";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}

-(void) reset{
self.cbNotifyCharacteristic = nil;
self.cbReadWriteCharacteristic = nil;
Expand All @@ -237,66 +229,44 @@ -(void) reset{
// BT State Handler
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
static CBCentralManagerState previousState = -1;

switch (_cbCentralManager.state) {
case CBCentralManagerStatePoweredOff:
{
// BT is not powered on
// connecting... so just disconnect
if(_connectTimer.isValid){
[_connectTimer invalidate];
}
NSError *error = [NSError errorWithDomain:kBLESerialServiceErrorDomain code:kBLESerialServiceErrorCodePoweredOff userInfo:nil];
[self doDisconnect:error];

// Tell user to power ON BT for functionality, but not on first run - the Framework will alert in that instance.
if (previousState != -1) {
[self discoveryStatePoweredOff];
}
break;
}

case CBCentralManagerStateUnauthorized:
{
/* Tell user the app is not allowed. */
// Use denied the bt access for your app
break;
}

case CBCentralManagerStateUnsupported:
case CBCentralManagerStateUnknown:
{
/* Bad news, let's wait for another event. */
// unknow/unsupported states
break;
}

case CBCentralManagerStatePoweredOn:
{
//TODO - start discovery
/*
pendingInit = NO;
[self loadSavedDevices];
[centralManager retrieveConnectedPeripherals];
[discoveryDelegate discoveryDidRefresh];
*/
//TODO - should start connect?
NSLog(@"Bluetooth is powered on");
break;
}

case CBCentralManagerStateResetting:
{
/*
[self clearDevices];
[discoveryDelegate discoveryDidRefresh];
[peripheralDelegate alarmServiceDidReset];
pendingInit = YES;
*/
break;
}
}

previousState = _cbCentralManager.state;
}

// Peripherals found
Expand Down
15 changes: 14 additions & 1 deletion BLESerial/Demo/BLESerialDemo/BLEDemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,20 @@ -(IBAction)onConnect:(id)sender{
BLEDemoSendReceiveViewController *vc = [[[BLEDemoSendReceiveViewController alloc] initWithSerialService:_serialService] autorelease];
[self.navigationController pushViewController:vc animated:YES];
}else{
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Connect failed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];

NSString *title,*message;
if(error.code == kBLESerialServiceErrorCodePoweredOff){
title = @"Bluetooth Power";
message = @"You must turn on Bluetooth in Settings in order to use LE";
}else if(error.code == kBLESerialServiceErrorCodeTimeout){
title = @"Connect timeout";
message = @"Please make sure your BLE device works properly";
}else{
title = @"Error";
message = [NSString stringWithFormat:@"Connect failed with error code: %d",error.code];
}

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
}
}];
Expand Down

0 comments on commit 32285d8

Please sign in to comment.