Additional Information

Site Information

 Loading... Please wait...

SAE J1939 to Bluetooth Gateway - Android Code (Java) Sample Code

Posted by Wilfried Voss on

Electronic Logging Device - ELD

Just as a fair warning upfront, the code that I am referring to in this post is not a finished sample code, but it demonstrates how to receive and decode SAE J1939 message frames (PGNs) from our JCOM Bluetooth device. 

To explain the situation: Some time ago, we had started a customer project with the intention to develop an ELD (Electronic Logging Device). The project was ultimately abandoned due to lack of funds. 

Note: An electronic logging device (ELD) is electronic hardware that is attached to a commercial motor vehicle engine to record driving hours. The driving hours of commercial drivers (truck and bus drivers) are regulated by a set of rules known as the hours of service (HOS). The Commercial Vehicle Driver Hours of Service Regulations vary in Canada and the United States.

During the project's initial lifetime, we developed a hardware that would meet the requirements of the ELD rule, including the necessary firmware. The hardware is described in our product page  CAN Bus / SAE J1939 Electronic Logging Device With Bluetooth, BLE, WiFi Option, and, after the project ended, we created a few SAE J1939 devices based on the same hardware, however, incorporating some optional modules to support Bluetooth, BLE (Bluetooth Low Energy), WiFi, GPS, and more.

At the same time, we developed a concept of our ELD device as described in our post  ELD Concept: SAE J1939 Data Recording And Display Using Android Or iOS Devices. Let me emphasize the point that we chose an Android tablet (not iPhone with iOS) not only as a data terminal but also as GPS and cellular connection provider. 

Note: The choice for Android over iOS was made due to practical considerations, such as price and reliability in an electrically harsh environment (i.e. a vehicle). Using BLE on a daily basis (iPhone, iMac in combination with airpods, keyboard, wireless mouse), I have come to doubt BLE's reliability in an industrial or vehicle environment.

We are, however, neither Android nor Java programming experts, so we hired an external resource to develop the Android Java code. The sample code that I am offering here receives the SAE J1939 data frames from our Bluetooth device and decodes them for further processing. The code also took some sample PGNs and displays them on the screen.

Note: The Java code is based on an older version of the Android OS and may need modification to suit upgraded versions. However, the code is suited to serve as a helpful resource for your own project.

Our product, the  SAE J1939 to Bluetooth Gateway With 9-Pin Deutsch Connection Cable, works perfectly in combination with our jCOM1939 Monitor, an SAE J1939 Monitoring, Analyzer and ECU Simulation software under Windows, and it is very well suited for an adaptation to Android (or iOS).

And while I am not convinced that BLE is a good option for ELD purposes, our Bluetooth device can be easily converted to BLE by replacing the BT module with a BLE alternative such as the one described in  Dual-Mode Bluetooth And BLE To USB Module.

Last, but not least, please be aware that the serial communication between the BT device and the receiving device (Andoid tablet/phone/phablet, PC, etc.) is well documented (see below resources). We also have developed sample code in C and C# to demonstrate the communication. As a matter of fact, we used a C# to Java converter to provide sample code for our Java developer.

Development Resources


Getting Started with Bluetooth Low Energy: Tools and Techniques for Low-Power NetworkingGetting Started with Bluetooth Low Energy: Tools and Techniques for Low-Power Networking

With Bluetooth Low Energy (BLE), smart devices are about to become even smarter. This useful guide shows how this impressive wireless technology helps developers create mobile applications that share data with external hardware and how hardware engineers can obtain easy and reliable access to mobile operating systems.

This book renders a reliable, high-level overview of how devices use BLE to communicate with each other. You learn about valuable low-cost tools for developing and testing BLE-enabled mobile apps and embedded firmware and get examples using various development platforms, including iOS and Android for app developers and embedded platforms for product designers and hardware engineers.

  • Understand how data is organized and transferred by BLE devices
  • Explore BLE’s concepts, key limitations, and network topology
  • Dig into the protocol stack to grasp how and why BLE operates
  • Learn how BLE devices discover each other and establish secure connections
  • Set up the tools and infrastructure for BLE application development
  • Get examples for connecting BLE to iPhones, iPads, Android devices, and sensors
  • Develop code for a simple device that transmits heart rate data to a mobile device

More Information...