Skip to content

Commit ba0574e

Browse files
committed
add init sequence, update dependencies
1 parent a6d8db7 commit ba0574e

File tree

5 files changed

+107
-6
lines changed

5 files changed

+107
-6
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ org.jetbrains.dokka.experimental.gradle.pluginMode=V2Enabled
2626
org.jetbrains.dokka.experimental.gradle.pluginMode.noWarn=true
2727

2828
GROUP=com.pnuema.android
29-
VERSION_NAME=1.7.1
29+
VERSION_NAME=1.8.0
3030

3131
POM_NAME=Android OBD Library
3232
POM_ARTIFACT_ID=obd

gradle/libs.versions.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
[versions]
22
#libs
33
kotlinx-serialization = "1.9.0"
4-
vanniktech-maven-publish = "0.33.0"
4+
vanniktech-maven-publish = "0.34.0"
55
evalex = "3.5.0"
66
androidx-startup = "1.2.0"
77

88
#plugins
99
kotlin= "2.2.0"
10-
gradlePlugins-agp = "8.11.0"
10+
gradlePlugins-agp = "8.11.1"
1111
tomlChecker = "0.52.0"
1212
gradleCacheFix = "3.0.1"
1313
dokka = "2.0.0"
1414

1515
[libraries]
16-
dokka-gradle = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "dokka" }
17-
kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
1816
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
1917
evalex = { module = "com.ezylang:EvalEx", version.ref = "evalex"}
2018
androidx-startup = { module = "androidx.startup:startup-runtime", version.ref = "androidx-startup" }

obd/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ group = project.properties["GROUP"] as String
1313
android {
1414
base.archivesName.set("obd")
1515
namespace = "com.pnuema.android.obd"
16-
compileSdk = 35
16+
compileSdk = 36
1717
defaultConfig {
1818
minSdk = 24
1919
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.pnuema.android.obd.statics
2+
3+
import android.bluetooth.BluetoothSocket
4+
import android.util.Log
5+
import com.pnuema.android.obd.commands.OBDCommand
6+
import com.pnuema.android.obd.enums.ObdModes
7+
import com.pnuema.android.obd.enums.ObdProtocols
8+
import com.pnuema.android.obd.models.PID
9+
import com.pnuema.android.obd.statics.ObdLibrary.TAG
10+
import com.pnuema.android.obd.statics.PIDUtils.getPid
11+
import com.pnuema.android.obd.statics.PersistentStorage.clearAll
12+
import java.io.IOException
13+
14+
@Suppress("unused")
15+
class ObdInitSequence {
16+
companion object {
17+
private const val ECU_RESPONSE_TIMEOUT = 25
18+
private const val MODE_AT = "AT"
19+
20+
/**
21+
* Will run the connection sequence to setup the connection with the ELM327 device
22+
* given a connected bluetooth socket with the device
23+
* @param socket Bluetooth socket that is already connected to the ELM327 device
24+
* @return True if connection sequence succeeded and the test PID was retrieved successfully
25+
* from the device, false otherwise.
26+
*/
27+
fun run(socket: BluetoothSocket): Boolean {
28+
var connected: Boolean
29+
try {
30+
Log.d(TAG, "Socket connected")
31+
clearAll()
32+
var initPid: PID = getPid(ObdModes.MODE_01, "00") ?: return false
33+
val inputStream = socket.inputStream
34+
val outputStream = socket.outputStream
35+
36+
//set defaults
37+
initPid.mode = MODE_AT
38+
initPid.PID = "D"
39+
var cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
40+
Log.d(TAG, "Set defaults sent (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)
41+
42+
//resets the ELM327
43+
initPid.mode = MODE_AT
44+
initPid.PID = "Z"
45+
cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
46+
Log.d(TAG, "Reset command sent (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)
47+
48+
//extended responses off
49+
initPid.mode = MODE_AT
50+
initPid.PID = "E0"
51+
cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
52+
Log.d(TAG, "Extended Responses Off (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)
53+
54+
//line feeds off
55+
initPid.mode = MODE_AT
56+
initPid.PID = "L0"
57+
cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
58+
Log.d(TAG, "Turn Off Line Feeds (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)
59+
60+
//printing of spaces off
61+
initPid.mode = MODE_AT
62+
initPid.PID = "S0"
63+
cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
64+
Log.d(TAG, "Printing Spaces Off (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)
65+
66+
//headers off
67+
initPid.mode = MODE_AT
68+
initPid.PID = "H0"
69+
cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
70+
Log.d(TAG, "Headers Off (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)
71+
72+
//set protocol
73+
initPid.mode = "$MODE_AT SP"
74+
initPid.PID = ObdProtocols.AUTO.value.toString()
75+
cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
76+
Log.d(TAG, "Select Protocol (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)
77+
78+
//set timeout for response from the ECU
79+
initPid.mode = "$MODE_AT ST"
80+
initPid.PID = Integer.toHexString(0xFF and ECU_RESPONSE_TIMEOUT)
81+
cmd = OBDCommand(initPid).setIgnoreResult(true).run(inputStream, outputStream)
82+
Log.d(TAG, "Set timeout (" + initPid.mode + " " + initPid.PID + ") Received: " + cmd.rawResult)
83+
84+
if (socket.isConnected) {
85+
initPid = getPid(ObdModes.MODE_01, "00") ?: return false
86+
Log.d(TAG, "Mode 1 PID 00: " + OBDCommand(initPid).run(inputStream, outputStream).formattedResult)
87+
connected = initPid.calculatedResultString != null
88+
} else {
89+
Log.e(TAG, "Bluetooth socket disconnected during connection init")
90+
connected = false
91+
}
92+
} catch (e: IOException) {
93+
Log.e(TAG, "IOException on init commands: " + e.message)
94+
connected = false
95+
} catch (e: InterruptedException) {
96+
Log.e(TAG, "InterruptedException on init commands: " + e.message)
97+
connected = false
98+
}
99+
return connected
100+
}
101+
}
102+
}

obd/src/main/java/com/pnuema/android/obd/statics/ObdLibrary.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import java.io.InputStream
2121
* @author Brad Barnhill
2222
*/
2323
internal object ObdLibrary {
24+
const val TAG = "ObdLibrary"
2425
fun init(applicationContext: Application) {
2526
this.applicationContext = applicationContext
2627
}

0 commit comments

Comments
 (0)