I arrange a perform to learn a characteristic, and it really works appropriately, I ship the request from central:
func readCharacteristicValue() {
if let peripheral = connectedPeripheral, let attribute = targetCharacteristic {
peripheral.readValue(for: attribute)
}
}
And I course of the reply from peripheral:
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
//Richiamo comportamenti differenti in base al servizio
swap request.attribute.uuid {
case Constants.uuid.AuthService:
let authenticationInstance = PAuthentication.pAuthentication
authenticationInstance.responseToReadRequest(request: request, peripheralManager: peripheralManager, clearServiceCharacteristic: clearServiceCharacteristic)
case Constants.uuid.OtherService: print("Different implementation")
default: print("Nothing")
}
}
that is then captured by this perform from central:
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor attribute: CBCharacteristic, error: Error?){
//Se qualcosa va storto
if let error = error {
print("Error studying attribute worth: (error)")
}
//In base alla caratteristica che sta rispondendo gestisco in maniera differente le risposte
swap attribute.uuid {
case Constants.uuid.AuthService:
let authenticationInstance = CAuthentication.cAuthentication
authenticationInstance.responceFromReadRequest(attribute: attribute, disconnect: disconnect)
default:
print("Nothing")
}
}
However when I attempt to make a write request, I can get to the peripheral, however when it responds I am unable to see the response, however I do not perceive why. That is the perform that sends the write request by central:
func writeStringToCharacteristic(_ string: String) {
if let knowledge = string.knowledge(utilizing: .utf8), let peripheral = connectedPeripheral, let attribute = targetCharacteristic {
peripheral.writeValue(knowledge, for: attribute, sort: .withResponse)
}
}
That is the perform in peripheral than deal with the message:
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
for request in requests {
if request.attribute == attribute {
if let worth = request.worth {
// Esempio: Processa i dati scritti dal Central
let receivedString = String(knowledge: worth, encoding: .utf8)
print("Obtained knowledge from Central: (receivedString ?? "")")
if (receivedString == "Auth"){
var dataToSend:Knowledge?
if let title = SharedData.shared.AuthData["name"],
let surname = SharedData.shared.AuthData["surname"],
let date = SharedData.shared.AuthData["date"] {
dataToSend = "(title), (surname), (date)".knowledge(utilizing: .utf8)
// Ora puoi utilizzare 'dataToSend' per inviare i dati alla caratteristica Bluetooth
} else {
print("A number of values are lacking")
}
request.worth = dataToSend
peripheralManager.reply(to: request, withResult: .success)
//NON RIESCO A RECAPITARE LA RISPOSTA DAL CENTRALE
}
}
}
}
}
From the latter perform of the peripheral, I can’t see the message despatched to the central. So the central transmits the info to the peripheral however I am unable to then see the response from the central.