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 file:
        Button startScanningButton;         Button stopScanningButton;         ListView deviceListView; 

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

Get IoT Projects with Bluetooth Low Energy now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.