iOS13 Reading is not permitted.

// 数据接收
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
     if let error = error {
            // error返回错误 : Reading is not permitted.
            FIILPrintLog(message: "更新特征值时发生错误,错误信息--\(error.localizedDescription)")
            return
      }
}

iOS13上, 连接BLE蓝牙时候, 返回错误:Reading is not permitted.

查找问题后,发现在"搜索到特征回调"方法中,写了错误的方法导致的,返回的特征是写和通知特征, 在这个特征中,我写上了读的方法, 在iOS13以前没有影响, iOS13时候,系统报出了错误

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        // 错误地方  在iOS13以前没有影响, iOS13时候,系统报出了错误
        // 写 UUID 
        if characteristic.uuid == characteristicWriteUUID {
               // 读  (这里是不对❌, 应该写到读的uuid里面, 请看改正后的代码)
              peripheral.readValue(for: characteristic)
              if let value = characteristic.value {
              FIILPrintLog(message: "读到peripheral.readValue数据----\(NSData.init(data: value))")
                        }
         }
}

改正后的代码

    /// 搜索到特征回调
    ///
    /// - Parameters:
    ///   - peripheral: 外围设备
    ///   - service: 服务
    ///   - error: 错误信息
    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        
        
        if (error != nil) {
            self.delegate?.bleConnection(status: false)
            FIILPrintLog(message: "外围设备寻找特征过程中发生错误,错误信息: \(error!.localizedDescription)")
        }
        //        FIILPrintLog(message: "搜索特征")
        
        if self.bleHeaderType == .F020 {
            let serviceUUID = CBUUID.init(string: self.kServiceUUID)
            
            let characteristicWriteUUID = CBUUID.init(string: self.kCharacteristicsWriteUUID)
            let characteristicNotiUUID = CBUUID.init(string: self.kCharacteristicsNotiUUID)
            let characteristicReadUUID = CBUUID.init(string: self.kCharacteristicsReadUUID)
            
            if service.uuid == serviceUUID {
                for characteristic in service.characteristics! {
                    // 写
                    if characteristic.uuid == characteristicWriteUUID {
                        self.write = characteristic
                        // 蓝牙连接代理协议
                        self.delegate?.bleConnection(status: true)
                    // 通知
                    }else if characteristic.uuid == characteristicNotiUUID {
                        peripheral.setNotifyValue(true, for: characteristic)
                    // 读
                    }else if characteristic.uuid == characteristicReadUUID {
                        self.readOrWrite = characteristic
                        peripheral.readValue(for: characteristic)
                        if let value = characteristic.value {
                            FIILPrintLog(message: "读到peripheral.readValue数据----\(NSData.init(data: value))")
                        }
                    }
                }
            }
    }

你可能感兴趣的:(iOS13 Reading is not permitted.)