获取CBATTErrorDomain代码= 6向外围设备写入值时,请求不支持错误

问题描述

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

   1. Enable the 2-step verification
   2. Create App Passwdord to be use by your system HERE and use it 
   3. Now go to .env file and make these changes
        
        MAIL_DRIVER=smtp
        MAIL_HOST=smtp.gmail.com
        MAIL_PORT=587
        [email protected]
        MAIL_PASSWORD=password generated from app password
        MAIL_ENCRYPTION=tls
        
        This worked fine on mine.

    For 2-step verification 
       https://www.google.com/landing/2step/
    
    For App Password
       https://security.google.com/settings/security/apppasswords


    

设置我的自定义BLEPeripheral并在Peripheral中开始广告

// MARK:CBPeripheralManagerDelegate

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

将数据写入Central的外围设备

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

解决方法

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

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