Skip to content

Instantly share code, notes, and snippets.

@stephen
Created December 31, 2013 23:37
Show Gist options
  • Save stephen/8203356 to your computer and use it in GitHub Desktop.
Save stephen/8203356 to your computer and use it in GitHub Desktop.
playing with BLE + Misfit Shine
//
// sandboxAppDelegate.m
// Sandbox
//
// Created by Stephen Wan on 12/31/13.
// Copyright (c) 2013 Stephen Wan. All rights reserved.
//
#import "sandboxAppDelegate.h"
#import <IOBluetooth/IOBluetooth.h>
@implementation sandboxAppDelegate {
CBCentralManager* manager;
CBPeripheral* shine;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your
NSLog(@"Initializing Core Bluetooth");
manager = [CBCentralManager.alloc initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager*)central
{
if (central.state == CBCentralManagerStatePoweredOn)
{
NSLog(@"CB Scanning for devices");
[central scanForPeripheralsWithServices:nil options:nil];
}
}
- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {
NSLog(@"Connecting to %@", peripheral.name);
NSLog(@"Advertisement");
for (id key in advertisementData) {
NSLog(@"key: %@, value: %@ \n", key, [advertisementData objectForKey:key]);
}
NSLog(@"End Advertisement\n");
shine = peripheral;
[central connectPeripheral:shine options:nil];
}
- (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Peripheral connected: %@", shine.name);
peripheral.delegate = self;
[peripheral discoverServices:nil];
}
- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverServices:(NSError *)error {
for (CBService *service in peripheral.services) {
NSLog(@"Discovered service %@", service);
if (service.UUID)
NSLog(@"Discovering characteristics for service %@ (%@)", service, service.UUID.data);
NSLog(@"\n");
[peripheral discoverCharacteristics:nil forService:service];
}
}
- (void)peripheral:(CBPeripheral *)peripheral
didDiscoverCharacteristicsForService:(CBService *)service
error:(NSError *)error {
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"On service %@, discovered characteristic %@", service.UUID.data, characteristic.UUID.data);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment