Skip to content

Commit

Permalink
Merge pull request #15 from nazgu1/feature/vgate-icar-pro
Browse files Browse the repository at this point in the history
Add VGate iCar Pro BLE characteristics to scanner
  • Loading branch information
kkonteh97 authored Apr 3, 2024
2 parents 62e98b3 + b4afb73 commit 6065050
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions Sources/SwiftOBD2/Communication/bleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public enum ConnectionState {
}

class BLEManager: NSObject, CommProtocol {
static let services = [
CBUUID(string: "FFE0"),
CBUUID(string: "FFF0"),
CBUUID(string: "18F0"), //e.g. VGate iCar Pro
]

let logger = Logger(subsystem: "com.kemo.SmartOBD2", category: "BLEManager")

static let RestoreIdentifierKey: String = "OBD2Adapter"
Expand Down Expand Up @@ -54,7 +60,7 @@ class BLEManager: NSObject, CommProtocol {
override init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: .main, options: [CBCentralManagerOptionShowPowerAlertKey: true,
CBCentralManagerOptionRestoreIdentifierKey: BLEManager.RestoreIdentifierKey])
CBCentralManagerOptionRestoreIdentifierKey: BLEManager.RestoreIdentifierKey])
}

// MARK: - Central Manager Control Methods
Expand Down Expand Up @@ -82,7 +88,7 @@ class BLEManager: NSObject, CommProtocol {
logger.debug("Bluetooth is On.")
#endif
guard let device = connectedPeripheral else {
startScanning([CBUUID(string: "FFE0"), CBUUID(string: "FFF0")])
startScanning(Self.services)
return
}

Expand Down Expand Up @@ -123,7 +129,7 @@ class BLEManager: NSObject, CommProtocol {
logger.info("Connected to peripheral: \(peripheral.name ?? "Unnamed")")
connectedPeripheral = peripheral
connectedPeripheral?.delegate = self
connectedPeripheral?.discoverServices([CBUUID(string: "FFE0"), CBUUID(string: "FFF0")])
connectedPeripheral?.discoverServices(Self.services)
connectionState = .connectedToAdapter
obdDelegate?.connectionStateChanged(state: .connectedToAdapter)
}
Expand All @@ -140,7 +146,7 @@ class BLEManager: NSObject, CommProtocol {
}
self.foundPeripheralCompletion = nil
}
self.startScanning([CBUUID(string: "FFF0"), CBUUID(string: "FFE0")])
self.startScanning(Self.services)
}
}
}
Expand All @@ -150,11 +156,14 @@ class BLEManager: NSObject, CommProtocol {
func didDiscoverServices(_ peripheral: CBPeripheral, error _: Error?) {
for service in peripheral.services ?? [] {
print("Discovered service: \(service.uuid)")
if service.uuid == CBUUID(string: "FFE0") {
switch service {
case CBUUID(string: "FFE0"):
peripheral.discoverCharacteristics([CBUUID(string: "FFE1")], for: service)
} else if service.uuid == CBUUID(string: "FFF0") {
case CBUUID(string: "FFF0"):
peripheral.discoverCharacteristics([CBUUID(string: "FFF1"), CBUUID(string: "FFF2")], for: service)
} else {
case CBUUID(string: "18F0"):
peripheral.discoverCharacteristics([CBUUID(string: "2AF0"), CBUUID(string: "2AF1")], for: service)
default:
peripheral.discoverCharacteristics(nil, for: service)
}
}
Expand All @@ -169,13 +178,20 @@ class BLEManager: NSObject, CommProtocol {
if characteristic.properties.contains(.notify) {
peripheral.setNotifyValue(true, for: characteristic)
}
if characteristic.uuid.uuidString == "FFE1" {
switch characteristic.uuid.uuidString {
case "FFE1": // for servcice FFE0
ecuWriteCharacteristic = characteristic
ecuReadCharacteristic = characteristic
} else if characteristic.uuid.uuidString == "FFF1" {
case "FFF1": // for servcice FFF0
ecuReadCharacteristic = characteristic
case "FFF2": // for servcice FFF0
ecuWriteCharacteristic = characteristic
case "2AF0": // for servcice 18F0
ecuReadCharacteristic = characteristic
} else if characteristic.uuid.uuidString == "FFF2" {
case "2AF1": // for servcice 18F0
ecuWriteCharacteristic = characteristic
default:
break
}
}

Expand Down

0 comments on commit 6065050

Please sign in to comment.