Smart Switch using Xamarin, MVVMCross and Arduino

The project originated from hesitation to get up in middle of the night to turn fan/heater on off. One should be able to do it by pulling out phone from under the pillow.

Hardware side has been done using Arduino, Bluetooth and Power relay. The software side has been built using Xamarin Studio, Visual studio 2013, MVVMCross for Android and Windows phone.

The hardware details will shortly be published on hackster.io.
https://www.hackster.io/sameerk/smart-switch

This blog discusses the App side of the project:
This project has same workflow across all platforms and the Ux has the similar set of elements. That code could be common across all platforms. The components that need to be different across platforms are Ux elements and the Bluetooth communicator. The common code message Bluetooth manager using IOC. The data in the common code can be bound to the Ux elements on different platforms using Data Binding framework.

This requirement leads to looking for frameworks that support MVVM design pattern. Several choices are available for this including Xamarin.Forms, MvvmCross, MvvmLight and many more. This app has been developed using MvvmCross.

The Solution has three projects.

  1. The core library consists of the ViewModels, Interface definitions and related objects.
  2. Android project consists of Bluetooth implementation for that platform and App screens.
  3. Windows Phone project consists of Bluetooth implementation for windows phone platform and App screens.

When app starts it will register Bluetooth manager instance corresponding to the platform. The core app will push the start view model and views container will display the corresponding view.

When user taps Refresh button, command in the viewmodel obtains IBluetoothManager from IOC container. It queries the paired devices. It raises property change to enable corresponding buttons.

There is similar command pattern used for Connect button and toggle switch. The details are in the code.

Logic to handle user workflow is wrapped in View Model it can be shared across platforms.

Exceptions are captured by ViewModel and displayed as error messages. ViewModel also displays other status messages.

Leave a comment