Created
March 8, 2016 08:18
-
-
Save paulw11/b2b33d7ff4c86818ac4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BTManager.swift | |
// TableTest | |
// | |
// Created by Paul Wilkinson on 8/03/2016. | |
// Copyright © 2016 Paul Wilkinson. All rights reserved. | |
// | |
import Foundation | |
import CoreBluetooth | |
class BTManager: NSObject, CBPeripheralManagerDelegate { | |
var peripheralManager:CBPeripheralManager? | |
var bluetoothServices:CBMutableService? | |
var nameCharacteristic:CBMutableCharacteristic? | |
var myIdentity:String? | |
// UUID for the one peripheral service, declared outside the class: | |
var peripheralServiceUUID = CBUUID(string: "E9D2CF3A-E136-4381-BEAA-A66A8AE21996") | |
// UUID for one characteristic of the service above, declared outside the class: | |
var nameCharacteristicUUID = CBUUID(string: "E9D2CF3A-E136-4381-BEAA-A66A8AE21997") | |
override init() { | |
super.init() | |
peripheralManager = CBPeripheralManager(delegate:self, queue:nil) | |
bluetoothServices = CBMutableService(type: peripheralServiceUUID, primary: true) | |
} | |
func configureUtilityForIdentity(identity:String?) { | |
var characteristicsArray:[CBCharacteristic] = [] | |
myIdentity = identity | |
if (identity != nil) { | |
nameCharacteristic = | |
CBMutableCharacteristic(type: nameCharacteristicUUID, | |
properties: (CBCharacteristicProperties.Read), | |
value: identity!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false), | |
permissions: CBAttributePermissions.Readable) | |
characteristicsArray.append(nameCharacteristic!) | |
} | |
bluetoothServices!.characteristics = characteristicsArray | |
} | |
func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager) { | |
switch (peripheral.state) { | |
case .PoweredOn: | |
print("Current Bluetooth State: PoweredOn") | |
break; | |
case .PoweredOff: | |
print("Current Bluetooth State: PoweredOff") | |
break; | |
case .Resetting: | |
print("Current Bluetooth State: Resetting") | |
break; | |
case .Unauthorized: | |
print("Current Bluetooth State: Unauthorized") | |
case .Unknown: | |
print("Current Bluetooth State: Unknown") | |
break; | |
case .Unsupported: | |
print("Current Bluetooth State: Unsupported") | |
break; | |
} | |
} | |
func publishServices() { | |
if (self.bluetoothServices!.characteristics?.count < 0) { | |
self.configureUtilityForIdentity("testing") | |
} | |
peripheralManager!.addService(self.bluetoothServices!) | |
} | |
func peripheralManager(peripheral: CBPeripheralManager, didAddService service: CBService, error: NSError?) { | |
if (error != nil) { | |
print("PerformerUtility.publishServices() returned error: \(error!.localizedDescription)") | |
print("Providing the reason for failure: \(error!.localizedFailureReason)") | |
} | |
else { | |
peripheralManager!.startAdvertising([CBAdvertisementDataServiceUUIDsKey : [service.UUID]]) | |
} | |
} | |
func peripheralManagerDidStartAdvertising(peripheral: CBPeripheralManager, | |
error: NSError?) { | |
if (error != nil) { | |
print("peripheralManagerDidStartAdvertising encountered an error.") | |
print(error!.localizedDescription) // Error: One or more parameters were invalid | |
print(error!.localizedFailureReason) // Error: nil | |
} | |
print ("Debug: peripheralManagerDidStartAdvertising()") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment