我需要通过移动应用程序控制汽车安全(车门打开/关闭)。我正在使用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)
}
}
}发布于 2021-10-27 15:11:02
这个问题更适合在https://mechanics.stackexchange.com这样的论坛上,因为它不是关于编程的,而是关于OBD2的。
也就是说,无论如何你都不能通过OBD2做到这一点。OBD2专为废气排放诊断而设计,虽然您可以使用非标准PID访问更多传感器,但警报等关键设施希望不会通过通用OBD2插座暴露。
https://stackoverflow.com/questions/67498056
复制相似问题