From 52c608396777a48dd0ba25dc843e3c02ba36062d Mon Sep 17 00:00:00 2001 From: "severin.memmishofer" Date: Thu, 13 Jul 2023 17:41:18 +0200 Subject: [PATCH] Minor changes to BT --- RippleChat/BluetoothController.swift | 7 ++++++ RippleChat/File.swift | 32 ++++++++++++++++++++++++++++ RippleChat/Views/PeeringView.swift | 2 -- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 RippleChat/File.swift diff --git a/RippleChat/BluetoothController.swift b/RippleChat/BluetoothController.swift index e2aa63c..27ece4a 100644 --- a/RippleChat/BluetoothController.swift +++ b/RippleChat/BluetoothController.swift @@ -31,8 +31,15 @@ extension BluetoothController: CBCentralManagerDelegate { func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { if !peripherals.contains(peripheral) { + centralManager?.connect(peripheral) self.peripherals.append(peripheral) self.peripheralNames.append(peripheral.name ?? "unnamed device") } } + + func sendWantVector(data: Data) { + for peripheral in peripherals { + //peripheral.writeValue(data, for: peripheral., type: .withoutResponse) + } + } } diff --git a/RippleChat/File.swift b/RippleChat/File.swift new file mode 100644 index 0000000..a25a428 --- /dev/null +++ b/RippleChat/File.swift @@ -0,0 +1,32 @@ +// +// File.swift +// RippleChat +// +// Created by Severin Memmishofer on 13.07.23. +// + +func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { + if let error = error { + print("Error discovering services: \(error.localizedDescription)") + return + } + + for service in peripheral.services ?? [] { + if service.uuid == CBUUID(string: "YourServiceUUIDHere") { + peripheral.discoverCharacteristics([CBUUID(string: "YourCharacteristicUUIDHere")], for: service) + } + } +} + +func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { + if let error = error { + print("Error discovering characteristics: \(error.localizedDescription)") + return + } + + for characteristic in service.characteristics ?? [] { + if characteristic.uuid == CBUUID(string: "YourCharacteristicUUIDHere") { + // You've found your characteristic! + } + } +} diff --git a/RippleChat/Views/PeeringView.swift b/RippleChat/Views/PeeringView.swift index 8319f88..5a693e6 100644 --- a/RippleChat/Views/PeeringView.swift +++ b/RippleChat/Views/PeeringView.swift @@ -24,13 +24,11 @@ struct PeeringView: View { let WANT_msg = WantMessage(friends: dataStore.friends) let encoded_msg = try JSONEncoder().encode(WANT_msg) - } catch { fatalError(error.localizedDescription) } }) { - } .padding() }