A while back I posted an article about an Android app I wrote that allows you to perform real-time vehicle diagnostics using Google Glass and an OBD-II adapter. At the end of that article, I promised I would share the source code once I completed it. Unfortunately, I have lost the final source code for that project. Earlier today, I received an email from a reader asking for an explanation of how to read data from the OBD-II adapter. Since I’ve received several other emails asking this same question, I thought I would turn my reply to that email into a post to share with anyone else that might be interested in doing this.
BTW, I did manage to find an earlier version of that source code in one of my backups which I will share a link to at the end of this article. Just be warned, though, that the code is very messy (there’s a lot of commented out debugging stuff in there), it isn’t documented, and it doesn’t include all of the stuff mentioned in the video at the link above. It also only supports reading RPM & speed information, but I had also started adding support for MPG which wasn’t finished at the time this backup was made. But, this code and the following explanation should be enough to get you started with creating your own Android app that allows you to perform real-time vehicle diagnostics using your Google Glass.
So, communication with the OBD-II adapter requires sending “AT” Hayes commands (just like the old commands used for communicating with modems – “AT” is short for “Attention”). The first command you always send is “AT” plus a command to indicate attention from the device.
For working with OBD-II, the first command you always send is “AT SP 0”. This tells the adapter to automagically detect the protocol whenever you send your first OBD request. If everything worked, you should get a “OK” response.
You will always follow this up by sending the “AT DP” command (“Display Protocol”) which will verify the protocol which should return “AUTO”.
Next, you will want to issue commands for retrieving whatever it is you’re looking for and register a callback to process the response from your commands. Your command will always be 2 bytes. The first 2 characters / first byte are the mode you want to work with (most often it will be “01” or Mode 1 which returns real-time data. If you want historical snapshots, you’ll use “02” – Mode 2). The second 2 characters / second byte will be the PID for the command you want to issue. For example, if you want to get your vehicle’s RPMs, you would send “010C”. If you want to get the vehicle speed, you would send “010D”.
You can get a list of PIDs at http://obdcon.sourceforge.net/2010/06/obd-ii-pids/.
When you send these commands, you’ll get back a list of bytes which you’ll have to decode using a lookup table (much like the one listed at the link above). The response should begin with 2 bytes. The first byte should be “41” which identifies a successful response for Mode 1. The second byte will be the command you sent (ie. “0C” for RPMs or “0D” for speed). After those 2 bytes, you will receive the number of bytes returned indicated by the third column in the lookup table at the link above.
Here’s an example for getting the RPMs.
If you want to get the RPMs, you will send the following (without the leading >).
Which will return
41 0C 0F A0
The first 2 bytes (41 0C) tell us that it received a good request for RPMs and that it’s now returning a response of “0F A0”. To calculate the RPM value, you would use:
0x0FA0 = 4000
4000 / 4 = 1000 rpm
When working with speed, you would do this:
41 0D FF
0xFF = 255 km/h
You can find all of those formulas at the link above and can convert the data to the measurement of your choice (ie. mph instead of km/h).
To help you get started creating your own Google Glass app that communicates with an OBD-II adapter over Bluetooth, you can download my example app mentioned above at Real-time diagnostics with Google Glass and OBD-II.
That’s it. If you have any questions about any of this, please feel free to post them in the comments below.
PayPal will open in a new tab.