From 8d7d6dd8ea56829ed1ce68b84800e294fb3b0b66 Mon Sep 17 00:00:00 2001 From: Sebastian Lenzlinger <74497638+sebaschi@users.noreply.github.com> Date: Thu, 13 Jul 2023 18:27:58 +0200 Subject: [PATCH] Add Read from Characteristic at Peripheral --- RippleChat.xcodeproj/project.pbxproj | 8 +++---- RippleChat/BTPeripheral.swift | 31 +++++++++++++++++++++++++++ RippleChat/BluetoothController.swift | 32 +++++++++++++++++++++++++--- RippleChat/Views/PeeringView.swift | 2 +- 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/RippleChat.xcodeproj/project.pbxproj b/RippleChat.xcodeproj/project.pbxproj index d70e1d1..3532a1c 100644 --- a/RippleChat.xcodeproj/project.pbxproj +++ b/RippleChat.xcodeproj/project.pbxproj @@ -497,7 +497,7 @@ CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_ASSET_PATHS = "\"RippleChat/Preview Content\""; - DEVELOPMENT_TEAM = B5S58UWR64; + DEVELOPMENT_TEAM = GN2B48NJ47; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_NSBluetoothAlwaysUsageDescription = "Allow for bluetooth use"; @@ -512,7 +512,7 @@ "@executable_path/Frameworks", ); MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = unibas.inetsec.RippleChat; + PRODUCT_BUNDLE_IDENTIFIER = unibas.inetsec.RippleChat1; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_VERSION = 5.0; @@ -528,7 +528,7 @@ CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_ASSET_PATHS = "\"RippleChat/Preview Content\""; - DEVELOPMENT_TEAM = B5S58UWR64; + DEVELOPMENT_TEAM = GN2B48NJ47; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_NSBluetoothAlwaysUsageDescription = "Allow for bluetooth use"; @@ -543,7 +543,7 @@ "@executable_path/Frameworks", ); MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = unibas.inetsec.RippleChat; + PRODUCT_BUNDLE_IDENTIFIER = unibas.inetsec.RippleChat1; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_VERSION = 5.0; diff --git a/RippleChat/BTPeripheral.swift b/RippleChat/BTPeripheral.swift index 913b7e0..4727de8 100644 --- a/RippleChat/BTPeripheral.swift +++ b/RippleChat/BTPeripheral.swift @@ -51,4 +51,35 @@ extension BluetoothPeripheral: CBPeripheralManagerDelegate { print("Started Advertising") } + + func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) { + for request in requests { + if let value = request.value { + // Handle the received data + let receivedData = Data(value) + + // Decode the received JSON string into your data structure + let decoder = JSONDecoder() + do { + let receivedObject = try decoder.decode(String.self, from: receivedData) + // Use the received object to update your app state as needed + print(receivedObject) + } catch { + print("Failed to decode JSON: \(error)") + } + + // If you want to write back to the central +// if let central = request.central { +// let dataToWrite = // some data you want to send back +// let writeType: CBCharacteristicWriteType = // choose .withResponse or .withoutResponse +// central.writeValue(dataToWrite, for: request.characteristic, type: writeType) +// } + + } + + // Respond to the write request + peripheral.respond(to: request, withResult: .success) + } + } + } diff --git a/RippleChat/BluetoothController.swift b/RippleChat/BluetoothController.swift index 27ece4a..20b1b26 100644 --- a/RippleChat/BluetoothController.swift +++ b/RippleChat/BluetoothController.swift @@ -13,6 +13,8 @@ class BluetoothController: NSObject, ObservableObject { private var peripherals: [CBPeripheral] = [] @Published var peripheralNames: [String] = [] + var writeCharacteristics: [CBCharacteristic] = [] + let BLE_SERVICE_UUID = CBUUID(string: "6e400001-7646-4b5b-9a50-71becce51558") let BLE_CHARACTERISTIC_UUID_RX = CBUUID(string: "6e400002-7646-4b5b-9a50-71becce51558") @@ -37,9 +39,33 @@ extension BluetoothController: CBCentralManagerDelegate { } } - func sendWantVector(data: Data) { - for peripheral in peripherals { - //peripheral.writeValue(data, for: peripheral., type: .withoutResponse) + + func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { + guard let characteristics = service.characteristics else { + print("No characteristics found for service \(service.uuid)") + return + } + + for characteristic in characteristics { + if characteristic.properties.contains(.write) || characteristic.properties.contains(.writeWithoutResponse) { + // This characteristic supports writing + writeCharacteristics.append(characteristic) + } + } + } + + func writeToCharacteristics(message: String) { + guard let messageData = message.data(using: .utf8) else { + print("Could not convert message to data.") + return + } + + // Go through + for characteristic in writeCharacteristics { + // Go through connected peripherals and write to their characteristic + for peripheral in peripherals { + peripheral.writeValue(messageData, for: characteristic, type: .withResponse) + } } } } diff --git a/RippleChat/Views/PeeringView.swift b/RippleChat/Views/PeeringView.swift index 5a693e6..d780f22 100644 --- a/RippleChat/Views/PeeringView.swift +++ b/RippleChat/Views/PeeringView.swift @@ -23,7 +23,7 @@ struct PeeringView: View { do { let WANT_msg = WantMessage(friends: dataStore.friends) let encoded_msg = try JSONEncoder().encode(WANT_msg) - + btController.writeToCharacteristics(message: String(data: encoded_msg, encoding: .utf8)!) } catch { fatalError(error.localizedDescription) }