From 32285d862d874c2ffbd4b2a9b681f70f76343f33 Mon Sep 17 00:00:00 2001 From: Shawn Chain Date: Fri, 26 Jul 2013 21:42:15 +0800 Subject: [PATCH] Error code fix and code clean up --- BLESerial/BLESerialService.m | 42 +++---------------- .../BLESerialDemo/BLEDemoViewController.m | 15 ++++++- 2 files changed, 20 insertions(+), 37 deletions(-) diff --git a/BLESerial/BLESerialService.m b/BLESerial/BLESerialService.m index 3820817..4f0d38b 100644 --- a/BLESerial/BLESerialService.m +++ b/BLESerial/BLESerialService.m @@ -52,7 +52,8 @@ @interface BLESerialService(){ #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; @@ -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]; @@ -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; @@ -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 diff --git a/BLESerial/Demo/BLESerialDemo/BLEDemoViewController.m b/BLESerial/Demo/BLESerialDemo/BLEDemoViewController.m index b594897..78b3d7f 100644 --- a/BLESerial/Demo/BLESerialDemo/BLEDemoViewController.m +++ b/BLESerial/Demo/BLESerialDemo/BLEDemoViewController.m @@ -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]; } }];