Bluetooth Low Energy (BLE)

ble-graphic2

WARNING THIS BLOG IS VERY INFORMATIC IN ITS NATURE, YOU HAVE BEEN WARNED.

In this blog post, I will be covering what I’ve researched and learned about the new Bluetooth protocol called “Bluetooth Low Energy” “(BLE)” also known as “Bluetooth Smart”.

In 2009 Bluetooth SIG announced a new version of Bluetooth noted as V4.0 that included a new protocol called BLE. Originally BLE was made by Nokia in 2006 under the name Wibree, however, Wibree was merged into the main Bluetooth standard in 2010 with the release of Bluetooth V4.0.

So what makes Bluetooth V4.0 so special? Well Bluetooth V4.0 operates two different protocols. Number 1 being Bluetooth classic, and number 2 being Bluetooth Low Energy, both protocols operate under the 2.4 GHz ISM band, however, both protocols are not compatible. BLE uses a 40 2MHz-wide channels while Classic uses the 89 1MHz-whide Channels, but one thing both protocols have in common is the ability to use frequency hopping to spread their RF energy.

Bluetooth Low energy, however, is unique in comparison to Bluetooth Classic, not only in the sense of the 40 2MHz-wide Channels but also in how it operates and how its operation allows for low power consumption. With BLE communications two types of communication’s come into play “GAP” and “GATT”, both operate differently, however, comes with its beneficial applications.

 

Generic Acess Profile (GAP)

GAP defines how BLE devices can make them selfs available to each other and how two devices can communicate with each other. GAP plays four different roles that allow for communication to take place:

  1. Broadcaster
  2. Observer
  3. Peripheral
  4. Central

 

Broadcaster – Broadcasts public advertising data packets, an example of this would be as if a button was pressed every 20 – 40ms to show that a signal is taking place within a system however in terms of Bluetooth Low Energy application, these advertising data packets may come as a local thermometer presenting its latest temperature update, or for a business to advertise its latest sales.

Observer – An observer device listens to the data in the advertising packets that is being sent by the broadcaster, an observer can be a range of different devices that is programmed to take in this information, such as a smartphone that is take in that temperate update or present the sales deals.

Note: Overserver and Broadcaster roles do not need to connect in order to transfer data. These two roles are typically used in Bluetooth beacon technology.

Peripheral – Peripheral devices make its presence known through advertising a data packet to allow for a central device to establish a connection. After a connection has been made by a central device the peripheral device stops broadcasting to other central devices and stays connected with the device that has accepted the connection request.

Central – Central devices initiate the connection between its self and a peripheral by the first listening to the advertised data made by a peripheral device. When a central device wants to connect it sends a request connection data packet to a peripheral device. If the peripheral device accepts the request from the central device, a connection is therefore made. Another note about central devices is its ability to connect to many other peripheral devices. Central device can also update the connection parameters between the two devices, the peripheral device cannot, however, the peripheral device can ask permission to the central device to update the connection parameters.

Note: Central or Peripheral devices can terminate the connection made, both devices can terminate connections intentionally, other reasons why the connection may break is due to power issues (such as the device’s batteries dying out) or out of signal range.

Do take a quick few minutes to process that information before moving onto GATT

Generic Attribute Profile (GATT)

GATT defines the roles how two BLE devices send back and forth data using concepts called “services” and “characteristics”. However similar to GAP, GATT is very different as the connection is exclusive to the two connected devices. A GATT connection is only made after a central and peripheral device has gone through the GAP process and updated its communication parameters.

A GATT connection requires two devices whose roles are followed:

 Client – Sends a request to the GATT server, the client can either read and/or write attributes found in the server

Server – the role of the server is to store the attributes, once the client makes the request, the server must make the attributes available.

Client-Server Relationships

An example of a client-server relationship would be. If my accelerometer test device were fully operational and is experiencing movement, and I want to know what type of movement is being experienced by my test device in the form of updated graphs or data through my phone. The test device acts as the server and provides the information, while the phone acts as the client reading the information.

