Skip to content

Commit

Permalink
updates for Bluegiga DKBLE112 default thermometer app.
Browse files Browse the repository at this point in the history
  • Loading branch information
timburks committed Sep 10, 2012
1 parent 7020fc8 commit 7f6fe91
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 50 deletions.
4 changes: 4 additions & 0 deletions iOSHealthThermometer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
2267074A15FE3D2E00105A61 /* BLE112_eval_kit_web_pic.png in Resources */ = {isa = PBXBuildFile; fileRef = 2267074915FE3D2E00105A61 /* BLE112_eval_kit_web_pic.png */; };
22A8518B153E8F3E000B4042 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 22A8518A153E8F3E000B4042 /* UIKit.framework */; };
22A8518D153E8F3E000B4042 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 22A8518C153E8F3E000B4042 /* Foundation.framework */; };
22A8518F153E8F3E000B4042 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 22A8518E153E8F3E000B4042 /* CoreGraphics.framework */; };
Expand All @@ -18,6 +19,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
2267074915FE3D2E00105A61 /* BLE112_eval_kit_web_pic.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = BLE112_eval_kit_web_pic.png; sourceTree = "<group>"; };
22A85186153E8F3E000B4042 /* iOSHealthThermometer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = iOSHealthThermometer.app; sourceTree = BUILT_PRODUCTS_DIR; };
22A8518A153E8F3E000B4042 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
22A8518C153E8F3E000B4042 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -80,6 +82,7 @@
children = (
22A85199153E8F3E000B4042 /* AppDelegate.h */,
22A8519A153E8F3E000B4042 /* AppDelegate.m */,
2267074915FE3D2E00105A61 /* BLE112_eval_kit_web_pic.png */,
22A851AB153E964A000B4042 /* ThermometerViewController.h */,
22A851AC153E964A000B4042 /* ThermometerViewController.m */,
22A85191153E8F3E000B4042 /* Supporting Files */,
Expand Down Expand Up @@ -150,6 +153,7 @@
buildActionMask = 2147483647;
files = (
22A85195153E8F3E000B4042 /* InfoPlist.strings in Resources */,
2267074A15FE3D2E00105A61 /* BLE112_eval_kit_web_pic.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
18 changes: 16 additions & 2 deletions iOSHealthThermometer/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,30 @@
#import "AppDelegate.h"
#import "ThermometerViewController.h"

@interface AppDelegate ()
@property (nonatomic, strong) ThermometerViewController *thermometerViewController;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"Launching");
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];

self.window.rootViewController = [[ThermometerViewController alloc] init];
self.window.rootViewController = self.thermometerViewController = [[ThermometerViewController alloc] init];
return YES;
}

- (void) applicationWillResignActive:(UIApplication *)application
{
[self.thermometerViewController disconnect];
}

- (void) applicationDidBecomeActive:(UIApplication *)application
{
[self.thermometerViewController startScan];
}

@end
Binary file added iOSHealthThermometer/BLE112_eval_kit_web_pic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 4 additions & 5 deletions iOSHealthThermometer/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,21 @@
<string>1.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
</array>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
3 changes: 3 additions & 0 deletions iOSHealthThermometer/ThermometerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@

@interface ThermometerViewController : UIViewController

- (void) disconnect;
- (void) startScan;

@end
113 changes: 70 additions & 43 deletions iOSHealthThermometer/ThermometerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ @interface ThermometerViewController () <CBCentralManagerDelegate, CBPeripheralD
@property (nonatomic, strong) UILabel *temperatureLabel;
@property (nonatomic, strong) UILabel *rssiLabel;
@property (nonatomic, strong) UILabel *manufacturerLabel;

@property (nonatomic, assign) BOOL automaticallyReconnect;
@end

@implementation ThermometerViewController
Expand All @@ -36,35 +38,45 @@ - (void) prepareLabel:(UILabel *) label {
label.autoresizingMask = UIViewAutoresizingFlexibleWidth+UIViewAutoresizingFlexibleHeight;
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor redColor];
label.textColor = [UIColor whiteColor];
[self.view addSubview:label];
}

- (void)loadView
{
[super loadView];
self.temperatureLabel = [[UILabel alloc]
initWithFrame:CGRectInset(self.view.bounds,
0.2*self.view.bounds.size.width,
0.45*self.view.bounds.size.height)];
self.temperatureLabel.font = [UIFont boldSystemFontOfSize:self.view.bounds.size.width * 0.10];
self.temperatureLabel.text = @"";

self.view.backgroundColor = [UIColor colorWithWhite:0.2 alpha:1];

UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"BLE112_eval_kit_web_pic.png"]];
[self.view addSubview:imageView];
CGRect imageFrame = imageView.frame;
imageFrame.origin.x = 0.5*(self.view.bounds.size.width - imageFrame.size.width);
imageFrame.origin.y = 0.5*(self.view.bounds.size.height - imageFrame.size.height);
imageView.frame = imageFrame;

CGRect labelFrame = self.view.bounds;
labelFrame.size.height = self.view.bounds.size.height * 0.2;
self.temperatureLabel = [[UILabel alloc] initWithFrame:labelFrame];
self.temperatureLabel.font = [UIFont boldSystemFontOfSize:self.view.bounds.size.height * 0.1];
self.temperatureLabel.text = @"--";
[self prepareLabel:self.temperatureLabel];

self.rssiLabel = [[UILabel alloc]
initWithFrame:CGRectOffset(self.temperatureLabel.frame,
0,
self.temperatureLabel.frame.size.height)];
self.rssiLabel.font = [UIFont boldSystemFontOfSize:self.view.bounds.size.width * 0.05];
self.rssiLabel.text = @"";
self.rssiLabel.font = [UIFont boldSystemFontOfSize:self.view.bounds.size.height * 0.05];
self.rssiLabel.text = @"--";
[self prepareLabel:self.rssiLabel];


self.manufacturerLabel = [[UILabel alloc]
initWithFrame:CGRectOffset(self.rssiLabel.frame,
0,
self.rssiLabel.frame.size.height)];
self.manufacturerLabel.font = [UIFont boldSystemFontOfSize:self.view.bounds.size.width * 0.05];
labelFrame = self.view.bounds;
labelFrame.size.height = self.view.bounds.size.height * 0.1;
labelFrame.origin.y = self.view.bounds.size.height - labelFrame.size.height;
self.manufacturerLabel = [[UILabel alloc] initWithFrame:labelFrame];
self.manufacturerLabel.font = [UIFont boldSystemFontOfSize:self.view.bounds.size.height * 0.05];
self.manufacturerLabel.text = @"Disconnected";
[self prepareLabel:self.manufacturerLabel];
}
Expand Down Expand Up @@ -103,6 +115,8 @@ - (BOOL) isLECapableHardware
// Request CBCentralManager to scan for peripherals
- (void) startScan
{
NSLog(@"startScan");
self.automaticallyReconnect = YES;
NSArray *services = nil; // [NSArray arrayWithObject:[CBUUID UUIDWithString:@"1809"]];
[self.manager scanForPeripheralsWithServices:services options:nil];
}
Expand All @@ -113,6 +127,13 @@ - (void) stopScan
[self.manager stopScan];
}

- (void) disconnect
{
self.automaticallyReconnect = NO;
[self.manager cancelPeripheralConnection:self.peripheral];
}


#pragma mark - CBCentralManager delegate methods

// Invoked when the central manager's state is updated.
Expand All @@ -121,70 +142,76 @@ - (void) centralManagerDidUpdateState:(CBCentralManager *)central
[self isLECapableHardware];
}


- (void) connectToPeripheral:(CBPeripheral *) peripheral {
[self stopScan];
self.peripheral = peripheral;
[self.peripheral setDelegate:self];
NSLog(@"connecting...");
[self.manager connectPeripheral:peripheral
options:[NSDictionary dictionaryWithObject:
[NSNumber numberWithBool:YES]
forKey:
CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}


// Invoked when the central discovers peripheral while scanning.
- (void) centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)aPeripheral
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
NSLog(@"RSSI %@", RSSI);
NSMutableArray *peripherals = [self mutableArrayValueForKey:@"thermometers"];
if(![self.thermometers containsObject:aPeripheral])
[peripherals addObject:aPeripheral];
if(![self.thermometers containsObject:peripheral])
[peripherals addObject:peripheral];

[self connectToPeripheral:peripheral];
// Retrieve already known devices
[self.manager retrievePeripherals:[NSArray arrayWithObject:(id)aPeripheral.UUID]];
// [self.manager retrievePeripherals:[NSArray arrayWithObject:(id)peripheral.UUID]];
}

// Invoked when the central manager retrieves the list of known peripherals.
// Automatically connect to first known peripheral
- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
NSLog(@"Retrieved peripheral: %u - %@", [peripherals count], peripherals);
[self stopScan];
// If there are any known devices, automatically connect to it.
if([peripherals count] >= 1) {
NSLog(@"connecting...");
self.peripheral = [peripherals objectAtIndex:0];
[self.manager connectPeripheral:self.peripheral
options:[NSDictionary dictionaryWithObject:
[NSNumber numberWithBool:YES]
forKey:
CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}
}

// Invoked when a connection is succesfully created with the peripheral.
// Discover available services on the peripheral
- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)aPeripheral
- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"connected");
[aPeripheral setDelegate:self];
[aPeripheral discoverServices:nil];
[peripheral discoverServices:nil];
}

// Invoked when an existing connection with the peripheral is torn down.
// Reset local variables
- (void) centralManager:(CBCentralManager *)central
didDisconnectPeripheral:(CBPeripheral *)aPeripheral
didDisconnectPeripheral:(CBPeripheral *)peripheral
error:(NSError *)error
{
NSLog(@"centralManager:%@ didDisconnectPeripheral:%@ error:%@", central, peripheral, error);
if (self.peripheral) {
[self.peripheral setDelegate:nil];
self.peripheral = nil;
}
self.temperatureLabel.text = @"";
self.rssiLabel.text = @"";
self.temperatureLabel.text = @"--";
self.rssiLabel.text = @"--";
self.manufacturerLabel.text = @"Disconnected";
[self startScan];
if (self.automaticallyReconnect) {
[self startScan];
}
}

// Invoked when the central manager fails to create a connection with the peripheral.
- (void) centralManager:(CBCentralManager *)central
didFailToConnectPeripheral:(CBPeripheral *)aPeripheral
didFailToConnectPeripheral:(CBPeripheral *)peripheral
error:(NSError *)error
{
NSLog(@"Fail to connect to peripheral: %@ with error = %@", aPeripheral, [error localizedDescription]);
NSLog(@"Fail to connect to peripheral: %@ with error = %@", peripheral, [error localizedDescription]);
if (self.peripheral) {
[self.peripheral setDelegate:nil];
self.peripheral = nil;
Expand All @@ -195,24 +222,24 @@ - (void) centralManager:(CBCentralManager *)central

// Invoked upon completion of a -[discoverServices:] request.
// Discover available characteristics on interested services
- (void) peripheral:(CBPeripheral *)aPeripheral didDiscoverServices:(NSError *)error
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
for (CBService *aService in aPeripheral.services) {
for (CBService *aService in peripheral.services) {
NSLog(@"Service found with UUID: %@", aService.UUID);

/* Thermometer Service */
if ([aService.UUID isEqual:[CBUUID UUIDWithString:@"1809"]]) {
[aPeripheral discoverCharacteristics:nil forService:aService];
[peripheral discoverCharacteristics:nil forService:aService];
}

/* Device Information Service */
if ([aService.UUID isEqual:[CBUUID UUIDWithString:@"180A"]]) {
[aPeripheral discoverCharacteristics:nil forService:aService];
[peripheral discoverCharacteristics:nil forService:aService];
}

/* GAP (Generic Access Profile) for Device Name */
if ([aService.UUID isEqual:[CBUUID UUIDWithString:CBUUIDGenericAccessProfileString]]) {
[aPeripheral discoverCharacteristics:nil forService:aService];
[peripheral discoverCharacteristics:nil forService:aService];
}
}
}
Expand Down

0 comments on commit 7f6fe91

Please sign in to comment.