How to close the sensor connection
When the Android Things app is destroyed, it is important to release all the connections we have used to exchange data with the sensor and remove all listeners. In this way, we free all the resources used by the app. In more details, we have to release the SDA pin used to communicate to the sensor so that other apps can reuse it. In order to destroy the app gracefully, we have to execute the following steps:
- Unregister the sensor listener we have used to listen to value changes.
- Unregister the sensor listener we have used to know when the sensor is connected to the Android Things board.
- Close the sensor connection.
The best approach is executing the previous steps in the onDestroy
method of our monitoring app. The following code describes how to implement these steps:
@Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); sensorManager.unregisterListener(tempCallback); sensorManager.unregisterListener(pressCallback); mySensorDriver...