But one interesting point about Client and Server relationships is the fact the roles can be swapped depending on how information is flowing, for example, if the phone needed to send an update for the test device, now the phone would act as the server while the test device acts as the client. Another interesting point would be the fact that central and peripheral devices can act as either a client or server as silently hinted from the previous sentence, but in terms of GAP and GATT communications, both are independently different from one another.

 

Conclusion

In the world of IoT (Internet of things) many new ideas for devices have been under development, mediums such as BLE or NFC have already quickly changed how many of us on earth operate for example contactless payments via card was a NFC “thing” that came out 3 to 5 years ago, now many countries are slowly moving to using their phones via NFC for contactless payments. In nursing homes, Fitbit-like BLE enabled devices are now potentially becoming both a tracker and heart monitor for residents or even patients in a hospital.

Many of these “things” are still the research and development stage however with active devices using BLE, more devices can be interconnected to one another, especially with Bluetooth 5 and their upcoming feature called “Bluetooth Mesh” what will create a whole network of different BLE devices together.

Bluetooth V4.0 – V4.2, and Bluetooth 5 are setting the scene for future and new IoT applications in almost every and any sector.

Resources:

EDN-Europe: http://www.edn-europe.com/blog/bluetooth-low-energy-ble-short-history-ble-standard-and-gatt

Bluetooth SIG: https://www.bluetooth.com/what-is-bluetooth-technology/how-it-works

Adafruit: https://learn.adafruit.com/introduction-to-bluetooth-low-energy/introduction

Punch Through Designs: https://punchthrough.com/bean/docs/guides/everything-else/how-gap-and-gatt-work/

 

Advertisements

My Cordova Environment

For this blog post, I will be covering the do’s and didn’t do’s for my Cordova Environment which was operating on a Ubuntu 16.01 on a virtual machine.

Minor Details:

Originally I had intended on using my windows laptop to build my app however from all the junk that had been downloaded, installed, accumulated over previous projects, I have decided to us a Virtual Machine running Ubuntu on it to use as my App development Environment, however, due to hardware issues (my computer being not simply powerful enough) I had to return to my old web developing friend my MacBook Pro. Thankfully it is powerful enough run a fast Ubuntu build. Moving on.

Apache Cordova is a Mobile Application Framework that allows software programmers to build mobile applications using a web stack (HTML5, CSS3, JS) instead of using platform-specific APIs for Android, IOS, or Windows which would be three or more different software. It reduces it to one framework that allows you to build apps towards the platform of choice with plugins eg. IOS or Android.

This creates a hybrid application that neither native mobile (due to rendering done via web views) nor is purely web-based (they are not viewed on a browser though packed as standard mobile apps via installing platform e.g. IOS App Store or Google Play Store).

Environment Build (WARNING!! This may get tedious and boring)

Just to warn anyone who is following this example of mine, I had originally started with 15Gb for my Ubuntu OS running on my virtual machine, now this was expanded first to 20Gb and then 100Gb. This was done by using the following command on the terminal (note: my operating system is OSx).  Also before typing the code below make sure to be in the directory of the file. If anyone is wondering what is using most of this disk space, it is the Android SDK. And for anyone who is curious, the total amount of disk space that was the Ubuntu environment used about 23GB.

 


               Mac Terminal: VBoxManage modifyhd “<vdi_file_full_path_and_name>” –resize 40000

A list of Things to Install:

  • NodeJS
  • NPM
  • GIT
  • Cordova
  • Java
  • JavaC
  • Ant
  • Android SDK Tools

NodeJS

Type the following in the Terminal:


               sudo apt-get install nodejs

To see if nodejs works type this into the terminal:


nodejs --version

If Nodejs is installed, the latest version should show up in the terminal.

Next, in order for Cordova to work, Nodejs needs to have an alias “node”.


               sudo ln -s /usr/bin/nodejs /usr/bin/node

 

NPM

Type this in the Terminal:


               sudo apt-get install npm

To see if NPM is installed:


               npm --version

