Add Read from Characteristic at Peripheral

This commit is contained in:
Sebastian Lenzlinger 2023-07-13 18:27:58 +02:00
parent 52c6083967
commit 8d7d6dd8ea
4 changed files with 65 additions and 8 deletions

View File

@ -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;

View File

@ -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)
}
}
}

View File

@ -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) {
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(data, for: peripheral., type: .withoutResponse)
peripheral.writeValue(messageData, for: characteristic, type: .withResponse)
}
}
}
}

View File

@ -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)
}