# Open-Source MQTT-Based End-to-End IoT System for Smart City Scenarios
## 4 Proof-of-Concept
### Gateway Interface to the Broker: The Android Platform
#### 4.2.1 BLE Management  
The first action to be performed in order to establish a Bluetooth connection between the two devices is a scan of the nearby BLE devices with the ATT role of peripherals. Figure 3: The Android App main Activity. This is achieved by interacting with the Android app toolbar, whose role is to display the list of the nearby BLE devices so that the user is able to pick the device they wish to connect to from the list. In order to initialize the scan, it is mandatory to declare in the AndroidManifest.xml file the correct permissions the Android needs in order to use the Bluetooth API. Moreover, since the API 23 Android requires the user to accept at run-time those permissions that are labeled as dangerous, the user has to allow the app to acquire the permissions regarding the location of the user since they are a mandatory requirement for the BLE API to work. Once the permissions have been granted, an object of the class BluetoothAdapter() is instantiated, which abstracts the Bluetooth Radio, integrated in the smartphone, and allows interacting with it through the software code. The first operation allows checking if the device running the app supports BLE; in case it does not, the app will not allow the user to proceed further. This instance is used later to instantiate an object of the class BluetoothLeScanner(), which is used for all those operations regarding the scanning of BLE devices. Moreover, the Android allows implementing a white list of the scanned devices we might be interested in by discarding the others. Since we aim to connect to a small pool of devices the IoT wearables implementing the SoC programmed previously, we believe implementing a white list will allow the Android app to connect to only to the ESP32 SoCs on the wearable devices to prevent the Android app from connecting to other devices. Once the scan has been initialized, the results will be available in a callback function and the graphic interface is updated at run-time with all the nearby devices scanned previously being filtered. In order to implement the Bluetooth communication, it is necessary to use multithreading techniques due to the asynchronous behavior of the communication; hence, we have a message queue in the Android with all the threads to be run. Once the user has chosen the correct wearable device to connect to, they are redirected back to the main activity where they will initialize the connection to the GATT server. The connection to the GATT server starts by instantiating an object of the class BluetoothDevice(), which is used to invoke the method connectGatt(Contextcontext,booleanautoConnect, BluetoothGattCallBack bluetoothGattCallBack). This allows the smartphone to automatically connect to the wearable devices, as long as they are bounded. In order to acquire the output of this method call, we have defined a callback function through the use of the Java anonymous inner class construct, which creates a class that implements a Java interface; hence, certain Abstract methods must be overridden as in Listing 1. ```
bluetoothGattCallbackgattCallBack-newBluetoothGattCallback(){ @Override publicvoidonConnectionStateChange(BluetoothGattgatt,intstatus,intnewState){  super.onConnectionStateChange(gatt,status,newState);... } } @Override publicvoidonServicesDiscovered(BluetoothGattgatt,intstatus){  super.onServicesDiscovered(gatt,status);... } } @Override publicvoidonCharacteristicChanged(BluetoothGattgatt,BluetoothGattCharacteristiccharacteristic){ super.onCharacteristicChanged(gatt,characteristic);... } } @Override publicvoidonDescriptorWrite(BluetoothGattgatt,BluetoothGattDescriptor,intstatus){
```super.onDescriptorWrite(gatt,descriptor,status); ...  } } ```  
The method **public void** onConnectionStateChange (BluetoothGatt gatt, **int** status, **int** newState) is used to check on the status of the Bluetooth connection between the two devices involved in the data exchange, indicating when a GATT client has connected/disconnected to/from a remote GATT server.