GIT

To install GIT on Ubuntu type the following:


               Sudo apt-get install git

Bored yet? Don’t worry were almost done…


 

Cordova

Cordova yay! type this into the terminal:


               Sudo npm install -g cordova

And lastly to see if Cordova was installed properly do type the following in the terminal:


               cordova --version

Now Apache Cordova is fully installed however in order to start building apps another few pieces needs to be installed and set to PATH in order for the web stack to be converted into an App. and if you don’t believe me do type:


               cordova requirements

into your terminal. This should return a list with the following:

  • Java
  • Javac
  • Ant
  • Android SDK

So let’s keep at it.


 

Java

In order for the Android SDK to be installed, the Java Development Kit (JDK) needs to be installed.  To check if the JDK is already installed type the following into the terminal:


java -version

javac -version

If a version does pop up, that means Java is already installed, however, that Java is an OpenJDK rather than the Oracle JDK which is needed for signing Android apps.

The following steps show the process in installing the Oracle JDK.

First: We need to remove the OpenJDK by typing the following commands into the terminal:


sudo apt-get purge openjdk-\*

sudo apt-get autoremove

 

Next, we need to install a 32-bit library since were using a 64-bit Linux machine by typing the following:

Note some examples may say install the “ia32-libs” however that library isn’t available anymore, and running that command will direct you to lib32z1 unless it has been updated again.


               sudo apt-get lib32z1

 

Next the JDK “Java SE Development Kit” needs to be downloaded from the oracle website.

 

 

If using a 32 Bit system, make sure to download the Linux x86 and for 64 bit systems do make sure to download Linux x64. Also, make sure to download the “.tar.gz”.

Note: please do know whatEverYourFileNameIs.tar.gz will be replaced by whatever the real name of your downloaded file is.

 

 


mkdir -p ~/java 

cd ~/Downloads 

mv whatEverYourFileNameIs.tar.gz ~/java 

cd ~/java 

tar xvzf whatEverYourFileNameIs.tar.gz

rm whatEverYourFileNameIs.tar.gz

 

Now we must check the name of the JDK install directory with:


ls

 

The path to the JDK will be displayed in the terminal, and will look something like “~/java/jdk1.8.0_11”. Copy whatever directory the terminal has produced.

 

Now to add the JDK to bath we ned to edit the .bashrc in our home directory. To do this you can either use “nano” or “gedit”, I used gedit as I’m used to that method, however both work accordingly.

 


nano ~/.bashrc

or:

gedit ~/.bashrc

 

Add the following lines to the end of the code, which you might have copied in the last step.

 

 

 

 

 


JAVA_HOME=~/java/”FolderName”     PATH=$PATH:~/java/”FolderName”/bin 

               export JAVA_HOME

               export PATH

 

Now we must edit the .bash_profile in the terminal(make sure it’s in the home directory).

 

Simply type again I prefer the gedit.

 


nano ~/.bash_profile

or:

gedit ~/.bash_profile

 

 

Once gedit is opened paste the following in the file and save it.

 


               if [ -f ~/.bashrc ]; then   

                               source ~/.bashrc

               fi

 

now that should be everything. To see if its working type the following in the terminal


               javac -version 

               java -version

If version info has displayed you are done with the JDK install!

 

 

 

 

Ant

 

Apache Ant is a build system for Java, which is used by both Cordova and the Android SDK. Click here to get to the link and download the stable version. Be sure to downlaod the “apache-ant-whateverVersion-bin.zip” file


mkdir -p ~/ant 

cd ~/ant 

unzip apache-ant-1.9.4-bin.zip 

rm apache-ant-1.9.4-bin.zip 

 

now a familiar process will take place, we need to add Ant to PATH we do this again by editing the .bashrc file by first typing. Again I use the gedit method.

 


nano ~/.bash_profile

or:

gedit ~/.bash_profile

 

and just underneath your Java export and before “export PATH” and the bottom of the file add the following

 

</strong>

