Android Question bluetooth serial with OBDII-Dongle

Coltracer

Member
Licensed User
Longtime User
I'm having some problem connecting to a serial bluetooth OBDII-Dongle. I basically took the Chat Example tutorial to send an ATZ command to the Dongle (which basically behaves like a serial modem).

The problem is not talking to the dongle, if I finally get the connection up it works fine. But connecting is giving me a headache. Sometimes I get a [JSR82] connect: Connection is not connected: false error, sometime I get java.io.IOException: Service discovery failed and sometimes it works and I can send data.

Sometimes disabling/enabling bluetooth helps, sometimes restarting the tablet is necessary.

To speed things up I even tried removing the discovery code and substituting a direct connect to the known and paired device via serial1.connect("00:02:5D:B3:52:F1"). It makes no difference. The weird thing is, sometimes If I retry a couple of times it will suddenly connect.

Am I missing something? Is there a simple example anywhere, without using discovery, pairing, etc, where I can just fire up the app and connect to a known device via Mac-adress?

Here's my code, which you can see I mangled a bit. I put the connect command in multiple places, so I can see whats happening:

B4X:
'Activity module
Sub Process_Globals
    Dim admin As BluetoothAdmin
    Dim serial1 As Serial
    Dim foundDevices As List
    Type NameAndMac (Name As String, Mac As String)
    Dim connectedDevice As NameAndMac
End Sub

Sub Globals
    Dim btnSearchForDevices As Button
    Dim btnAllowConnection As Button
End Sub
Sub Wait(Seconds As Int)
  Dim ti As Long
  ti=DateTime.Now+(Seconds*1000)
  Do While DateTime.now < ti
    DoEvents
  Loop
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    If FirstTime Then
   
        admin.Initialize("admin")
        serial1.Initialize("serial1")
        Wait(10)
        'serial1.Connect("00:02:5D:B3:52:F1")
    End If
   
End Sub

Sub Activity_Resume
    btnSearchForDevices.Enabled = False
    btnAllowConnection.Enabled = False
    If admin.IsEnabled = False Then
        If admin.Enable = False Then
            ToastMessageShow("Error enabling Bluetooth adapter.", True)
        Else
            ToastMessageShow("Enabling Bluetooth adapter...", False)
            'the StateChanged event will be soon raised
        End If
    Else
        Admin_StateChanged(admin.STATE_ON, 0)
    End If
End Sub

Sub Admin_StateChanged (NewState As Int, OldState As Int)
    btnSearchForDevices.Enabled = (NewState = admin.STATE_ON)
    btnAllowConnection.Enabled = btnSearchForDevices.Enabled
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True Then
        serial1.Disconnect
    End If
End Sub

Sub btnSearchForDevices_Click
    'foundDevices.Initialize
    'If admin.StartDiscovery    = False Then
    '    ToastMessageShow("Error starting discovery process.", True)
    'Else
    '    ProgressDialogShow("Searching for devices...")
    'End If
    'Admin_DiscoveryFinished
    serial1.Connect("00:02:5D:B3:52:F1")
End Sub

Sub Admin_DiscoveryFinished
'    If foundDevices.Size = 0 Then
'        ToastMessageShow("No device found.", True)
'    Else
'        Dim l As List
'        l.Initialize
'        For i = 0 To foundDevices.Size - 1
'            Dim nm As NameAndMac
'            l.Add(nm.Name)
'        Next
'        Dim res As Int
'        res = InputList(l, "Choose device to connect", -1)
'        If res <> DialogResponse.CANCEL Then
'            connectedDevice = foundDevices.Get(res)
'            ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
'            serial1.Connect(connectedDevice.Mac)
            serial1.Connect("00:02:5D:B3:52:F1")
'        End If
'    End If
End Sub

Sub Admin_DeviceFound (Name As String, MacAddress As String)
    Log(Name & ":" & MacAddress)
    Dim nm As NameAndMac
    nm.Name = Name
    nm.Mac = MacAddress
    foundDevices.Add(nm)
    ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))
End Sub

Sub btnAllowConnection_Click
    'this intent makes the device discoverable for 300 seconds.
    Dim i As Intent
    i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
    i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
    StartActivity(i)
   
    serial1.Listen
End Sub

Sub Serial1_Connected (Success As Boolean)
    ProgressDialogHide
    Log("connected: " & Success)
    If Success = False Then
        Log(LastException.Message)
        ToastMessageShow("Error connecting: " & LastException.Message, True)
    Else
        StartActivity(ChatActivity)
    End If
End Sub
 

Coltracer

Member
Licensed User
Longtime User
Hi Erel,

thanks for your replay!

The Dongle listens after power on for a while, then powers down into powersaving mode, when there's no vehicle communication. But that's another problem. If I power on, the Torque app from Google Play Store connects faithfully everytime.

I'll try the the 2 alternatives you mentioned. Do I have to set the port values to something specific?
 
Upvote 0

Coltracer

Member
Licensed User
Longtime User
I tried all 10. None of them wanted to connect. I also tried the bluetooth code from this project (GPS_NMEA) and it's the same thing (actually I just tried the whole project and simply pressed connect). I have to press connect multiple times before it will connect. Sometimes I have to reboot for it to work at all. When it connects it's really stable and doesn't go away again.

