我正在开发 iOS BLE 应用程序来为手机充电。我做了一切纠正以发现特征。首先扫描外围设备并连接到它。发现具有通知和写入而没有响应属性的服务(FFB0)和特征(FFB1,FFB2)。
我找到了客户端特征配置描述符。我想向 PCB 发送命令以解锁充电 我想将值写入 FFB2 特性,但外围设备没有响应。这是我的代码。
我已经搜索了与此相关的所有内容,但没有找到任何解决方案如果有人解决了这个问题,这将对我有所帮助。
这是客户提供的文件:
每个PCB上都有一个BLE4.0蓝牙模块,每个蓝牙模块有一个单独的12字节长度地址码,PCB数据通道,Service UUID是OxFFBO,包括两个特征值,分别是OxFFBI和OxFFB2,通信长度数据为 1--20 字节。OxFFB1为APP数据下载通道。OxFBB2为蓝牙上传数据通道。
上电进入空闲模式,在空闲模式下,两个灯会闪烁。它会向手机发送一个ID码(一个字节),手机中的APP会得到这个ID码(一个字节)。ID 码可以通过手机设置进行设置。即通过ID码发送相应的解锁指令。
解锁指令为0x55,0xe1,(ID0+1),时间,以下为解锁指令说明:
ID0为ID码,“time”为用户解锁时间(5-120分钟),注意是二进制转十六进制,time==05表示5分钟,time==6表示6分钟, time ==A 表示 10 分钟,依此类推。如果PCB的ID0为0xff,解锁pcb 60min为:0x55,0xe1,0x00,0x3c;如果PCB的ID0为0x05,解锁pcb 10分钟为:0x55 , 0xe1 ,0x06 , 0x0a
当 PCB 上的 MCU 收到解锁指令时,它会开始打开输出和计时。计时超时后系统再次进入空闲模式。在空闲模式下,长按该键进入关机睡眠时间模式。
uart 的波特率设置为 9,600。APP发送给MCU的查询命令为0x55 0x01 MCU应回复APP的信息格式:0x55、0x02、bat_level、xH、XL(xH XL代表电池当前电压值,xH-代表高8位, XL---代表低8位)
import CoreBluetooth
let batteryServiceCBUUID = CBUUID(string: "FFB0")
let batteryServiceRequestCBUUID = CBUUID(string: "FFB1")
let batteryServiceRequestCBUUID2 = CBUUID(string: "FFB2")
class ChargingViewController: UIViewController , CBPeripheralDelegate
,CBCentralManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil, options: nil)
}
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 .unauthorised:
print("central.state is .unauthorised")
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 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 centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Connected=======>\(String(describing: batteryServicePeripheral))")
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 Delegate Methods
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 {
peripheral.setNotifyValue(true, for: characteristic)
}
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)")
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
print("Descriptor Present Value and uuid: \(des.uuid), value: \(String(describing: des.value))")
peripheral.readValue(for: discoveredDescriptor)
}
}
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))")
}
}