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+ }
0 commit comments