O'Reilly logo

IoT Projects with Bluetooth Low Energy by Madhur Bhargava

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Application Code

We have already created some UI and provided necessary permissions to our app, now we will add the necessary code to discover BLE services on any device. As the first step in this endeavour, it is important to discover the devices and then go over to the next step, which would be to discover the service on the device we are interested in.

Firstly, to control our UI elements (two Button and a ListView), we will need to access them in code:

  1. Declare the following variables in the MainActivity class at the top in the MainActivity.java file:
        Button startScanningButton;         Button stopScanningButton;         ListView deviceListView; 

The ListViews in Android are backed by adapters, which hold the data being displayed in a ListView.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required