外设未响应中央的writeRequest(蓝牙)

问题描述 投票:0回答:1

我设置了一个功能来读取功能,并且它工作正常,我从中央发送了请求:

func readCharacteristicValue() {
        if let peripheral = connectedPeripheral, let characteristic = targetCharacteristic {
            peripheral.readValue(for: characteristic)
        }
    }

我处理来自外围设备的答案:

func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
        
        //Richiamo comportamenti differenti in base al servizio
        switch request.characteristic.uuid {
            
        case Constants.uuid.AuthService:
                let authenticationInstance = PAuthentication.pAuthentication
                authenticationInstance.responseToReadRequest(request: request, peripheralManager: peripheralManager, clearServiceCharacteristic: clearServiceCharacteristic)
            
            case Constants.uuid.OtherService: print("Other implementation")
            
            default: print("Nothing")
        }
        
    }

然后由该函数从中央捕获:

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?){
        //Se qualcosa va storto
        if let error = error {
            print("Error reading characteristic value: \(error)")
        }

        //In base alla caratteristica che sta rispondendo gestisco in maniera differente le risposte
        switch characteristic.uuid {
            
        case Constants.uuid.AuthService:
                let authenticationInstance = CAuthentication.cAuthentication
                authenticationInstance.responceFromReadRequest(characteristic: characteristic, disconnect: disconnect)
            
            default:
                print("Nothing")
        }
    }

但是当我尝试发出写入请求时,我可以访问外围设备,但是当它响应时我看不到响应,但我不明白为什么。这是central发送写请求的函数:

    func writeStringToCharacteristic(_ string: String) {
        if let data = string.data(using: .utf8), let peripheral = connectedPeripheral, let characteristic = targetCharacteristic {
            peripheral.writeValue(data, for: characteristic, type: .withResponse)
        }
    }

这是外设中处理消息的函数:

func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
         for request in requests {
             if request.characteristic == characteristic {
                 if let value = request.value {
                     // Esempio: Processa i dati scritti dal Central
                     let receivedString = String(data: value, encoding: .utf8)
                     print("Received data from Central: \(receivedString ?? "")")
                     if (receivedString == "Auth"){
                         
                         var dataToSend:Data?
                         if let name = SharedData.shared.AuthData["name"],
                            let surname = SharedData.shared.AuthData["surname"],
                            let date = SharedData.shared.AuthData["date"] {
                            dataToSend = "\(name), \(surname), \(date)".data(using: .utf8)
                             // Ora puoi utilizzare 'dataToSend' per inviare i dati alla caratteristica Bluetooth
                         } else {
                             print("One or more values are missing")
                         }
                         
                         request.value = dataToSend
                         peripheralManager.respond(to: request, withResult: .success)
                         //NON RIESCO A RECAPITARE LA RISPOSTA DAL CENTRALE
                     }
                 }
             }
         }
     }

从外围设备的后一个功能中,我看不到发送到中央设备的消息。所以中央将数据传输到外围设备,但我看不到中央的响应。

ios swift bluetooth bluetooth-lowenergy
1个回答
0
投票

外设无法向中央设备发送数据以响应写入请求。

withResponse
写入类型要求外设发送已成功接收写入的响应(
withResult:.success
),但仅此而已。响应不包含任何附加数据。

对于外围设备向中央设备发送未经请求的数据,必须使用Indicate/Notify。

响应写入后,您需要使用

updateValue
将更新后的值传送到中央。您的中心必须之前使用过
setNotifyValue
才能收到该特征有新值的通知。

© www.soinside.com 2019 - 2024. All rights reserved.