【问题标题】:Getting CBATTErrorDomain Code=6 The request is not supported error while writing value to peripheral获取 CBATTErrorDomain Code=6 The request is not supported error while writing value to peripheral
【发布时间】:2020-08-30 06:13:03
【问题描述】:

我创建了自定义外围设备以将数据写入外围设备并尝试从我的中心写入数据。 执行写入值函数时,我收到不支持请求的错误。 这是我的代码。希望你能理解我的问题。 寻找解决方案。 提前致谢。

connectedPeripheral?.writeValue(data, for: characteristic, type: .withResponse)

设置我的自定义 BLEPeripheral 并在 Peripheral 中启动广告

// MARK:CBPeripheralManagerDelegate

func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
    if peripheral.state == .poweredOn {
        setup()
    } else {
        print("peripheral is not available: \(peripheral.state.rawValue)")
    }
}

func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
    if let error = error {
        print("Could not add service: \(error.localizedDescription)")
    } else {
        print("peripheral added service. Start advertising")
        let advertisementData: [String: Any] = [
            CBAdvertisementDataServiceUUIDsKey: [CBUUID(string: BLEIdentifiers.serviceIdentifier)],
            CBAdvertisementDataLocalNameKey: "BLE Sensor"  // This key will not be transmitted when app is backgrounded
        ]
        manager.startAdvertising(advertisementData)
    }
}

func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
    if let error = error {
        print("Could not start advertising: \(error.localizedDescription)")
    } else {
        print("peripheral started advertising")
    }
}

func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
    print("Did receive read request: \(request)")
    if !request.characteristic.uuid.isEqual(characteristic.uuid) {
        peripheral.respond(to: request, withResult: .requestNotSupported)
    } else {
        guard let value = characteristic.value else {
            peripheral.respond(to: request, withResult: .invalidAttributeValueLength)
            return
        }
        if request.offset > value.count {
            peripheral.respond(to: request, withResult: .invalidOffset)
        } else {
            request.value = value.subdata(in: request.offset..<value.count-request.offset)
            peripheral.respond(to: request, withResult: .success)
        }
    }
    
}
func setup() {
    let characteristicUUID = CBUUID(string: BLEIdentifiers.characteristicIdentifier)
    
    characteristic = CBMutableCharacteristic(type: characteristicUUID, properties: [.read, .write,.notify], value: nil, permissions: [.readable,.writeable])

    let descriptor = CBMutableDescriptor(type: CBUUID(string: CBUUIDCharacteristicUserDescriptionString), value: "BLESensor prototype")
    characteristic.descriptors = [descriptor]
    
    let serviceUUID = CBUUID(string: BLEIdentifiers.serviceIdentifier)
    let service = CBMutableService(type: serviceUUID, primary: true)
    
    service.characteristics = [characteristic]
    manager.add(service)
}

将数据写入 Central 中的外围设备

func writeDataToPeripheral(data: Data){
     if let characteristics =  ctService?.characteristics {
         for characteristic in characteristics {
             if characteristic.uuid == CBUUID(string: BLEIdentifiers.characteristicIdentifier) {
                 if characteristic.properties.contains(.write) {
                    connectedPeripheral?.writeValue(data, for: characteristic, type: .withResponse)
                 }
             }
         }
     }
 }

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
         guard error == nil else {
            print("Error discovering didWriteValueFor: error", error.debugDescription)
            //Getting Error Domain=CBATTErrorDomain Code=6 "The request is not supported."
             return
         }
         print("Message sent")
     }

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    if let error = error {
        print("peripheral failed to discover services: \(error.localizedDescription)")
    } else {
        peripheral.services?.forEach({ (service) in
            print("service discovered: \(service)")
            peripheral.discoverCharacteristics([CBUUID(string: BLEIdentifiers.characteristicIdentifier)], for: service)
        })
    }
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    if let error = error {
        print("NSA A peripheral failed to discover characteristics: \(error.localizedDescription)")
    } else {
        ctService = service
        service.characteristics?.forEach({ (characteristic) in
            print("NSA A characteristic discovered: \(characteristic)")
            if characteristic.uuid == CBUUID(string: BLEIdentifiers.characteristicIdentifier)  {
                // keep a reference to this characteristic so we can write to it
                writeCharacteristic = characteristic
            }
            if characteristic.properties.contains(.read) {
                peripheral.readValue(for: characteristic)
            }
            peripheral.discoverDescriptors(for: characteristic)
        })
    }
}

【问题讨论】:

  • 检查特征的属性以确认支持写入响应。它可能只支持不响应的写入。
  • @Paulw11 感谢您的回复。我已经在设置要写入的权限和属性。特征= CBMutableCharacteristic(类型:characteristicUUID,属性:[.read,.write],值:nil,权限:[.readable,.writeable])。仍然为什么我收到请求不受支持错误。你能帮忙吗?
  • 因为你已经让它写了,而不是写响应,你正在尝试写响应。
  • @Paulw11 是的,但 CBCharacteristicProperties 结构中没有可用的“写响应”。只有 write 和 writeWithoutResponse 与写入属性相关。那么如何实现写有响应呢?
  • edit您的问题显示您设置和发布服务的代码

标签: ios swift bluetooth-lowenergy core-bluetooth bluetooth-peripheral


【解决方案1】:

为了支持写入,您必须在您的CBPeripheralManagerDelegate 中实现didReceiveWrite 方法。

由于您没有此方法,因此您会收到对您的写入请求的“不支持”响应。

【讨论】:

  • 是的,现在工作。谢谢 Paulw
猜你喜欢
  • 2017-04-12
  • 2019-02-18
  • 1970-01-01
  • 2021-03-24
  • 2022-12-27
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多