Quickstart: Using ObdDeviceConnection
masterTo use the library, provide an InputStream and an OutputStream from your transport layer (Bluetooth, Wi-Fi, or USB) to create an ObdDeviceConnection.
Important Usage Notes:
- Threading: Call
run()from a background coroutine context (e.g.,Dispatchers.IO). Do not call it from the main thread on Android. - Concurrency: Each
ObdDeviceConnectioninstance is a serialized command channel guarded by a coroutineMutex. Reuse one instance per physical connection.
Recommended Setup Profile: For faster, lower-noise sessions, run these commands sequentially before querying data:
ResetAdapterCommand()SetEchoCommand(Switcher.OFF)SetLineFeedCommand(Switcher.OFF)SetSpacesCommand(Switcher.OFF)SetHeadersCommand(Switcher.OFF)SetAdaptiveTimingCommand(AdaptiveTimingMode.AUTO_1)(orAUTO_2for more aggressive timing)SelectProtocolCommand(ObdProtocols.AUTO)(or a known protocol)
import com.github.eltonvs.obd.command.AdaptiveTimingMode
import com.github.eltonvs.obd.command.ObdProtocols
import com.github.eltonvs.obd.command.Switcher
import com.github.eltonvs.obd.command.at.ResetAdapterCommand
import com.github.eltonvs.obd.command.at.SelectProtocolCommand
import com.github.eltonvs.obd.command.at.SetAdaptiveTimingCommand
import com.github.eltonvs.obd.command.at.SetEchoCommand
import com.github.eltonvs.obd.command.at.SetHeadersCommand
import com.github.eltonvs.obd.command.at.SetLineFeedCommand
import com.github.eltonvs.obd.command.at.SetSpacesCommand
import com.github.eltonvs.obd.command.control.TroubleCodesCommand
import com.github.eltonvs.obd.command.control.VINCommand
import com.github.eltonvs.obd.command.engine.RPMCommand
import com.github.eltonvs.obd.connection.ObdDeviceConnection
import java.io.InputStream
import java.io.OutputStream
suspend fun readObd(inputStream: InputStream, outputStream: OutputStream) {
val obdConnection = ObdDeviceConnection(inputStream, outputStream)
// Recommended low-noise ELM327 setup
obdConnection.run(ResetAdapterCommand())
obdConnection.run(SetEchoCommand(Switcher.OFF))
obdConnection.run(SetLineFeedCommand(Switcher.OFF))
obdConnection.run(SetSpacesCommand(Switcher.OFF))
obdConnection.run(SetHeadersCommand(Switcher.OFF))
obdConnection.run(SetAdaptiveTimingCommand(AdaptiveTimingMode.AUTO_1))
obdConnection.run(SelectProtocolCommand(ObdProtocols.AUTO))
val rpm = obdConnection.run(RPMCommand())
val vin = obdConnection.run(VINCommand(), useCache = true)
val troubleCodes = obdConnection.run(TroubleCodesCommand())
println("RPM: ${rpm.value} ${rpm.unit}")
println("VIN: ${vin.value}")
println("DTC: ${troubleCodes.value.ifBlank { "none" }}")
}