December 20, 2016


Building Desktop Apps with Electron for Xiaomi

Node.js Software Development Startups Co-Building

Smart apps designed to make our lives easier are becoming increasing popular. These range from fitness trackers to lightbulb controls.  When designing these apps, you may find you need to improve the user experience.  Or you need to access the raw data, but setting up a full stack project would be overkill for this purpose. That is when Electron comes into play.

Electron is a new way to develop modern desktop apps with all the power of Node.js, HTML, and CSS.  You can easily complete a functional app in a short time frame, which could solve 80% of your day-to-day needs.

In this example, we are going to explore the Electron universe by creating a simple app that will give us access to the IoT world.  Integrating scales from Xiaomi, we will build a “Smart Scale” app that will show the user’s weight in real time. With Electron, the data will be collected and presented in a nice, clean format.

 

STEP 1:  SETUP

First, we need to make sure the setup is done properly. Many templates are available for this step, but we are going to simplify this process by starting with a basic setup:

 

 

Once you have completed this step, you will have something like this:

 

 

* index.html  is the app’s main view.
* main.js is the app’s entry point.
* renderer.js is where all the Node.js staff is available.

 

STEP 2:  DIVIDE THE  PROCESS INTO 3 PARTS

In order to build our app, we need to split it into 3 parts:

  1. GUI and UX
  2. Data service
  3. Integration of data & UI

Our Smart Scale is built on BLE (Bluetooth Low Energy), which is a very nice protocol. In NPM land, the https://github.com/sandeepmistry/noble project provides us low-level API for basic Bluetooth interaction.

But we can go even further. A quick search on github will give us a ready-to-use NPM package for our Smart Scale (https://github.com/perillamint/node-xiaomi-scale):

 

 

If you have problems building these native extensions, you can check this discussion:(https://github.com/opensprints/opensprints-electron/issues/5).

 

STEP 3:  ENTER DATA/CODE

Now we are ready get our first real data into the app.

Our UI will be pretty simple:

 

 

We can get sample code from the node-xiaomi-scale package and put it, as well as our data layer, into the renderer.js file, and we are good to go:

 

 

STEP 4:  START YOUR APP

Here’s what we need to start our app:

 

 

![First Real Data](assets/first_data.png)

 

STEP 5:  MAKE YOUR APP COMPATIBLE WITH ALL PLATFORMS

Now we just need to polish our app and build it for all available platforms.

To get a nice, clean UI, we are going to use http://ignitersworld.com/lab/radialIndicator.html:

 

 

Here is our UI:

 

 

Here is our data layer:

 

 

STEP 6:  CREATE AN EXECUTABLE FILE

Now our app is just about ready. We need to start here: ![app](assets/real_app.png)

Then we can further build on our app:

 

 

We can also add some sections to our package.json:

 

 

Now we can run our app:

 

 

Electron is a great platform for building desktop apps in a matter of minutes. It allows you to jump right into development with minimal platform-specific knowledge.  You don’t have to be familiar with edge cases or various languages to set up a simple app that will work on MacOS, Windows, and Linux. You just need to have experience with JavaScript and CSS.  Plenty of tutorials and great starting materials are also available. So, if you are interested in Desktop App development, you can now get started.

This application can be found on github at  https://github.com/SphereSoftware/smart-scale-app 

Node.js Software Development Startups Co-Building

Latest Insights in Node.js

The Rise of Kotlin – Moving Away from Java for Android Development

Kotlin is a programming language for the Java Virtual Machine that’s able to be used in any scenarios that currently…

Introducing our Sphere Heroes Program – Artem Korenev – Employee of the month

At Sphere, employee recognition is a key component of our corporate culture. We believe in celebrating the successes of our…

Write For Sphere

Are you a writer with tech expertise? Then we want to hear from you! Here are a few guidelines for…

View All Articles arrow

We are here to help:

checkmarkto become a customer checkmarkto become an investor checkmarkto send a media inquiry checkmarkto join our team checkmarkto simply say ‘hi’
Get in Touch