Permalink
Browse files

update MWConnectionControllerDelegate protocol

  • Loading branch information...
1 parent a1f407d commit e5946ebfff1a568a5eadf76d56e0510fe73ff219 @ka010 committed Jun 20, 2012
View
4 MWConnectionController.h
@@ -39,7 +39,7 @@
@protocol MWConnectionControllerDelegate <NSObject>
-(void)connectionControllerDidOpenChannel:(MWConnectionController*)controller;
--(void)connectionControllerDidCloseChannel:(MWConnectionController*)controller;
+-(void)connectionControllerDidCloseChannel:(MWConnectionController*)controller withError:(NSError*)error;
-(void)connectionController:(MWConnectionController*)controller didReceiveData:(NSData*)data;
-
+-(void)connectionController:(MWConnectionController*)controller didFailWithError:(NSError*)error;
@end
View
2 MWKit/MWBTStackController.m
@@ -226,7 +226,7 @@ -(void)rfcommConnectionCreateFailedAtAddress:(bd_addr_t)addr forChannel:(uint16_
-(void) rfcommConnectionClosedForConnectionID:(uint16_t)connectionID {
- [self.delegate performSelector:@selector(connectionControllerDidCloseChannel:) withObject:self];
+ [self.delegate performSelector:@selector(connectionControllerDidCloseChannel:withError:) withObject:self withObject:nil];
connectionID = NULL;
}
View
2 MWKit/MWBluetoothController.m
@@ -384,7 +384,7 @@ - (void)rfcommChannelClosed:(IOBluetoothRFCOMMChannel *)rfcommChannel
- [self.delegate performSelector:@selector(connectionControllerDidCloseChannel:) withObject:self];
+ [self.delegate performSelector:@selector(connectionControllerDidCloseChannel:withError:) withObject:self withObject:nil];
View
2 MWKit/MWCoreBluetoothController.h
@@ -15,4 +15,6 @@
+(MWCoreBluetoothController *) sharedController;
+-(BOOL)isLEAvailable;
+
@end
View
89 MWKit/MWCoreBluetoothController.m
@@ -8,6 +8,7 @@
#import "MWCoreBluetoothController.h"
#import "MWMetaWatch.h"
+#include "crc16ccitt.h"
@interface MWCoreBluetoothController ()
@@ -16,6 +17,9 @@ @interface MWCoreBluetoothController ()
CBService *_service;
CBDescriptor *_descriptor;
+BOOL _pendingInit;
+BOOL _LEAvailable;
+
-(void)_startDiscovery;
@end
@@ -42,6 +46,8 @@ - (id)init
{
self = [super init];
if (self) {
+ _LEAvailable = NO;
+ _pendingInit = YES;
_manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
return self;
@@ -56,23 +62,34 @@ -(void)dealloc {
[super dealloc];
}
+
+#pragma mark -
+
+-(BOOL)isLEAvailable {
+ return _LEAvailable;
+}
+
+
#pragma mark MWConnectionControllerDelegate
-(void)startDiscovery {
if (_device) {
[self closeChannel];
- [self performSelector:@selector(_startDiscovery) withObject:nil afterDelay:2.0];
+ // [self _startDiscovery];
+ [self performSelector:@selector(_startDiscovery) withObject:nil afterDelay:5.0];
return;
}
[self _startDiscovery];
}
-(void)_startDiscovery {
+ [MWCoreBluetoothController cancelPreviousPerformRequestsWithTarget:self selector:@selector(_startDiscovery) object:nil];
+
NSLog(@"%@ did Start Discovery",self);
[_manager retrieveConnectedPeripherals];
-
+
}
-(void)openChannel {
@@ -86,9 +103,6 @@ -(void)closeChannel {
}
}
--(void)sendFrame:(NSData *)frame withLenght:(unsigned char)lenght {
-
-}
-(void)tx:(unsigned char)cmd options:(unsigned char)options data:(unsigned char *)inputData len:(unsigned char)len {
@@ -117,7 +131,7 @@ -(void)tx:(unsigned char)cmd options:(unsigned char)options data:(unsigned char
} logString = [logString stringByAppendingFormat:@"\n"];
[[MWMetaWatch sharedWatch]appendToLog:logString];
-
+
NSLog(@"%@",logString);
NSData *frame = [NSData dataWithBytes:(void*)data length:len+6];
@@ -126,26 +140,67 @@ -(void)tx:(unsigned char)cmd options:(unsigned char)options data:(unsigned char
if ([c.UUID isEqual:[CBUUID UUIDWithString:kMWWatchCharacteristicUUID]]) {
[_device writeValue:frame forCharacteristic:c type:CBCharacteristicWriteWithResponse];
}
-
+
}
}
#pragma mark CBCentralManagerDelegate
-(void)centralManagerDidUpdateState:(CBCentralManager *)central {
- // FIXME: handle device capabilities
+
+ static CBCentralManagerState previousState = -1;
+
+ switch ([central state]) {
+ case CBCentralManagerStatePoweredOff:
+ {
+ /* Tell user to power ON BT for functionality, but not on first run - the Framework will alert in that instance. */
+ if (previousState != -1) {
+ _LEAvailable=NO;
+ }
+ break;
+ }
+
+ case CBCentralManagerStateUnauthorized:
+ {
+ _LEAvailable=NO;
+ break;
+ }
+
+ case CBCentralManagerStateUnknown:
+ {
+ /* Bad news, let's wait for another event. */
+ break;
+ }
+
+ case CBCentralManagerStatePoweredOn:
+ {
+ _pendingInit = NO;
+ _LEAvailable = YES;
+ break;
+ }
+
+ case CBCentralManagerStateResetting:
+ {
+
+ _pendingInit = YES;
+ break;
+ }
+ }
+
+ previousState = [central state];
+
}
-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
NSLog(@"%@",peripherals);
}
-(void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals{
- NSLog(@"%@",peripherals);
+ NSLog(@"connected peripherals %@",peripherals);
if (peripherals.count == 0) {
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
-
+
[_manager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:kMWServiceUUID]] options:options];
}else {
[_device = [peripherals objectAtIndex:0]retain];
@@ -166,8 +221,10 @@ -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPerip
NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObject:boolYES]
forKeys:[NSArray arrayWithObject:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
if (_device) {
+ // do nothing if connected already
return;
}
+
_device = [peripheral retain];
_device.delegate = self;
[central connectPeripheral:_device options:dict];
@@ -180,12 +237,12 @@ -(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeriph
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"didDisconnect: %@ (error: %@)",peripheral,error);
-
+
[_device release];
_device = nil;
- [self.delegate performSelector:@selector(connectionControllerDidCloseChannel:) withObject:self];
-
+ [self.delegate performSelector:@selector(connectionControllerDidCloseChannel:withError:) withObject:self withObject:error];
+
}
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
@@ -223,20 +280,20 @@ -(void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteri
for (CBDescriptor *d in characteristic.descriptors) {
NSLog(@"didDiscoverDescriptors: %@ forCharacteristic: %@",d.UUID, characteristic.UUID);
[_device readValueForDescriptor:d];
-
+
_descriptor = [d retain];
}
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"didUpdateNotificationStateForCharacteristic: %@",characteristic.UUID);
-
+
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
NSLog(@"didUpdateValueForCharacteristic: %@ %@",characteristic.UUID, characteristic.value);
[self.delegate performSelector:@selector(connectionController:didReceiveData:) withObject:self withObject:[characteristic.value copy]];
-
+
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error {
View
2 MWKit/MWSerialPortController.m
@@ -338,7 +338,7 @@ -(void)restartChannel {
-(void)closeChannel {
CloseSerialPort(fileDescriptor);
- [self.delegate performSelector:@selector(connectionControllerDidCloseChannel:) withObject:self];
+ [self.delegate performSelector:@selector(connectionControllerDidCloseChannel:withError:) withObject:self withObject:nil];
}
View
9 MWMetaWatch.m
@@ -114,8 +114,8 @@ -(void)connectionControllerDidOpenChannel:(MWConnectionController *)controller {
}
--(void)connectionControllerDidCloseChannel:(MWConnectionController *)controller {
- NSLog(@"channel Closed");
+-(void)connectionControllerDidCloseChannel:(MWConnectionController *)controller withError:(NSError *)error {
+ NSLog(@"channel Closed (error:%@)",error);
self.logString = [self.logString stringByAppendingFormat:@"** Connection closed. \n"];
if ([self.delegate respondsToSelector:@selector(metawatchDidDisconnect:)]) {
@@ -135,7 +135,10 @@ -(void)connectionController:(MWConnectionController *)controller didReceiveData:
-
+-(void)connectionController:(MWConnectionController *)controller didFailWithError:(NSError *)error {
+ NSLog(@"did encounter error (error:%@)",error);
+ // FIXME: do something useful here
+}

0 comments on commit e5946eb

Please sign in to comment.