Skip to content

Commit 97b0a7c

Browse files
authored
Merge pull request #6 from GeoTecINIT/bump-android
Support for Wear OS 4 (Android 13 - API 33)
2 parents dcb6832 + 3219464 commit 97b0a7c

19 files changed

+372
-113
lines changed

README.md

Lines changed: 92 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# WearOS Sensors
22
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.geotecinit/wear-os-sensors/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.geotecinit/wear-os-sensors)
33

4-
The _wear-os-sensors_ library is an Android WearOS library that allows to collect data from the IMU sensors
4+
The _WearOS Sensors_ library is an Android WearOS library that allows to collect data from the IMU sensors
55
(i.e., accelerometer and gyroscope), the magnetometer, the heart rate, and the GPS of an Android WearOS
66
smartwatch (if the corresponding sensor is available in the device).
77

@@ -11,15 +11,16 @@ plugin (for the [NativeScript](https://nativescript.org) framework).
1111
The smartphone application counterpart can request to start/stop the data collection on the smartwatch, and
1212
then receive the collected data from the smartwatch.
1313

14-
> **Warning**: An application built with this library is completely useless if there is not a counterpart
14+
> [!IMPORTANT]
15+
> An application built with this library is completely useless if there is not a counterpart
1516
> application built with the _nativescript-wearos-sensors_ plugin installed in the paired smartphone.
1617
> In other words, the smartwatch can not work by itself alone. It requires for a smartphone to work.
1718
1819
The data collection can be started both from the smartwatch and from the paired smartphone. In addition,
1920
the library offers a way to communicate with the smartphone by sending messages.
2021

21-
The _wear-os-sensors_ library uses and extends the functionality of the Android
22-
[_background-sensors_](https://github.com/GeoTecINIT/BackgroundSensors) library, and therefore, it is safe to carry out
22+
The _WearOS Sensors_ library uses and extends the functionality of the Android
23+
[_Background Sensors_](https://github.com/GeoTecINIT/BackgroundSensors) library, and therefore, it is safe to carry out
2324
the data collection in the background (i.e., when the app is not in the foreground, or the smartwatch is idle).
2425

2526

@@ -28,7 +29,7 @@ To install the library you have to add the dependency in your `build.gradle`:
2829

2930
```groovy
3031
dependencies {
31-
implementation 'io.github.geotecinit:wear-os-sensors:1.1.0'
32+
implementation 'io.github.geotecinit:wear-os-sensors:1.2.0'
3233
}
3334
```
3435

@@ -38,10 +39,23 @@ The library has the following requirements:
3839
- An Android WearOS smartwatch running WearOS 1.0 (API level 23) or higher. In addition, the smartwatch must
3940
be paired with a smartphone with the counterpart application installed.
4041

41-
> **Warning**: Both applications (smartwatch and smartphone apps) must have the same [application id](https://developer.android.com/studio/build/configure-app-module#set-application-id).
42+
> [!IMPORTANT]
43+
> Both applications (smartwatch and smartphone apps) must have the same [application id](https://developer.android.com/studio/build/configure-app-module#set-application-id).
4244
> If that's not the case, the applications will not be able to interact.
4345
44-
> **Note**: Don't forget to check the requirements of [_nativescript-wearos-sensors_](https://github.com/GeoTecINIT/nativescript-wearos-sensors) too.
46+
> [!TIP]
47+
> Don't forget to check the requirements of [_nativescript-wearos-sensors_](https://github.com/GeoTecINIT/nativescript-wearos-sensors) too.
48+
49+
### Tested WearOS versions and devices
50+
The library has been tested in the following WearOS versions:
51+
52+
- **Wear OS 2.33** (Android 9 - API 28):
53+
- TicWatch Pro 3 GPS
54+
- **Wear OS 3.5** (Android 11 - API 30):
55+
- TicWatch Pro 3 GPS
56+
- TicWatch Pro 5
57+
- **WearOS 4** (Android 13 - API 33):
58+
- Samsung Watch4
4559

4660
## Usage
4761
The library offers two main features:
@@ -61,17 +75,34 @@ The library offers two main features:
6175
<details>
6276
<summary>Heart rate</summary>
6377

78+
You **must** add the following permission to the manifest:
79+
6480
```xml
6581
<uses-permission android:name="android.permission.BODY_SENSORS" />
6682
```
83+
84+
In addition, if your app runs on a WearOS 4+ smartwatch (Android 13 - API 33), add the following permission:
85+
86+
```xml
87+
<uses-permission android:name="android.permission.BODY_SENSORS_BACKGROUND" />
88+
```
89+
6790
</details>
6891
<details>
69-
<summary>Location</summary>
92+
<summary>Location</summary>
93+
94+
You **must** add the following permission to the manifest:
7095

7196
```xml
7297
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
7398
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
7499
```
100+
101+
In addition, if your app runs on a WearOS 3+ smartwatch (Android 13 - API 33), add the following permission:
102+
103+
```xml
104+
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
105+
```
75106
</details>
76107

77108
- [Messaging](#messaging): it allows to send and receive simple messages between both devices.
@@ -90,7 +121,13 @@ has to grant some permissions. The library handles this situation (i.e., when pe
90121
launches a notification to warn the user that some permissions need to be granted. However, we **do not**
91122
provide a mechanism to ask the permissions, we delegate that task on the developer. Why? To customise the
92123
way the permissions are required. In other words, the developer has to provide a class reference of an
93-
activity for requesting permissions using the [`PermissionsManager`](#permissionsmanager), and the library
124+
activity for requesting permissions using the [`PermissionsManager.setPermissionsActivity()`](#permissionsmanager). There are two
125+
ways of requesting permissions:
126+
127+
- If the data collection is started using the paired smartphone, the permissions will be automatically requested. You have to do nothing!
128+
- If the data collection is started from the smartphone, you should check that the necessary permissions are granted using the [`PermissionsManager.launchPermissionsRequestIfNeeded()`](#permissionsmanager)
129+
130+
and the library
94131
will start that activity when any permission is required and the user taps into the notification warning.
95132

96133
We have tried to make it easy for you to implement that activity:
@@ -129,13 +166,19 @@ public class YourRequestPermissionsActivity extends FragmentActivity {
129166
}
130167
```
131168

169+
If your app runs in WearOS 4+, the `POST_NOTIFICATIONS` permission will be required. To do so, we also
170+
provide the [`PermissionsManager.launchRequiredPermissionsRequest()`](#permissionsmanager).
171+
132172
Finally, here is a sample on how to setup your activity for requesting permissions:
133173
```java
134174
public class MainActivity extends Activity {
135175
protected void onCreate(Bundle savedInstanceState) {
136176
// ...
137177

138178
PermissionsManager.setPermissionsActivity(this, YourRequestPermissionsActivity.class);
179+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
180+
PermissionsManager.launchRequiredPermissionsRequest(this);
181+
}
139182
}
140183
}
141184
```
@@ -169,6 +212,10 @@ public class MainActivity extends Activity {
169212
}
170213

171214
public void onStartSingleCommandTap(Sensor sensor) {
215+
// If the sensor requires permissions and have not been granted...
216+
boolean requested = PermissionsManager.launchPermissionsRequestIfNeeded(this, sensor.getRequiredPermissions());
217+
if (requested) return;
218+
172219
CollectionConfiguration config = new CollectionConfiguration(
173220
selectedSensor,
174221
android.hardware.SensorManager.SENSOR_DELAY_GAME,
@@ -185,8 +232,9 @@ public class MainActivity extends Activity {
185232
}
186233
```
187234

188-
> **Note**: Here we are using [`Sensor`](#sensor) and [`CollectionConfiguration`](#collectionconfiguration)
189-
> from [_background-sensors_](https://github.com/GeoTecINIT/BackgroundSensors).
235+
> [!TIP]
236+
> Here we are using [`Sensor`](#sensor) and [`CollectionConfiguration`](#collectionconfiguration)
237+
> from [_Background Sensors_](https://github.com/GeoTecINIT/BackgroundSensors).
190238
> Check its documentation for more information.
191239
192240
### Messaging
@@ -228,9 +276,10 @@ public class MainActivity extends Activity {
228276
}
229277
```
230278

231-
> **Note**: you can find a full sample of all these features in the [MainActivity](app/src/main/java/es/uji/geotec/wearossensorsdemo/MainActivity.java)
232-
and [RequestPermissionsActivity](app/src/main/java/es/uji/geotec/wearossensorsdemo/RequestPermissionsActivity.java) activities
233-
of the demo application.
279+
> [!TIP]
280+
> You can find a full sample of all these features in the [MainActivity](app/src/main/java/es/uji/geotec/wearossensorsdemo/MainActivity.java)
281+
> and [RequestPermissionsActivity](app/src/main/java/es/uji/geotec/wearossensorsdemo/RequestPermissionsActivity.java) activities
282+
> of the demo application.
234283
235284
## API
236285

@@ -243,17 +292,23 @@ of the demo application.
243292
| `HEART_RATE` | Represents the heart rate monitor. |
244293
| `LOCATION` | Represents the GPS. |
245294

295+
Each sensor provide the `getRequiredPermissions()` method to obtain the permissions that need to be
296+
requested for the specified sensor. Use it along `PermissionsManager.launchPermissionsRequestIfNeeded()`.
297+
246298
### `SensorManager`
247-
Refer to the [_background-sensors_](https://github.com/GeoTecINIT/BackgroundSensors#sensormanager) documentation.
299+
Refer to the [_Background Sensors_](https://github.com/GeoTecINIT/BackgroundSensors#sensormanager) documentation.
248300

249301
### [`PermissionsManager`](wearossensors/src/main/java/es/uji/geotec/wearossensors/permissions/PermissionsManager.java)
250-
| **Static Method** | **Return type** | **Description** |
251-
|-------------------------------------------------------------------------|---------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
252-
| `permissionsToRequestFromIntent(Intent intent)` | `ArrayList<String>` | Permissions to request in the custom request permissions activity. |
253-
| `permissionsToBeRequested(Context context, ArrayList<String> required)` | `ArrayList<String>` | Returns the permissions that need to be requested (internal use only). |
254-
| `requestPermissions(Activity activity, ArrayList<String> permissions)` | `void` | Request the specified permissions previously obtained from `permissionsToRequestFromIntent`. You should call this method in your custom request permissions activity. |
255-
| `setPermissionsActivity(Context context, Class<?> permissionsActivity)` | `void` | Sets up the class that will be used for requesting permissions. You should call this method in your MainActivity class once your app has started. |
256-
| `getPermissionsActivity(Context context)` | `Class<?>` | Gets the class that will be used for requesting permissions. |
302+
| **Static Method** | **Return type** | **Description** |
303+
|--------------------------------------------------------------------------------------|---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
304+
| `permissionsToRequestFromIntent(Intent intent)` | `ArrayList<String>` | Permissions to request in the custom request permissions activity. |
305+
| `specialPermissionsToRequestFromIntent(Intent intent)` | `ArrayList<String>` | Special permissions to request in the custom request permissions activity. |
306+
| `launchRequiredPermissionsRequest(Activity activity)` | `void` | Launch the permissions activity to request the required permission to post notifications (only WearOS 4+). |
307+
| `launchPermissionsRequestIfNeeded(Activity activity, ArrayList<String> permissions)` | `boolean` | Launch the permissions activity to request the specified permissions. Call this method to request the permissions for a specified sensor. Returns `true` if the permissions activity has been launched. |
308+
| `permissionsToBeRequested(Context context, ArrayList<String> required)` | `ArrayList<String>` | Returns the permissions that need to be requested (internal use only). |
309+
| `requestPermissions(Activity activity, ArrayList<String> permissions)` | `void` | Request the specified permissions previously obtained from `permissionsToRequestFromIntent`. You should call this method in your custom request permissions activity. |
310+
| `setPermissionsActivity(Context context, Class<?> permissionsActivity)` | `void` | Sets up the class that will be used for requesting permissions. You should call this method in your MainActivity class once your app has started. |
311+
| `getPermissionsActivity(Context context)` | `Class<?>` | Gets the class that will be used for requesting permissions. |
257312

258313

259314
### [`PermissionsResultClient`](wearossensors/src/main/java/es/uji/geotec/wearossensors/permissions/PermissionsResultClient.java)
@@ -269,10 +324,10 @@ Refer to the [_background-sensors_](https://github.com/GeoTecINIT/BackgroundSens
269324
| `sendStopCommand(Sensor sensor)` | `void` | Sends a command to the smartphone to stop the collection of the specified sensor in the smartwatch. |
270325

271326
#### CollectionConfiguration
272-
Refer to the [_background-sensors_](https://github.com/GeoTecINIT/BackgroundSensors#collectionconfiguration) documentation.
327+
Refer to the [_Background Sensors_](https://github.com/GeoTecINIT/BackgroundSensors#collectionconfiguration) documentation.
273328

274329
#### Sensor
275-
Refer to the [_background-sensors_](https://github.com/GeoTecINIT/BackgroundSensors) documentation.
330+
Refer to the [_Background Sensors_](https://github.com/GeoTecINIT/BackgroundSensors) documentation.
276331

277332
### [`PlainMessageClient`](wearossensors/src/main/java/es/uji/geotec/wearossensors/plainmessage/PlainMessageClient.java)
278333
| **Method** | **Return type** | **Description** |
@@ -294,3 +349,16 @@ Apache License 2.0
294349
See [LICENSE](LICENSE).
295350

296351

352+
## Author
353+
354+
<a href="https://github.com/matey97" title="Miguel Matey Sanz">
355+
<img src="https://avatars3.githubusercontent.com/u/25453537?s=120" alt="Miguel Matey Sanz" width="120"/>
356+
</a>
357+
358+
359+
## Acknowledgements
360+
361+
The development of this library has been possible thanks to the Spanish Ministry of Universities (grant FPU19/05352).
362+
363+
364+

app/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ android {
1212
}
1313
}
1414

15-
compileSdk 31
15+
compileSdk 33
1616

1717
defaultConfig {
1818
applicationId "es.uji.geotec.wearossensorsdemo"
1919
minSdk 23
20-
targetSdk 31
20+
targetSdk 33
2121
versionCode 1
2222
versionName "1.0"
2323

@@ -30,6 +30,7 @@ android {
3030
signingConfig signingConfigs.release
3131
}
3232
}
33+
namespace 'es.uji.geotec.wearossensorsdemo'
3334
}
3435

3536
dependencies {

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="es.uji.geotec.wearossensorsdemo">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
43

54
<uses-permission android:name="android.permission.BODY_SENSORS" />
5+
<uses-permission android:name="android.permission.BODY_SENSORS_BACKGROUND" />
66
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
77
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
8-
8+
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
99

1010
<uses-feature android:name="android.hardware.type.watch" />
1111

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.content.res.Resources;
5+
import android.os.Build;
56
import android.os.Bundle;
67
import android.util.Log;
78
import android.view.View;
@@ -50,11 +51,17 @@ protected void onCreate(Bundle savedInstanceState) {
5051
});
5152

5253
PermissionsManager.setPermissionsActivity(this, RequestPermissionsActivity.class);
54+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
55+
PermissionsManager.launchRequiredPermissionsRequest(this);
56+
}
5357
}
5458

5559
public void onStartSingleCommandTap(View view) {
60+
WearSensor selectedSensor = (WearSensor) sensorSpinner.getSelectedItem();
61+
boolean requested = PermissionsManager.launchPermissionsRequestIfNeeded(this, selectedSensor.getRequiredPermissions());
62+
if (requested) return;
63+
5664
toggleVisibility(stopSingle, startSingle);
57-
Sensor selectedSensor = (Sensor) sensorSpinner.getSelectedItem();
5865
sensorSpinner.setEnabled(false);
5966

6067
CollectionConfiguration config = new CollectionConfiguration(

0 commit comments

Comments
 (0)