我正在开发 ios Ble 应用程序来为手机充电。我做了一切正确的事情来发现特性。首先 Scannig 外围设备并连接到它。发现具有通知和写入而没有响应属性的服务(FFB0)和特征(FFB1,FFB2)。我找到了客户端特征配置描述符。我想向 pcb 发送命令以解锁充电我想将值写入 FFB2 特性但外围设备没有响应。这是我的代码。我正在搜索与此相关的所有内容,但我没有找到任何解决方案如果有人解决这个问题,它对我有帮助
谢谢你
// MARK: - CBCentral,CBperipheral 委托方法
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .unknown:
print("central.state is .unknown")
case .resetting:
print("central.state is .resetting")
case .unsupported:
print("central.state is .unsupported")
case .unauthorized:
print("central.state is .unauthorized")
case .poweredOff:
print("central.state is .poweredOff")
case .poweredOn:
centralManager.scanForPeripherals(withServices: [batteryServiceCBUUID])
print("central.state is .poweredOn")
@unknown default:
fatalError()
}
print("state: \(central.state)")
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
switch peripheral.state {
case .unknown:
print("peripheral.state is .unknown")
case .resetting:
print("peripheral.state is .resetting")
case .unsupported:
print("peripheral.state is .unsupported")
case .unauthorized:
print("peripheral.state is .unauthorized")
case .poweredOff:
print("peripheral.state is .poweredOff")
case .poweredOn:
let advertisementData = [CBAdvertisementDataLocalNameKey: "Test Device"]
peripheralManager.startAdvertising(advertisementData)
service.characteristics = [characteristicMut]
peripheralManager.add(service)
// peripheralManager.delegate = self
print("peripheral.state is .poweredOn")
@unknown default:
fatalError()
}
print("state: \(peripheral.state)")
}
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
if let error = error {
print("Failed… error: \(error)")
return
}
print("Succeeded!")
// peripheralManager.stopAdvertising()
}
func peripheralManager(peripheral: CBPeripheralManager, didAddService service: CBService, error: NSError?)
{
if let error = error {
print("error: \(error)")
return
}
print("service: \(service)")
}
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
print("Subscribed Central=======>\(central)Characteristics \(characteristic)")
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print(peripheral)
batteryServicePeripheral = peripheral
batteryServicePeripheral.delegate = self
centralManager.stopScan()
centralManager.connect(batteryServicePeripheral)
}
func peripheralManager(peripheral: CBPeripheralManager, didReceiveReadRequest request: CBATTRequest)
{
if request.characteristic.uuid == characteristicMut.uuid
{
// Set the correspondent characteristic's value
// to the request
request.value = characteristicMut.value
// Respond to the request
peripheralManager.respond(
to: request,
withResult: .success)
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Connected=======>\(String(describing: peripheralManager))")
batteryServicePeripheral.delegate = self
batteryServicePeripheral.discoverServices([batteryServiceCBUUID])
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print("Fail To Connect=======>\(String(describing: error))")
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
if error == nil {
print("Disconnected========>\(peripheral)")
}else {
print("Disconnected========>\(String(describing: error))")
}
}
// MARK: - CBPeripheral 委托方法
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard let services = peripheral.services else { return }
for service in services {
print("SPAKA:PERIPHERALSERVICES============>\(service)")
peripheral.discoverCharacteristics(nil, for: service)
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let characteristics = service.characteristics {
//else { return }
for characteristic in characteristics {
print(characteristic)
if characteristic.uuid == batteryServiceCBUUID {
batteryServicePeripheral.setNotifyValue(true, for: characteristic)
batteryServicePeripheral.readValue(for: batteryCharacteristics)
}
if characteristic.uuid == batteryServiceRequestCBUUID2 {
batteryCharacteristics = characteristic
let str1 = "55e100"
let data = String(format: "%@%@",str1,hexTimeForChar)
guard let valueString = data.data(using: String.Encoding.utf8) else {return}
peripheral.writeValue(valueString, for: characteristic , type: CBCharacteristicWriteType.withoutResponse)
print("Value String===>\(valueString.debugDescription)")
batteryServicePeripheral.readValue(for: batteryCharacteristics)
peripheral.setNotifyValue(true, for: characteristic)
peripheral.discoverDescriptors(for: batteryCharacteristics)
}
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
if error == nil {
print("Message sent=======>\(String(describing: characteristic.value))")
}else{
print("Message Not sent=======>\(String(describing: error))")
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
if error == nil {
print("SPAKA : IS NOTIFYING UPDATED STATE ======>\(characteristic.isNotifying)")
print("SPAKA : UPDATED DESCRIPTION ======>\(String(describing: characteristic.description))")
}else{
print("SPAKA : ERRORUPDATEDNOTIFICATION\(String(describing: error))")
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
print("SPAKA: UPDATED VALUE RECEIVED========>\(String(describing: characteristic.value))")
print("characteristic UUID: \(characteristic.uuid), value: \(characteristic.value)")
guard let str = characteristic.value else { return }
if let string = String(bytes: str, encoding: .utf8) {
print("SPAKA==========>:::\(string)")
} else {
print("not a valid UTF-8 sequence")
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {
guard let desc = batteryCharacteristics.descriptors else { return
}
for des in desc {
print("BEGIN:SPAKA DESCRIPTOR========>\(des)")
discoveredDescriptor = des
peripheral.setNotifyValue(true, for: batteryCharacteristics)
}
}
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
if let error = error {
print("Failed… error: \(error)")
return
}
print("Descriptor Write Value uuid: \(descriptor.uuid), value: \(String(describing: descriptor.value))")
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor descriptor: CBDescriptor, error: Error?) {
if let error = error {
print("Failed… error: \(error)")
return
}
print("Descriptor Updated Value uuid: \(descriptor.uuid), value: \(String(describing: descriptor.value))")
batteryServicePeripheral.readValue(for: batteryCharacteristics)
}
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
for request in requests {
print(request)
if let value = request.value {
if let messageText = String(data: value, encoding: String.Encoding.utf8) {
print(messageText)
}
}
}
}
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest){
print("Received Request==========>\(request)")
}