BLE and ELM327 (CAN)

710 Views Asked by At

I need to control a car security (door open / close) via a mobile app. I am using ELM 327 adapter and BLE I connect to the device via BLE and read the characteristics of the services, but which command should I send and to which service to turn the alarm on / off?

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)
            }
        }
    }
1

There are 1 best solutions below

0
On

This question is more suitable on a forum, such as https://mechanics.stackexchange.com, since it's not so much about programming, but rather about OBD2.

That said, chances are you can't do this via OBD2 anyways. OBD2 has been designed for emission diagnostics and while there are non-standard PIDs with which you can get access to more sensors, facilities critical like an alarm are hopefully not exposed via the general OBD2 socket.