ANT_HOME=~/ant/apache-ant-1.9.4 PATH=$PATH:~/ant/apache-ant-1.9.4/bin

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; export ANT_HOME

<strong>

 

 

 

 

The code should look like this:

 


JAVA_HOME=~/java/jdk1.8.0_11 PATH=$PATH:~/java/jdk1.8.0_11/bin

export JAVA_HOME 

ANT_HOME=~/ant/apache-ant-1.9.4 

PATH=$PATH:~/ant/apache-ant-1.9.4/bin 

export ANT_HOME 

export PATH

 

Now to test to see if the install worked simply type in the terminal

 


ant –version

 

If a version number shows up, that shows Ant has been installed successfully.

 

 

 

Lastly the android SDK Tools

Android SDK Tools are used by Cordova to build Android apps.

 

First you need to download the SDK tools which will be located at the bottom of their downloads page. Downlaod the zip file.

Now follow the next set of steps:

 


mkdir –p ~/android-sdk

cd mv ~/Downloads/android-sdk_whatEverVersion-linux.zip

unzip android-sdk_r23.0.2-linux.zip

rm android-sdk_r23.0.2-linux.zip

 

now that the files are inplace we have to add this to PATH.

 

Add the following two lines to the .bashrc that can be accessed in the terminal by typing :


nano ~/.bash_profile

or:

gedit ~/.bash_profile

 

 

 

 

 

 

Again I like to use gedit, now add this code before the “export PATH” and after the export “ANT_HOME”.

 


PATH=$PATH:~/android-sdk-linux/platform-tools 

PATH=$PATH:~/android-sdk-linux/tools

 

Now your .bashrc should look a lot more like this.

 


JAVA_HOME=~/java/jdk1.8.0_11 PATH=$PATH:~/java/jdk1.8.0_11/bin 

export JAVA_HOME

ANT_HOME=~/ant/apache-ant-1.9.4 

PATH=$PATH:~/ant/apache-ant-1.9.4/bin 

export ANT_HOME 

PATH=$PATH:~/android-sdk-linux/platform-tools 

PATH=$PATH:~/android-sdk-linux/tools

export PATH

 

Note: the Android-SDK tools may not come with the platform-tools component, if the “Platform-tools” are not seen in your “Android-SDK” after you have unzipped the Android-SDK, the platform-tools component is available here. And to add the platform-tools component to path follow the steps above on how the Android SDK was installed.
Final Steps
Now that everything is almost fully installed we just need to use the Android-SDK-manager to download the necessary API in order to make Android apps build correctly. To open the Android-SDK-manager simply type the following into the terminal:


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Android

 

Now the software should pop up and give you a list of Android-API’s that is installed. As stated in the Apache Cordova website link, the version of Cordova you should have installed is 6. Version 6 requires API levels between 14 – 23. So it’s safe to select API 23 in the SDK-Manager, however for future references if something is not working correctly within your app build it maybe safe to assume that if you fall back on to a previous API build eg. API 14 – 22, it may fix the problem. Just to warn you the API’s were the everything that filled up my hard disk space within my virtual machine.

 

Now just to see if everything installed properly. Let’s open up the terminal and type:


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; adb version

 

Which will display the version of the Android debug bridge.

 

 

 

Now type in the terminal after everything has finally been installed and updated. Just to be sure the “PATH”ed files are working which should both display two directories.

 


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $JAVA_HOME

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $ANT_HOME

 

 

 

Updated 25/5/17

 

With the latest Cordova updated a few small changes have to be made

 

First, lets update Cordova


Sudo npm –g cordova

Ok

Next Gradle has to be installed separately.

Download the latest build here: https://gradle.org/releases

Download the zip file, and create a folder called “gradle” in “home” directory

Move the zip file into the gradle filder, and then unzip it, then remove it

Open up gedit ~/.bashrc and add a new file to %PATH by typing the following

 


GRADLE_HOME=~/gradle/gradle.ver

PATH=$PATH:~/gralde/gradle.ver/bin

EXPORT GRADL
<pre>