0

我需要通过移动应用程序控制汽车安全(门打开/关闭)。我正在使用 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)
            }
        }
    }
4

1 回答 1

0

这个问题在论坛上更合适,例如https://mechanics.stackexchange.com,因为它不是关于编程,而是关于 OBD2。

也就是说,无论如何你都不能通过 OBD2 做到这一点。OBD2 专为排放诊断而设计,虽然您可以使用非标准 PID 访问更多传感器,但希望警报等关键设施不会通过通用 OBD2 插座暴露。

于 2021-10-27T15:11:01.547 回答