Fliegl Counter HD iOS
Mindestvoraussetzung Hardware
iPod | iphone | iPad | iPad (mini) |
iPod touch 5th gen | iPhone 4s | iPad 3rd gen | iPad mini 1st gen |
CB - Core Bluetooth Framework
Öffnen Sie in Ihrem XCode Project im Dateibrowser die Projektdatei, wählen sie den Tab "Build Phases". Fügen Sie mittels "+" ein weiteres Framework zum Projekt hinzu, wählen Sie im modalen Dialog das Framework "CoreBluetooth.framework".
CBCentralManager
Um Bluetooth Peripherals zu entdecken und sich mit Ihnen zu verbinden benötigen Sie drei Klassen aus dem CoreBluetooth Framework.
- CBCentralManager
- CBPeripheral
- CBService
- CBCharacteristic
CBCentralManager
Der CBCentralManager, sobald instanziert, gibt aufschluss über den Zustand der Bluetooth Schnittstelle selbst, ermöglicht das entdecken, verbinden und trennen zu und von dem Fliegl Counter HD Beacon.
CBPeripheral
Die Instanzen der Klasse CBPeripheral bilden unsere Schnittstelle zu den Fliegl Counter HD Beacons, repräsentieren diese sozusagen innerhalb der Software. Die Peripheral Instanz ermöglicht Zugriff auf die Services und dessen Characteristicas eines Fliegl Beacons.
Implementierung
Als Beispielimplementierung sehen Sie hier einen UIViewController welcher sowohl das CBCentralManager- als auch das CBPeripheral Delegate Protokoll zeichnet.
ViewController.h
//
// ViewController.h
// Fliegl Counter Example
//
// Created by Johannes Duerr on 15.12.16.
// Copyright © 2016 Fliegl Agrartechnik. All rights reserved.
//
@import CoreBluetooth;
#import
/ Define UUID-Strings for services and characteristics
#define FLIEGL_BEACON_SERVICE_UUID @"C13ABBB0-C497-4C95-8699-01B142AF0C24"
@interface ViewController : UIViewController
{
// CBCentral
CBCentralManager* manager;
// CBPeripheral (This solution is not suited for production!)
CBPeripheral* discoveredPeripheral;
}
@end
ViewController.m
//
// ViewController.m
// Fliegl Counter Example
//
// Created by Johannes Duerr on 15.12.16.
// Copyright © 2016 Fliegl Agrartechnik. All rights reserved.
//
#import "ViewController.h"
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// create manager and peripheral
manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - CBCentral delegate methods
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
// catch and handle ***all*** managers states
switch ([manager state]) {
case CBManagerStatePoweredOff:
NSLog(@"CBCentralManagerStatePoweredOff");
break;
case CBManagerStateResetting:
NSLog(@"CBCentralManagerStateResetting");
break;
case CBManagerStateUnauthorized:
NSLog(@"CBCentralManagerStateUnauthorized");
break;
case CBManagerStateUnknown:
NSLog(@"CBCentralManagerStateUnknown");
break;
case CBManagerStateUnsupported:
NSLog(@"CBCentralManagerStateUnsupported");
break;
case CBManagerStatePoweredOn:
NSLog(@"CBManagerStatePoweredOn");
NSLog(@"Start Scanning for Fliegl Counter HD Beacons...");
CBUUID* beaconServiceUUID = [CBUUID UUIDWithString:FLIEGL_BEACON_SERVICE_UUID];
NSArray* serviceArray = [NSArray arrayWithObjects:beaconServiceUUID,nil];
[manager scanForPeripheralsWithServices:serviceArray options:nil];
break;
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"CentralManager found new peripheral");
NSLog(@"%@", [peripheral description]);
// catch pointer to discovered peripheral
discoveredPeripheral = peripheral;
// stop scanning
[manager stopScan];
// set ourselves as delegate for the discovered peripheral
[discoveredPeripheral setDelegate:self];
// connect to peripheral
[manager connectPeripheral:discoveredPeripheral options:nil];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"CentralManager did connect to peripheral:");
NSLog(@"%@", [peripheral description]);
// Start discovering Services
[peripheral discoverServices:nil];
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"CentralManager did disconnect from peripheral:");
NSLog(@"%@", [peripheral description]);
}
#pragma mark - CBPeripheral delegate methods
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
NSLog(@"\nDid discover Services...");
for (CBService* s in peripheral.services) {
NSLog(@"%@", [NSString stringWithFormat:@"%@", s.UUID.UUIDString]);
[peripheral discoverCharacteristics:nil forService:s];
}
}
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
for (CBCharacteristic *c in service.characteristics) {
NSLog(@"Did discover Characteristic: %@", [NSString stringWithFormat:@"%@", c.UUID.UUIDString]);
// Now you can read / write the value of the characteristic depending on its attributes.
// Call:
// - readValueForCharacteristic: to read its current value
// - writeValueForCharacteristic: to write a new value to the characteristic
// - setNotifyValue:YES|NO to get notifications from the peripheral if its characteristic value did change.
// iBeacon minor characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBB1-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// iBeacon major characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBB2-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// iBeacon uuid characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBB3-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// iBeacon Bluetooth local name characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBB4-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// iBeacon txpower characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBB5-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// Chip temperature characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBB6-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// Beacon button-state changed characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBB7-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// Accelerometer / Inclination characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBB8-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// Active time (seconds) characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBB9-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// Activity count characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBBC-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// Current time (seconds since 1st jan 1970) characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBBB-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// Tank event count characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBBE-C497-4C95-8699-01B142AF0C24"]) {
//[peripheral readValueForCharacteristic:c];
[peripheral setNotifyValue:YES forCharacteristic:c];
}
// Tank (full tanks) count characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBBF-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
// Battery information characteristic
if ([c.UUID.UUIDString isEqualToString:@"C13ABBC0-C497-4C95-8699-01B142AF0C24"]) {
[peripheral readValueForCharacteristic:c];
}
}
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
NSLog(@"\nPeripheral did update the value of characteristic:\n%@\n\nValue: %@\n",characteristic.UUID.UUIDString, [characteristic.value description] );
}
@end
Das gesamte Beispielprojekt steht Ihnen selbstverständlich auch als Download zur Verfügung .