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:
- 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.