Data collection libraries
The data collection from IMU sensors built-in Android smartphones or Wear OS smartwatches for HAR (and other applications) should be reliable, i.e., reduce the presence of missing data.
However, as indicated previously, it has been documented that a broad amount of passive sensing applications used in published research suffer up to 50% of missing data (Boonstra et al. 2018). As pointed out by González-Pérez et al. (2022), the cause of this missingness comes from the energy consumption restrictions imposed by the OS, which tends to reduce resources and even kill long-running processes (e.g., data collection).
To mitigate these energy restrictions, González-Pérez et al. (2022) propose a set of development guidelines that must be applied to reduce the amount of missing data, showing only a 1% of data missingness in long-running executions.
This section describes the developed libraries implementing these guidelines for data collection in Android smartphones and WearOS smartwatches, addressing research objective RO1.
Background Sensors
The Background Sensors library is an Android library developed in Java that implements a reliable passive collection of IMU sensors (i.e., accelerometer, gyroscope and magnetometer), and, therefore, it can be used to develop native Android applications that require to collect samples from such sensors. The library has been developed to support devices running from Andriod 5.0 (API level 21) to Android 13 (API level 33).
The version of the library at the moment of writing this thesis is v1.3.0. The library is available in:
Figure 3.1 includes the simplified architecture of the Background Sensors library.
The full documentation of the library and its components can be found in GitHub.
Sample usage
Tap on the numbers at the end of the lines to obtain insights about the code.
public class ExampleActivity extends Activity {
private SensorManager sensorManager;
private ServiceManager serviceManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
= new SensorManager(this);
sensorManager List<Sensor> sensors = sensorManager.availableSensors(BaseSensor.values());
= new ServiceManager(this, BaseSensorRecordingService.class);
serviceManager }
public void start() {
= BaseSensor.ACCELEROMETER;
Sensor sensor
= new CollectionConfiguration(
CollectionConfiguration config ,
sensor10000, // sensorDelay (microseconds)
100 // batchSize
);
= new RecordCallback {
RecordCallback callback public void onRecordsCollected(List<Record> records) {
// Accelerometer samples received here
}
};
.startCollection(config, callback);
serviceManager}
public void stop() {
.stopCollection(BaseSensor.ACCELEROMETER);
serviceManager}
}
- 1
-
Create an instance of
SensorManager
and callavailableSensors(...)
to determine which sensors are available in the device. - 2
-
Create a
ServiceManager
instance with aSensorRecordingService
. - 3
-
Create a
CollectionConfiguration
indicating thesensor
, thesensorDelay
(microseconds) and thebatchSize
. - 4
-
Implement the
RecordCallback
. The samples will be received here. - 5
-
Start the data collection calling
ServiceManager#startCollection(...)
. - 6
-
Stop the data collection calling
ServiceManager#stopCollection(...)
.
WearOS Sensors & NativeScript WearOS Sensors
The WearOS Sensors library is a Wear OS library written in Java that can be used to develop native Wear OS applications and, like Background Sensors, implements a reliable passive collection of IMU sensors (i.e., accelerometer, gyroscope and magnetometer). In addition, it supports collecting data from the heart rate sensor and the location services (i.e., GNSS).
On the other hand, NativeScript WearOS Sensors is a library written in TypeScript that can be used to build smartphone applications complementary to WearOS Sensors developed applications. Unlike the other libraries, this one cannot be used to develop native applications but applications built with the NativeScript development framework, which uses web technologies (e.g., JavaScript, TypeScript) to build Android and iOS applications.
The following features are available when using applications developed with these libraries:
- WearOS Sensors:
- Start and stop from the smartwatch the data collection of the available sensors in the device.
- Store collected data in the smartwatch.
- WearOS Sensors + Nativescript WearOS Sensors:
- Start and stop from the smartwatch the data collection of the available sensors in the device.
- Start and stop from the paired smartphone the data collection of the available sensors in the smartwatch.
- Store the collected data in the smartwatch or in the smartphone.
The WearOS Sensors and NativeScript WearOS Senosrs libraries have been developed to support devices running from Wear OS 1.0 ( level 23) to Wear OS 4 ( level 33) and from Android 6 ( level 23) to Android 13 ( level 33), respectively.
The version of WearOS Sensors and NativeScript WearOS Sensors at the moment of writing this thesis are v1.2.1 and v1.3.0. The libraries are available in:
- WearOS Sensors:
- NativeScript WearOS Sensors:
Figure 3.2 includes the simplified architecture of the WearOS Sensors and NativeScript WearOS Sensors libraries.
The full documentation of the libraries and their components can be found in their respective repositories: WearOS Sensors and NativeScript WearOS Sensors.
Sample usage
Managing collection process and storing data in smartwatch.
Since WearOS Sensors is built on top of Background Sensors the same code as the one presented previously can be employed for this purpose.
Managing collection process in smartwatch, storing data in smartphone.
public class ExampleActivity extends Activity {
private CommandClient commandClient;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
= new CommandClient(this);
commandClient }
public void start() {
= WearSensor.ACCELEROMETER;
Sensor sensor
= new CollectionConfiguration(
CollectionConfiguration config ,
sensor10000, // sensorDelay (microseconds)
100 // batchSize
);
.sendStartCommand(config);
commandClient}
public void stop() {
.sendStopCommand(WearSensor.ACCELEROMETER);
commandClient}
}
- 1
-
Create a
CommandClient
instance. - 2
-
Create a
CollectionConfiguration
including thesensor
,sensorDelay
(microseconds) andbatchSize
. - 3
-
Start the data collection using the
CommandClient#sendStartCommand(...)
. - 4
-
Stop the data collection using the
CommandClient#sendStopCommand(...)
.
function registerListener() {
const collectorManager = getCollectorManager();
.addSensorListener((sensorRecord: SensorRecord<any>) => {
collectorManager// Accelerometer samples received here.
;
}) }
- 5
-
Obtain a
CollectorManager
instance using thegetCollectorManager()
function. - 6
-
Register a listener with the
CollectorManager#addSensorListener(...)
.
Managing collection process and storing data in smartphone.
let collectorManager;
function registerListener() {
const collectorManager = getCollectorManager();
.addSensorListener((sensorRecord: SensorRecord<any>) => {
collectorManager// Accelerometer samples received here.
;
})
}
let smartwatch;
async function getConnectedSmartwatch() {
const nodesDiscovered = await getNodeDiscoverer().getConnectedNodes();
= nodesDiscovered[0];
smartwatch
}
function start() {
const config: CollectionConfiguration = {sensorInterval: 10000, batchSize: 100};
.startCollecting(smartwatch, SensorType.ACCELEROMETER, config);
collectorManager
}
function stop() {
.stopCollecting(smartwatch, SensorType.ACCELEROMETER);
collectorManager }
- 1
-
Obtain a
CollectorManager
instance using thegetCollectorManager()
function. - 2
-
Register a listener with the
CollectorManager#addSensorListener(...)
. - 3
-
Use
NodeDiscoverer#getConnectedNodes()
to get a reference to the connected smartwatches. - 4
-
Create a
CollectionConfiguration
including thesensorInterval
(microseconds) andbatchSize
. - 5
-
Start the data collection on the selected smartwatch calling
CollectorManager#startCollecting(...)
. - 6
-
Stop the data collection on the selected smartwatch calling
CollectorManager#stopCollecting(...)
.
Integration in the AwarNS Framework
The AwarNS Framework, implemented by a colleague researcher, is an Android-based development framework aimed to facilitate the development of context-aware smartphone applications which require systematic data acquisition, on-device analyses and perform actions based on these analyses (González-Pérez et al. 2023). The framework is built on top of the NativeScript Task Dispatcher, which allows the definition and execution of reactive workflows using its building blocks: Tasks
(code units), Events
(execution drivers) and TaskGraphs
(allow the definition of reactive workflows, i.e., Tasks
triggered by Events
) (González-Pérez et al. 2022).
This dissertation contributes to the AwarNS Framework by wrapping the previously described data collection tools in it. This integration would allow the combination of already existing features in the framework (e.g., geolocation) with the features of the developed libraries (e.g., IMU data for HAR) to implement context-aware applications (e.g., recognition of activities in specific contexts).
In particular, two new packages are developed and added into the framework: the Phone Sensors and the Wear OS packages. These packages wrap up the Background Sensors and NativeScript WearOS Sensors libraries to integrate their functionality into the AwarNS Framework.
For a more detailed description of the AwarNS Framework, check its GitHub repository and the research paper (González-Pérez et al. 2023)
The full documentation of the libraries and their components can be found in the AwarNS Framework repository: Phone Sensors and WearOS Sensors.