// 數(shù)據(jù)接收
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
// error返回錯(cuò)誤 : Reading is not permitted.
FIILPrintLog(message: "更新特征值時(shí)發(fā)生錯(cuò)誤鳍徽,錯(cuò)誤信息--\(error.localizedDescription)")
return
}
}
iOS13上, 連接BLE藍(lán)牙時(shí)候, 返回錯(cuò)誤:Reading is not permitted.
查找問題后,發(fā)現(xiàn)在"搜索到特征回調(diào)"方法中,寫了錯(cuò)誤的方法導(dǎo)致的,返回的特征是寫和通知特征, 在這個(gè)特征中,我寫上了讀的方法, 在iOS13以前沒有影響, iOS13時(shí)候,系統(tǒng)報(bào)出了錯(cuò)誤
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
// 錯(cuò)誤地方 在iOS13以前沒有影響, iOS13時(shí)候,系統(tǒng)報(bào)出了錯(cuò)誤
// 寫 UUID
if characteristic.uuid == characteristicWriteUUID {
// 讀 (這里是不對?, 應(yīng)該寫到讀的uuid里面, 請看改正后的代碼)
peripheral.readValue(for: characteristic)
if let value = characteristic.value {
FIILPrintLog(message: "讀到peripheral.readValue數(shù)據(jù)----\(NSData.init(data: value))")
}
}
}
改正后的代碼
/// 搜索到特征回調(diào)
///
/// - Parameters:
/// - peripheral: 外圍設(shè)備
/// - service: 服務(wù)
/// - error: 錯(cuò)誤信息
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if (error != nil) {
self.delegate?.bleConnection(status: false)
FIILPrintLog(message: "外圍設(shè)備尋找特征過程中發(fā)生錯(cuò)誤,錯(cuò)誤信息: \(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
// 藍(lán)牙連接代理協(xié)議
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數(shù)據(jù)----\(NSData.init(data: value))")
}
}
}
}
}