Last active
April 22, 2020 09:43
-
-
Save paulw11/c202e452fa54d19254b3adb983874d59 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
// | |
// BLEPeripheral.swift | |
// BLEPublishTest | |
// | |
// Created by Paul Wilkinson on 22/4/20. | |
// Copyright © 2020 Paul Wilkinson. All rights reserved. | |
// | |
import Foundation | |
import CoreBluetooth | |
class BLEPeripheral: NSObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate, CBPeripheralDelegate { | |
static let shared = BLEPeripheral() | |
var peripheralManager: CBPeripheralManager | |
var centralManager: CBCentralManager | |
private let beaconOperationsQueue = DispatchQueue(label: "beacon_operations_queue") | |
private var peripheralName: String? | |
private var connectTarget: CBPeripheral? | |
let kTRANSFER_SERVICE_UUID = "29ada058-c7d6-4ed5-bc7f-1c7b0458b3b8" | |
let kTRANSFER_CHARACTERISTIC_UUID = "91e032f2-c915-47c6-a8d9-6b3bc6c8e73d" | |
override init() { | |
self.peripheralManager = CBPeripheralManager(delegate: nil, queue: beaconOperationsQueue, options: nil) | |
self.centralManager = CBCentralManager() | |
super.init() | |
self.peripheralManager.delegate = self | |
self.centralManager.delegate = self | |
} | |
public func startAdvertising(serviceID: CBUUID, name: String) { | |
let valueData = name.data(using: .utf8) | |
self.peripheralName = name | |
let CustomChar = CBMutableCharacteristic(type: CBUUID(string: kTRANSFER_CHARACTERISTIC_UUID), properties: [.read], value: valueData, permissions: [.readable]) | |
let myService = CBMutableService(type: serviceID, primary: true) | |
myService.characteristics = [CustomChar] | |
peripheralManager.add(myService) | |
peripheralManager.startAdvertising([ | |
CBAdvertisementDataServiceUUIDsKey: [serviceID],CBAdvertisementDataLocalNameKey: peripheralName!]) | |
} | |
func startScan() { | |
self.centralManager.scanForPeripherals(withServices: [CBUUID(string:kTRANSFER_SERVICE_UUID)], options: nil) | |
} | |
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { | |
if peripheral.state == .poweredOn { | |
print("Powered on, start advertising") | |
self.startAdvertising(serviceID: CBUUID(string:self.kTRANSFER_SERVICE_UUID), name: "Test") | |
} | |
} | |
func centralManagerDidUpdateState(_ central: CBCentralManager) { | |
if central.state == .poweredOn { | |
print("Powered on, start scanning") | |
self.startScan() | |
} | |
} | |
public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { | |
print("Discovered \(peripheral)") | |
self.connectTarget = peripheral | |
central.connect(peripheral) | |
} | |
public func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { | |
print("Connected to \(peripheral)") | |
peripheral.delegate = self | |
peripheral.discoverServices([CBUUID(string:kTRANSFER_SERVICE_UUID)]) | |
} | |
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { | |
for service in peripheral.services ?? [] { | |
print(service) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment