我需要通过移动应用程序控制汽车安全(门打开/关闭)。我正在使用 ELM 327 适配器和 BLE 我通过 BLE 连接到设备并读取服务的特性,但是我应该发送哪个命令以及打开/关闭警报到哪个服务?
import UIKit
import CoreBluetooth
class ViewController: UIViewController {
private var centralManager: CBCentralManager!
private var peripheral: CBPeripheral!
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
}
extension ViewController: CBPeripheralDelegate, CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
print("Central state update")
if central.state != .poweredOn {
print("Central is not powered on")
} else {
centralManager.scanForPeripherals(withServices: nil, options: nil)
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
guard peripheral.name == "OBDBLE" else {return}
print(peripheral.identifier)
self.centralManager.stopScan()
self.peripheral = peripheral
self.peripheral.delegate = self
self.centralManager.connect(self.peripheral, options: nil)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
guard peripheral == self.peripheral else {return}
print("Connected to your Particle Board")
peripheral.discoverServices(nil)
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard let services = peripheral.services else {return}
print("didDiscoverServices")
for service in services {
print(service)
peripheral.discoverCharacteristics(nil, for: service)
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
guard let characteristics = service.characteristics else {return}
print("didDiscoverCharacteristicsFor: \(service.uuid)")
for characteristic in characteristics {
print(characteristic)
}
}
}