Last active
June 17, 2019 09:40
-
-
Save lindskogen/da1b991902a52400b6a83a682850863a to your computer and use it in GitHub Desktop.
Sketch of a BindableObject for finding friends by local PTP
This file contains 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
// | |
// BluetoothCentral.swift | |
// | |
// Created by Johan Lindskogen on 2019-06-16. | |
// Copyright © 2019 Johan Lindskogen. All rights reserved. | |
// | |
import SwiftUI | |
import CoreBluetooth | |
import Combine | |
let serviceUUID = CBUUID(string: "99EE9B56-C16C-40F7-9449-612684B6EBAE") | |
class BluetoothCentral: NSObject, BindableObject, CBPeripheralManagerDelegate, CBCentralManagerDelegate { | |
let didChange = PassthroughSubject<Void, Never>() | |
var central: CBCentralManager | |
var peripheral: CBPeripheralManager | |
var clients : Set<CBPeripheral> = [] { | |
didSet { | |
didChange.send(()) | |
} | |
} | |
override init() { | |
self.central = CBCentralManager() | |
self.peripheral = CBPeripheralManager() | |
} | |
func start() { | |
central.delegate = self | |
peripheral.delegate = self | |
let myCharacteristic = CBMutableCharacteristic(type: serviceUUID, properties: [.read, .notifyEncryptionRequired], value: nil, permissions: CBAttributePermissions.readEncryptionRequired) | |
let myService = CBMutableService(type: serviceUUID, primary: true) | |
myService.characteristics = [myCharacteristic] | |
peripheral.add(myService) | |
central.scanForPeripherals(withServices: [serviceUUID], options: nil) | |
peripheral.startAdvertising([CBAdvertisementDataServiceUUIDsKey: [serviceUUID]]) | |
} | |
func centralManagerDidUpdateState(_ central: CBCentralManager) { | |
} | |
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { | |
self.clients.insert(peripheral) | |
} | |
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { | |
} | |
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) { | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment