Skip to content

Commit 6ae7820

Browse files
authored
Merge pull request #8 from GeoTecINIT/local-collection
Local collection example
2 parents 2121083 + 66c20a8 commit 6ae7820

File tree

8 files changed

+246
-77
lines changed

8 files changed

+246
-77
lines changed

README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ two main steps in the process:
123123

124124
1. Check if permissions are required:
125125
- If the data collection is started using the paired smartphone, the check is automatic. You have to do nothing!
126-
- If the data collection is started using the smartphone, you need to do the check using the [`PermissionsManager.launchPermissionsRequestIfNeeded()`](#permissionsmanager) method.
126+
- If the data collection is started using the smartwatch, you need to do the check using the [`PermissionsManager.launchPermissionsRequestIfNeeded()`](#permissionsmanager) method.
127127
2. Request the required permissions: the library handles mostly of the process by you, but you still have to do execute some steps:
128128
- Create an Activity: it will be used by the library to request the permissions. This will allow you to define a UI where some information can be given prior to the permissions request.
129129
- In your activity:
@@ -182,9 +182,58 @@ public class MainActivity extends Activity {
182182
```
183183

184184
#### Start/stop data collection from smartphone
185-
The library fully handles this for you. You have to do nothing!
185+
The library fully handles this for you. You can start and stop the data collection from the smartphone
186+
and receive the collected data. You have to do nothing!
186187

187188
#### Start/stop data collection from smartwatch
189+
When starting the data collection from the smartwatch, you can chose where the collected data will be
190+
delivered: to the smartwatch itself or to the paired smartphone.
191+
192+
##### Obtain the data in the smartwatch
193+
You can use the [`ServiceManager`](#servicemanager) provided by _Background Sensors_ to start and stop
194+
the data collection and receive the data in the device:
195+
196+
```java
197+
public class MainActivity extends Activity {
198+
// ...
199+
private CommandClient commandClient;
200+
201+
@Override
202+
protected void onCreate(Bundle savedInstanceState) {
203+
// ...
204+
sensorManager = new SensorManager(context);
205+
serviceManager = new ServiceManager(this, WearSensorRecordingService.class);
206+
}
207+
208+
public void setupUI() {
209+
List<Sensor> availableSensors = sensorManager.availableSensors(WearSensor.values());
210+
}
211+
212+
public void onStartSingleCommandTap(WearSensor sensor) {
213+
CollectionConfiguration config = new CollectionConfiguration(
214+
selectedSensor,
215+
android.hardware.SensorManager.SENSOR_DELAY_GAME,
216+
selectedSensor == WearSensor.HEART_RATE || selectedSensor == WearSensor.LOCATION ? 1 : 50
217+
);
218+
serviceManager.startCollection(config, records -> {
219+
// ...
220+
});
221+
}
222+
223+
public void onStopSingleCommandTap(Sensor sensor) {
224+
serviceManager.stopCollection(sensor, records -> {
225+
// ...
226+
});
227+
}
228+
229+
// ...
230+
}
231+
```
232+
233+
> [!TIP]
234+
> Please, refer to the [_Background Sensors_](https://github.com/GeoTecINIT/BackgroundSensors#servicemanager) documentation.
235+
236+
##### Obtain the data in the smartphone
188237
To start or stop the data collection, the smartphone needs to be updated regarding the change in the data
189238
collection status. So, if we want to start/stop the data collection from the smartwatch, we have to notify that intention to the smartphone.
190239
Then, the smartphone will update its internal status and once everything is set up, it will confirm the smartwatch
@@ -296,6 +345,9 @@ requested for the specified sensor. Use it along `PermissionsManager.launchPermi
296345
### `SensorManager`
297346
Refer to the [_Background Sensors_](https://github.com/GeoTecINIT/BackgroundSensors#sensormanager) documentation.
298347

348+
### `ServiceManager`
349+
Refer to the [_Background Sensors_](https://github.com/GeoTecINIT/BackgroundSensors#servicemanager) documentation.
350+
299351
### [`PermissionsManager`](wearossensors/src/main/java/es/uji/geotec/wearossensors/permissions/PermissionsManager.java)
300352
| **Static Method** | **Return type** | **Description** |
301353
|--------------------------------------------------------------------------------------|---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

app/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ dependencies {
3737
implementation 'com.google.android.support:wearable:2.8.1'
3838
implementation 'androidx.percentlayout:percentlayout:1.0.0'
3939
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
40-
implementation 'androidx.recyclerview:recyclerview:1.2.1'
41-
implementation 'androidx.wear:wear:1.0.0'
40+
implementation 'androidx.wear:wear:1.3.0'
4241
implementation project(path: ':wearossensors')
4342
compileOnly 'com.google.android.wearable:wearable:2.8.1'
4443
}

app/src/main/java/es/uji/geotec/wearossensorsdemo/MainActivity.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@
99
import android.widget.ArrayAdapter;
1010
import android.widget.Button;
1111
import android.widget.LinearLayout;
12+
import android.widget.RadioButton;
13+
import android.widget.RadioGroup;
1214
import android.widget.Spinner;
1315

14-
import es.uji.geotec.backgroundsensors.collection.CollectionConfiguration;
1516
import es.uji.geotec.backgroundsensors.sensor.Sensor;
1617
import es.uji.geotec.backgroundsensors.sensor.SensorManager;
17-
import es.uji.geotec.wearossensors.command.CommandClient;
1818
import es.uji.geotec.wearossensors.plainmessage.PlainMessage;
1919
import es.uji.geotec.wearossensors.plainmessage.PlainMessageClient;
2020
import es.uji.geotec.wearossensors.permissions.PermissionsManager;
2121
import es.uji.geotec.wearossensors.sensor.WearSensor;
22+
import es.uji.geotec.wearossensorsdemo.command.CollectionCommand;
23+
import es.uji.geotec.wearossensorsdemo.command.LocalCollectionCommand;
24+
import es.uji.geotec.wearossensorsdemo.command.RemoteCollectionCommand;
2225

2326
public class MainActivity extends Activity {
2427

2528
private LinearLayout linearLayout;
29+
private RadioGroup destinationRadio;
2630
private Button startSingle, stopSingle;
2731
private Spinner sensorSpinner;
28-
29-
private CommandClient commandClient;
32+
private CollectionCommand command;
3033
private PlainMessageClient messageClient;
3134

3235
@Override
@@ -38,7 +41,7 @@ protected void onCreate(Bundle savedInstanceState) {
3841
setupButtons();
3942
setupSpinner();
4043

41-
commandClient = new CommandClient(this);
44+
command = new LocalCollectionCommand(this);
4245
messageClient = new PlainMessageClient(this);
4346
messageClient.registerListener(message -> {
4447
Log.d("MainActivity", "received " + message);
@@ -56,28 +59,39 @@ protected void onCreate(Bundle savedInstanceState) {
5659
}
5760
}
5861

62+
public void onDestinationButtonClicked(View view) {
63+
boolean checked = ((RadioButton) view).isChecked();
64+
if (!checked) return;
65+
66+
switch(view.getId()) {
67+
case R.id.local_collection:
68+
this.command = new LocalCollectionCommand(this);
69+
break;
70+
case R.id.remote_collection:
71+
this.command = new RemoteCollectionCommand(this);
72+
break;
73+
}
74+
}
75+
5976
public void onStartSingleCommandTap(View view) {
6077
WearSensor selectedSensor = (WearSensor) sensorSpinner.getSelectedItem();
6178
boolean requested = PermissionsManager.launchPermissionsRequestIfNeeded(this, selectedSensor.getRequiredPermissions());
6279
if (requested) return;
6380

6481
toggleVisibility(stopSingle, startSingle);
6582
sensorSpinner.setEnabled(false);
83+
destinationRadio.setEnabled(false);
6684

67-
CollectionConfiguration config = new CollectionConfiguration(
68-
selectedSensor,
69-
android.hardware.SensorManager.SENSOR_DELAY_GAME,
70-
selectedSensor == WearSensor.HEART_RATE || selectedSensor == WearSensor.LOCATION ? 1 : 50
71-
);
72-
commandClient.sendStartCommand(config);
85+
command.executeStart(selectedSensor);
7386
}
7487

7588
public void onStopSingleCommandTap(View view) {
7689
toggleVisibility(startSingle, stopSingle);
77-
Sensor selectedSensor = (Sensor) sensorSpinner.getSelectedItem();
7890
sensorSpinner.setEnabled(true);
91+
destinationRadio.setEnabled(true);
7992

80-
commandClient.sendStopCommand(selectedSensor);
93+
WearSensor selectedSensor = (WearSensor) sensorSpinner.getSelectedItem();
94+
command.executeStop(selectedSensor);
8195
}
8296

8397
public void onSendFreeMessageTap(View view) {
@@ -96,6 +110,7 @@ private void setupLayout() {
96110
private void setupButtons() {
97111
startSingle = findViewById(R.id.start_single_command);
98112
stopSingle = findViewById(R.id.stop_single_command);
113+
destinationRadio = findViewById(R.id.destination_collection);
99114
}
100115

101116
private void setupSpinner() {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package es.uji.geotec.wearossensorsdemo.command;
2+
3+
import es.uji.geotec.wearossensors.sensor.WearSensor;
4+
5+
public interface CollectionCommand {
6+
void executeStart(WearSensor sensor);
7+
void executeStop(WearSensor sensor);
8+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package es.uji.geotec.wearossensorsdemo.command;
2+
3+
import android.content.Context;
4+
import android.util.Log;
5+
6+
import es.uji.geotec.backgroundsensors.collection.CollectionConfiguration;
7+
import es.uji.geotec.backgroundsensors.service.manager.ServiceManager;
8+
import es.uji.geotec.wearossensors.sensor.WearSensor;
9+
import es.uji.geotec.wearossensors.services.WearSensorRecordingService;
10+
11+
public class LocalCollectionCommand implements CollectionCommand {
12+
13+
private ServiceManager serviceManager;
14+
15+
public LocalCollectionCommand(Context context) {
16+
this.serviceManager = new ServiceManager(context, WearSensorRecordingService.class);
17+
}
18+
@Override
19+
public void executeStart(WearSensor sensor) {
20+
CollectionConfiguration config = new CollectionConfiguration(
21+
sensor,
22+
android.hardware.SensorManager.SENSOR_DELAY_GAME,
23+
sensor == WearSensor.HEART_RATE || sensor == WearSensor.LOCATION ? 1 : 50
24+
);
25+
this.serviceManager.startCollection(config, records -> {
26+
Log.d("LOCAL COLLECTION", sensor.toString()+ " records: " + records.toString());
27+
});
28+
}
29+
30+
@Override
31+
public void executeStop(WearSensor sensor) {
32+
this.serviceManager.stopCollection(sensor);
33+
}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package es.uji.geotec.wearossensorsdemo.command;
2+
3+
import android.content.Context;
4+
5+
import es.uji.geotec.backgroundsensors.collection.CollectionConfiguration;
6+
import es.uji.geotec.wearossensors.command.CommandClient;
7+
import es.uji.geotec.wearossensors.sensor.WearSensor;
8+
9+
public class RemoteCollectionCommand implements CollectionCommand {
10+
11+
private CommandClient commandClient;
12+
13+
public RemoteCollectionCommand(Context context) {
14+
this.commandClient = new CommandClient(context);
15+
}
16+
@Override
17+
public void executeStart(WearSensor sensor) {
18+
CollectionConfiguration config = new CollectionConfiguration(
19+
sensor,
20+
android.hardware.SensorManager.SENSOR_DELAY_GAME,
21+
sensor == WearSensor.HEART_RATE || sensor == WearSensor.LOCATION ? 1 : 50
22+
);
23+
this.commandClient.sendStartCommand(config);
24+
}
25+
26+
@Override
27+
public void executeStop(WearSensor sensor) {
28+
this.commandClient.sendStopCommand(sensor);
29+
}
30+
}

0 commit comments

Comments
 (0)