The really weird part is, even if it wont connect at all anymore and I would have to reboot, I simply start "torque pro" and it connects right away. What does this app do to reset the bluetooth that basic4android can't? I even tried turning bluetooth off and on within the app. It just wont connect.
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
I tried all 10. None of them wanted to connect. I also tried the bluetooth code from this project (GPS_NMEA) and it's the same thing (actually I just tried the whole project and simply pressed connect). I have to press connect multiple times before it will connect. Sometimes I have to reboot for it to work at all. When it connects it's really stable and doesn't go away again.

The really weird part is, even if it wont connect at all anymore and I would have to reboot, I simply start "torque pro" and it connects right away. What does this app do to reset the bluetooth that basic4android can't? I even tried turning bluetooth off and on within the app. It just wont connect.
Hi:
I'm also trying to make a app to work with my obd2, can you help me?
I can connect to the obd but nothing comes in.
Do I need to send a request for info?
if so what commands do I send?
or can you tell me where I can find this commands.
Like send info.
Get faults.
reset faults.
etc.

Thank you: for any help you can give me.
 
Last edited:
Upvote 0

Coltracer

Member
Licensed User
Longtime User
That depends on your interface chip used. If it's an ELM327 there's a full specsheet with commands available here. There are some basic commands which work even without a car attached so you can benchtest your app. all you need is 12V on Pin 16 and GND on Pins 4 and 5. ATZ (reset), ATI (info) and ATRV (request voltage) should work.
Here's how it looks, IF I can get it to connect.

Screenshot_2013-10-28-23-15-59.png


You can find OBDII-PID explanations at Wikipedia
 
Upvote 0

Coltracer

Member
Licensed User
Longtime User
OK, I've found the solution and it's pissing me off.

If I use serial1.connect(someMACAdress) it hardly ever connects.

If I use serial1.connect2(someMACAdress,"00001101-0000-1000-8000-00805F9B34FB") it works EVERY SINGLE TIME.

The Online-Help states:

15.png
Connect (MacAddress AsString)

Tries to connect to a device with the given address. The connection is done in the background.
The Connected event will be raised when the connection is ready (or fails).
The UUID used for the connection is the default UUID: 00001101-0000-1000-8000-00805F9B34FB.

So in my understanding, the 2 lines of code above should be exactly the same. Well, they surely don't behave the same.

Erel, care to comment on this? Is this a library error or some quirk in Android?
 
Upvote 0

Coltracer

Member
Licensed User
Longtime User
Well, then I simply cannot explain, why one line works and the other one doesn't. And there's no typo in my code, I've checked a gazillion times.
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
Th
Well, then I simply cannot explain, why one line works and the other one doesn't. And there's no typo in my code, I've checked a gazillion times.
Thanks:
Is your program freezing up after you send a command ?
ATRV
Mine does, have to end and start program then it works again.
By the way mine connected with "Serial1.Connect3(PairedDevices.Get(l.Get(res)),1)"
if this helps any.
 
Upvote 0

Coltracer

Member
Licensed User
Longtime User
You're using the Textreader in the timer from the old example. That hung my application too. Use asyncstreams. Unfortunately that chops up the obd2 Data. Thats Not so tragic while reading, you can piece together by hand. But Thats a nogo while writing. I use textwriter for writing and an asyncstream for reading. I'll post my code tomorrow, i'm already in bed. ;-)
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
You're using the Textreader in the timer from the old example. That hung my application too. Use asyncstreams. Unfortunately that chops up the obd2 Data. Thats Not so tragic while reading, you can piece together by hand. But Thats a nogo while writing. I use textwriter for writing and an asyncstream for reading. I'll post my code tomorrow, i'm already in bed. ;-)
:)
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
Hello:

Is it possible to request a value identifier from the obd to return together with the response?
exp: at rv + (cr)
response: 12.5v voltage

Thank you for your help :)
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
Hello:

Is it possible to request a value identifier from the obd to return together with the response?
exp: at rv + (cr)
response: 12.5v voltage

Thank you for your help :)
Hello:
Have you been able to get your app to work ?
I'm having all kind of problems.
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
Hello:
Have you been able to get your app to work ?
I'm having all kind of problems.
Hello:
Well I made some head way:
With the one request: 01 05
I can get the info I need, but I need to use the "ATMA"
and I'm a little lost:

Settings= at L1,at H1,at S1,at AL,at SP0

With the "at ma"
I get:
8A EA 29 20 A7 00 30
AA EB 60 20 A7 00 E7
E8 FF 10 03 B3
88 25 29 07 00 97
68 49 10 10 56 90
E8 FF 60 03 98
88 3B 10 43 80 0D
48 FF 40 06 03 AC
8A EA 40 A0 9E 50 26
AA EB 60 A0 9E 50 2E
8A EA 10 A0 8C 00 65
8A EA 10 20 8C 00 A5
88 53 10 04 C4
8A EA 10 A0 8C 00 65
4A EA 40 20 91 00 6B
48 FF 40 06 03 AC

What I need to do is find out which byte(s) is the pid, data etc.
I have been looking at the " en.wikipedia.org/wiki/OBD-II_PIDs‎"
Can't make heads or tails :-(

Can you please HELP ? :(
 
Upvote 0

goflygaz

New Member
Licensed User
Longtime User
You're using the Textreader in the timer from the old example. That hung my application too. Use asyncstreams. Unfortunately that chops up the obd2 Data. Thats Not so tragic while reading, you can piece together by hand. But Thats a nogo while writing. I use textwriter for writing and an asyncstream for reading. I'll post my code tomorrow, i'm already in bed. ;-)

I have a similar application and this is helping me get going, did you ever post the code that worked with asyncstream being used for the read? - much appreciated if you could please.
 
Upvote 0
Top