问题描述
我需要通过移动应用控制汽车安全(门打开/关闭)。我正在使用 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 (将#修改为@)