Add Read from Characteristic at Peripheral
This commit is contained in:
parent
52c6083967
commit
8d7d6dd8ea
@ -497,7 +497,7 @@
|
|||||||
CODE_SIGN_STYLE = Automatic;
|
CODE_SIGN_STYLE = Automatic;
|
||||||
CURRENT_PROJECT_VERSION = 1;
|
CURRENT_PROJECT_VERSION = 1;
|
||||||
DEVELOPMENT_ASSET_PATHS = "\"RippleChat/Preview Content\"";
|
DEVELOPMENT_ASSET_PATHS = "\"RippleChat/Preview Content\"";
|
||||||
DEVELOPMENT_TEAM = B5S58UWR64;
|
DEVELOPMENT_TEAM = GN2B48NJ47;
|
||||||
ENABLE_PREVIEWS = YES;
|
ENABLE_PREVIEWS = YES;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
INFOPLIST_KEY_NSBluetoothAlwaysUsageDescription = "Allow for bluetooth use";
|
INFOPLIST_KEY_NSBluetoothAlwaysUsageDescription = "Allow for bluetooth use";
|
||||||
@ -512,7 +512,7 @@
|
|||||||
"@executable_path/Frameworks",
|
"@executable_path/Frameworks",
|
||||||
);
|
);
|
||||||
MARKETING_VERSION = 1.0;
|
MARKETING_VERSION = 1.0;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = unibas.inetsec.RippleChat;
|
PRODUCT_BUNDLE_IDENTIFIER = unibas.inetsec.RippleChat1;
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
SWIFT_EMIT_LOC_STRINGS = YES;
|
SWIFT_EMIT_LOC_STRINGS = YES;
|
||||||
SWIFT_VERSION = 5.0;
|
SWIFT_VERSION = 5.0;
|
||||||
@ -528,7 +528,7 @@
|
|||||||
CODE_SIGN_STYLE = Automatic;
|
CODE_SIGN_STYLE = Automatic;
|
||||||
CURRENT_PROJECT_VERSION = 1;
|
CURRENT_PROJECT_VERSION = 1;
|
||||||
DEVELOPMENT_ASSET_PATHS = "\"RippleChat/Preview Content\"";
|
DEVELOPMENT_ASSET_PATHS = "\"RippleChat/Preview Content\"";
|
||||||
DEVELOPMENT_TEAM = B5S58UWR64;
|
DEVELOPMENT_TEAM = GN2B48NJ47;
|
||||||
ENABLE_PREVIEWS = YES;
|
ENABLE_PREVIEWS = YES;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
INFOPLIST_KEY_NSBluetoothAlwaysUsageDescription = "Allow for bluetooth use";
|
INFOPLIST_KEY_NSBluetoothAlwaysUsageDescription = "Allow for bluetooth use";
|
||||||
@ -543,7 +543,7 @@
|
|||||||
"@executable_path/Frameworks",
|
"@executable_path/Frameworks",
|
||||||
);
|
);
|
||||||
MARKETING_VERSION = 1.0;
|
MARKETING_VERSION = 1.0;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = unibas.inetsec.RippleChat;
|
PRODUCT_BUNDLE_IDENTIFIER = unibas.inetsec.RippleChat1;
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
SWIFT_EMIT_LOC_STRINGS = YES;
|
SWIFT_EMIT_LOC_STRINGS = YES;
|
||||||
SWIFT_VERSION = 5.0;
|
SWIFT_VERSION = 5.0;
|
||||||
|
|||||||
@ -51,4 +51,35 @@ extension BluetoothPeripheral: CBPeripheralManagerDelegate {
|
|||||||
print("Started Advertising")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,6 +13,8 @@ class BluetoothController: NSObject, ObservableObject {
|
|||||||
private var peripherals: [CBPeripheral] = []
|
private var peripherals: [CBPeripheral] = []
|
||||||
@Published var peripheralNames: [String] = []
|
@Published var peripheralNames: [String] = []
|
||||||
|
|
||||||
|
var writeCharacteristics: [CBCharacteristic] = []
|
||||||
|
|
||||||
let BLE_SERVICE_UUID = CBUUID(string: "6e400001-7646-4b5b-9a50-71becce51558")
|
let BLE_SERVICE_UUID = CBUUID(string: "6e400001-7646-4b5b-9a50-71becce51558")
|
||||||
let BLE_CHARACTERISTIC_UUID_RX = CBUUID(string: "6e400002-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 {
|
for peripheral in peripherals {
|
||||||
//peripheral.writeValue(data, for: peripheral., type: .withoutResponse)
|
peripheral.writeValue(messageData, for: characteristic, type: .withResponse)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,7 +23,7 @@ struct PeeringView: View {
|
|||||||
do {
|
do {
|
||||||
let WANT_msg = WantMessage(friends: dataStore.friends)
|
let WANT_msg = WantMessage(friends: dataStore.friends)
|
||||||
let encoded_msg = try JSONEncoder().encode(WANT_msg)
|
let encoded_msg = try JSONEncoder().encode(WANT_msg)
|
||||||
|
btController.writeToCharacteristics(message: String(data: encoded_msg, encoding: .utf8)!)
|
||||||
} catch {
|
} catch {
|
||||||
fatalError(error.localizedDescription)
|
fatalError(error.localizedDescription)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user