Last active
December 24, 2018 01:21
-
-
Save paulw11/1c893a119b7cfaa5bd2a9461097e3ef4 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
// | |
// BTConnectionHandler.swift | |
// AdTableTest | |
// | |
// Created by Paul Wilkinson on 24/06/2016. | |
// Copyright © 2016 Paul Wilkinson. All rights reserved. | |
// | |
import Foundation | |
import CoreBluetooth | |
class BTConnectionHandler: NSObject, CBPeripheralManagerDelegate { | |
var cbManager: CBPeripheralManager! | |
var serialService: CBMutableService! | |
var advertisementData: [String: AnyObject]! | |
var tx: CBMutableCharacteristic! | |
var rx: CBMutableCharacteristic! | |
let TX_UUID = CBUUID(string: "68A36B42-DE0D-452E-9A5B-E2655CB73163") | |
let TX_PROPERTIES: CBCharacteristicProperties = [.read, .notify] | |
let TX_PERMISSIONS: CBAttributePermissions = .readable | |
let RX_UUID = CBUUID(string: "3B66D024-2336-4F22-A980-8095F4898C42") | |
let RX_PROPERTIES: CBCharacteristicProperties = .write | |
let RX_PERMISSIONS: CBAttributePermissions = .writeable | |
let SERIAL_UUID = CBUUID(string: "81F02411-BAD8-4367-8EE0-710FFA610608") | |
var receiveQueue = [NSData](); | |
override init() { | |
super.init() | |
self.cbManager = CBPeripheralManager(delegate: self, queue: nil) | |
// cbManager.startAdvertising(ADVERTISEMENT_DATA) | |
self.serialService = CBMutableService(type: SERIAL_UUID, primary: true) | |
self.tx = CBMutableCharacteristic(type: TX_UUID, properties: TX_PROPERTIES, value: nil, permissions: TX_PERMISSIONS) | |
self.rx = CBMutableCharacteristic(type: RX_UUID, properties: RX_PROPERTIES, value: nil, permissions: RX_PERMISSIONS) | |
self.serialService.characteristics = [tx,rx] | |
} | |
// CENTRAL read from TX | |
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) { | |
print("Read request received from central device") | |
request.value = tx.value | |
self.cbManager.respond(to: request, withResult: .success) | |
} | |
// CENTRAL write to RX | |
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) { | |
print("Write request(s) received from central device") | |
for request in requests { | |
if let value = request.value { | |
DispatchQueue.global(attributes: .qosUtility).sync { ()-> Void in | |
self.receiveQueue.append(value) | |
} | |
} | |
self.cbManager.respond(to: request, withResult: .success) | |
} | |
} | |
func startAdvertising() { | |
self.advertisementData = [CBAdvertisementDataLocalNameKey: "serialThingy", CBAdvertisementDataServiceUUIDsKey: [SERIAL_UUID]] | |
self.cbManager.add(self.serialService) | |
self.cbManager.startAdvertising(advertisementData) | |
} | |
func stopAdvertising() { | |
self.cbManager.remove(self.serialService) | |
self.cbManager.stopAdvertising() | |
} | |
// Print state of peripheral manager as it changes | |
func peripheralManagerDidUpdateState(_ peripheral:CBPeripheralManager) { | |
switch peripheral.state { | |
case .poweredOn: | |
print("Bluetooth is powered on") | |
startAdvertising() | |
case .poweredOff: | |
stopAdvertising() | |
default: | |
print("Bluetooth state is \(peripheral.state.rawValue)") | |
} | |
} | |
func getReceivedData() -> NSData? { | |
var returnData: NSData? = nil | |
DispatchQueue.global(attributes: .qosUtility).sync { ()-> Void in | |
if (self.receiveQueue.count > 0) { | |
returnData = self.receiveQueue.removeFirst() | |
} | |
} | |
return returnData | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment