如何解决获取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
MAIL_USERNAME=youremail@gmail.com
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
方法。
由于您没有此方法,因此您的写入请求得到“不支持”的响应。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。