微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

BLE 和 ELM327 (CAN)

如何解决BLE 和 ELM327 (CAN)

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。