AndroidBluetoothXamarin

Xamarin Forms and ODB2: Talking with the vehicles

xamarin-forms-obd2-logo

Today, I will present Xamarin Forms (XF) mobile application (iOS and Android) which talks to the vehicle. I will present demo app which reads vehicle speed, engine revolutions per minute-RPM and some other data, such as engine temperature.

For that I will use: Xamarin Forms as mobile development framework, On-board diagnostics – ODB2 as car diagnostics and reporting system and a Bluetooth as a communication technology.

To get you motivated, I will start from the end and I will show my mobile application first and how it works.

As you can see, my mobile app is reading speed, RPM and engine temperature parameters.

To start with, to build such type of application you need to know basics of:

For my experiment I used OBD2 Bluetooth dongle to communicate with the car. I plugged into my car’s OBD2 connector, as shown on bottom pictures.

my-car-obd2-ble-dongle

Here, I will not go into details how I manage to handle communication with BLE dongle, but you can read more about this in my blog post here: https://www.jenx.si/2020/08/13/bluetooth-low-energy-uart-service-with-xamarin-forms/.

Let’s do some coding

In Visual Studio 2019 I created new Xamarin Forms project with Shell template. Next, I added some awesome NuGet dependencies, like Prism.Forms, Prism.Unity.Forms and Xamarin.Essentials. Furthermore, for my speed and RPM gauges I used trial version of Syncfusion.Xamarin.SfGauge GUI library. For Bluetooth communication, as usually, I used super-awesome Ble.Plugin component.

Just a glimpse into my solution when everything was on place:

Let’s look at the App.xaml.cs class and AppShell.xaml where general structure of my app is defined:

My App and AppShell

Nothing unusual here, except that my application inherits from PrismApplication. This way I can use Prism.Unity Inversion of control (IoC) container functionality in my Xamarin Forms application.

This time (regarded to my past demos and blog post) my app uses MVVM pattern, service Dependency injection approach and other fancy features. With all these features, I am one step closer to more modular, organized and more extensible type of the application.

Now, let’s look basic app pages.

Connect Page

View

ViewModel

Look & Feel

connect-to-my-car-page

Kmph page

View

ViewModel

Look & Feel

km-per-hour-page

RPM page

View

ViewModel

Look & Feel

rpm-page

Info page

View

ViewModel

Look & Feel

car-status-info-page

Code is more or less self-explanatory, so no extra comments are needed. Basic work here is reading OBD2 with BLE dongle, parse ODB2 responses and bind data to views.

Other small tricks

Navigation service

I frequently use Prism & Unity toolkits with my projects. For different purposes: Dependency Injection, as Service Locator, event aggregation, for Navigation, Popups, modularity and similar.

Currently, Prism is not yet fully compatible with XF Shell. For instance, navigation is not working with XF Shell. So, my first update was custom INavigationService. The idea was to navigate Xamarin forms Shell from my ViewModels. Here is the one very quick fix:

And my implementation:

Usage is very simple – after injecting this singleton into ViewModel I can call it like this:

and I am navigated to the requested route defined in my Shell.

Activation or navigation aware ViewModels

In past, with Prism navigation service and INavigatedAware mechanism, I could use built-in mechanism which tells when some view is activated (navigate to) and deactivated (navigate from). Again, with broken Prism/XF Shell functionality, this is not currently available. So, I needed to cope with some other trick here. I created BaseContentPage and wire up page life-cycle events to Page ViewModel binding context. Quick and efficient alternative to Prism’s INavigatedAware.

This way I could control my page/viewmodel activation and deactivation.

OBD2 over BLE – communication layer

As stated before, ODB2 over BLE is described a little bit in more details here: https://www.jenx.si/2020/08/13/bluetooth-low-energy-uart-service-with-xamarin-forms/

Here, I will just show out two APIs definition how I managed to handle all the communication:

IObd2BluetoothManager, which handles BLE communication (connect, disconnect, scan, persist connection, handles disconnect, etc…)

and

IUartManager, which handles ELM327 and OBD2 communication on top of BLE layer.

Summary

Of course, my app if far from being perfect. But it’s a start. Now, I have basic template to work on it and put some additional features on top.

In this blog post I showed how with combining different technologies and tools one can easy achieve very interesting applications. In my case presented here- mobile application which talking to the cars!

Happy coding.

2 thoughts on “Xamarin Forms and ODB2: Talking with the vehicles

  1. Hello! I followed this guide but got an error. If it’s possible, can you send me the project? This is the only project I’ve found that is used in Xamarin.Forms!

    Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.