// // 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) { } }