Android Question Looking for OBDII Bluetooth example

Csaba Balogh

Member
Licensed User
Hi There,

I am very new to Bluetooth connection and also to OBDII.
I was searching the forum topics lately and found some useful threads but I couldn't find one that would give a complete example on how to get the hang of the problem.

On the first run I'd like to read the RPM data of my car's engine. As it is for hobby purposes for now I would highly appreciate if someone who has previous experience regarding ODBII bluetooth com. could lite me up with some hints where to get started.

Thanks a lot in advance.
 
Last edited:

Csaba Balogh

Member
Licensed User
Now I am trying to use an old Serial stuff which seems to be working correctly for connection with my bth dongle.



I can connect to my dongle with the code above. The only thing I cannot do is to send a correct request message and then get an answer.
Actually I don't know what the correct format of a message is.

The message is i.e : "ATZ\r\n" Is that the way it is supposed to be sent? Or "ATZ" & CRLF
This would be the string that shall be converted into byte code and sent to serial port.

Thanks,
Csaba
 
Last edited:
Upvote 0

Csaba Balogh

Member
Licensed User
This issue is finally solved with the following formula:

The message is like "ATZ" and the carridge return is chr(13) as usual

So the code is:

B4X:
'Activity module
Sub Process_Globals
   Dim AStreams As AsyncStreams
   Dim Serial1 As Serial
   'Dim Timer1 As Timer
   Dim connected As Boolean
End Sub

Sub Globals
   Dim btnSend As Button
   Dim txtLog As EditText
   Dim txtSend As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Serial1.Initialize("Serial1")
      'Timer1.Initialize("Timer1", 200)
   End If
   Activity.LoadLayout("1")
   Activity.AddMenuItem("Connect", "mnuConnect")
   Activity.AddMenuItem("Disconnect", "mnuDisconnect")
End Sub
Sub Activity_Resume
   If Serial1.IsEnabled = False Then
      Msgbox("Please enable Bluetooth.", "")
   Else
      Serial1.Listen 'listen for incoming connections
   End If
End Sub
Sub mnuConnect_Click
   Dim PairedDevices As Map
   PairedDevices = Serial1.GetPairedDevices
   Dim l As List
   l.Initialize
   For i = 0 To PairedDevices.Size - 1
      l.Add(PairedDevices.GetKeyAt(i))
   Next
   Dim res As Int
   res = InputList(l, "Choose device", -1) 'show list with paired devices
   If res <> DialogResponse.CANCEL Then
      Serial1.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address
   End If
End Sub

Sub Serial1_Connected (Success As Boolean)
   If Success Then
      ToastMessageShow("Connected successfully", False)
      AStreams.Initialize(Serial1.InputStream, Serial1.OutputStream, "AStreams")
      Log("AStreams Initialized")
      'timer1.Enabled = True
      connected = True
   Else
      connected = False
      'Timer1.Enabled = False
      Msgbox(LastException.Message, "Error connecting.")
   End If
End Sub
Sub mnuDisconnect_Click
   If connected Then
      AStreams.Close
      Serial1.Disconnect
      connected = False
      Log("AStreams Closed")
   End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If UserClosed Then
        Log("closing")
        AStreams.Close
    End If
End Sub

Sub btnSend_Click
    Dim SerialMsg As String=""
    If connected Then
        If AStreams.IsInitialized = False Then
            Return
        End If
        If AStreams.IsInitialized=True And txtSend.Text.Length > 0 Then
            Dim buffer() As Byte
            SerialMsg = txtSend.Text & Chr(13)
            buffer = SerialMsg.GetBytes("UTF8")
            AStreams.Write(buffer)
            Log("Sending: " & SerialMsg)
            txtSend.Text = ""
        End If
    End If
End Sub
Sub AStreams_NewData (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
   txtLog.Text = txtLog.Text & msg
    Log("Rec: " & msg)
End Sub

Sub AStreams_Error
   Log("AStreams Error: " & LastException.Message)
    ToastMessageShow(LastException.Message, True)
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…