1.3.0 • Published 3 months ago

nativescript-wearos-sensors v1.3.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
3 months ago

nativescript-wearos-sensors

npm npm

The nativescript-wearos-sensors is a plugin developed with the NativeScript framework. It allows to develop smartphone applications for collecting data from the IMU sensors (i.e., accelerometer and gyroscope), the magnetometer, the heart rate and the GPS of a paired Android WearOS smartwatch (if the corresponding sensor is available in the device).

The Android WearOS smartwatch must run a counterpart application built using the native WearOS Sensors library. Then, the smartphone application can request the smartwatch to start/stop the collection of the specified sensor, and the smartwatch will send the collected samples to the smartphone application.

!WARNING An application using this plugin is completely useless if there is not a counterpart application build with the WearOS Sensors library installed in the paired smartwatch. In other words, the smartphone can not work by itself alone. It requires a smartwatch to work.

The data collection can be started both from the smartphone and from the paired smartwatch. In addition. the plugin offers a way to communicate with the smartwatch by sending messages.

Installation

Run the following command in your project's root folder.

ns plugin add nativescript-wearos-sensors

Requirements

This plugin is only supported by Android smartphones. To use it to build an application, the following requirements apply:

  • An Android smartphone running Android 6 (API level 23) or higher.

!CAUTION The targetSdkVersion of the built application must be lower or equal than 31 (Android 12). Apps built targeting >=31 will not work due to an issue with Dynamic File Loading.

  • In addition, the smartphone must be paired with a smartwatch with the counterpart application installed. To link a smartwatch to the smartphone, you must also install the Smartwatch WearOS by Google or the specific application provided by the smartwatch manufacturer (e.g., Mobvoi Health, Samsung Wearable, etc.) and follow the procedure to link both devices.

!IMPORTANT Both applications (smartphone and smartwatch apps) must have the same application id. If that's not the case, the applications will not be able to interact. You can change the application id of a NativeScript application in the nativescript.config.ts.

Usage

The plugin offers two features:

  • Sensor data collection from the paired smartwatch: it can be started/stopped from the smartphone and from the smartwatch. The smartwatch is able to start and stop the data collection thanks to the WearCommands feature.
  • Plain Messaging: it allows to send and receive simple messages between both devices.

In first place, you need to initialize the plugin with a WearosSensorsConfig in your app.ts (TypeScript app) or main.ts (Angular app) file:

// TypeScript App:
import { Application } from "@nativescript/core";
// or Angular App:
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app/app.module";

// WearOSSensors import
import { wearosSensors, allSensors } from "nativescript-wearos-sensors";

wearosSensors.init({
    sensors: allSensors,
    disablePlainMessaging: false,
    disableWearCommands: false
});

// TypeScript App:
Application.run({ moduleName: "app-root" });
// Angular App:
platformNativeScriptDynamic().bootstrapModule(AppModule);

The initialization parameter is optional, and it allows specifying which sensors are enabled (sensors), and if PlainMessaging (disablePlainMessaging) and WearCommands (disableWearCommands) features are enabled. The default configuration is the one shown in the example above: all sensors and features enabled.

!NOTE The configuration allows to conditionally wire up native components with the core of the plugin. This allows to reduce the memory used by the application when some features are not going to be used.

Sensor data collection

As stated above, the data collection can be started/stopped by both devices, but only the smartphone has access to the collected data.

To receive the collected data, the smartphone has to register a listener (at least one) via the CollectorManager. A listener can be set up for a specific node/s (i.e., smartwatch) and a specific sensor/s. This means that there can be several listeners registered, all of them listening for different nodes or sensors. This behaviour can be achieved using ListenerFilters. Here's an example of registering different listeners:

import { getCollectorManager } from "nativescript-wearos-sensors/collection";
import { SensorRecord } from "nativescript-wearos-sensors/sensors/records";
import { Node } from "nativescript-wearos-sensors/node";
import { ListenerFilter } from "nativescript-wearos-sensors/listeners";
import { SensorType } from "nativescript-wearos-sensors/sensors";


function registerGlobalListener() {
    // Register a listener witout filters --> receives records from all sources
    getCollectorManager().addSensorListener((sensorRecord: SensorRecord<any>) => {
        console.log(deviceId, JSON.stringify(sensorRecord));
    });
}

function registerListenerForNode(node: Node) {
    // Register a listener filtering per node --> receives all kind of records from that node
    const filter: ListenerFilter = {
        nodes: [node]
    }
    getCollectorManager().addSensorListener((sensorRecord: SensorRecord<any>) => {
        console.log(deviceId, JSON.stringify(sensorRecord));
    }, filter);
}

function registerListenerForSensor(sensor: SensorType) {
    // Register a listener filtering per sensor --> receives records of that sensor from any node
    const filter: ListenerFilter = {
        sensors: [sensor]
    }
    getCollectorManager().addSensorListener((sensorRecord: SensorRecord<any>) => {
        console.log(deviceId, JSON.stringify(sensorRecord));
    }, filter);
}

function registerListenerForNodeAndSensors(node: Node, sensors: SensorType[]) {
    // Register a listener filtering per node and sensor --> receives records of that sensors from that node
    const filter: ListenerFilter = {
        nodes: [node],
        sensors: sensors
    }
    getCollectorManager().addSensorListener((sensorRecord: SensorRecord<any>) => {
        console.log(deviceId, JSON.stringify(sensorRecord));
    }, filter);
}

Start/stop data collection from smartphone

In order to start the data collection for node, first you have to get the connected nodes using the NodeDiscoverer. Then, once you have the connected node, you have to follow some steps to start the data collection:

  1. Check if a specific sensor on the node is ready to be collected from.
  2. If it is not ready:
    1. It is because the sensor is not present in the device.
    2. The sensor is in the device, but the smartwatch app has no permissions to collect from that sensor.
  3. If we lack of permissions, we can just ask the user to grant them.
    1. If permissions are rejected, it's the end of the way...
    2. If permissions are granted, we can start the collection!!

To start the data collection, we also should specify a CollectionConfiguration, where we can indicate the desired time between consecutive samples (sensorDelay), and the amount of samples to deliver each time (batchSize). The configuration is optional, if no configuration is provided then default values apply.

!NOTE Due to the smartwatch has to send the collected data via Bluetooth, we can't send individual samples when working with a high sampling rate. That would saturate the connection. To solve this problem, we send the samples in batches.

Here is an example of this collection procedure:

import { getNodeDiscoverer } from "nativescript-wearos-sensors/node";
import { getCollectorManager, PrepareError, CollectionConfiguration } from "nativescript-wearos-sensors/collection";
import { Node } from "nativescript-wearos-sensors/node";
import { SensorType } from "nativescript-wearos-sensors/sensors";

async function getNodes(): Promise<Node[]> {
    await nodesDiscovered = nodeDiscoverer.getConnectedNodes();
    const nodes = []
    nodesDiscovered.forEach((nodeDiscovered) => {
        if (nodeDiscovered.error) {
            this.logger.logResult(nodeDiscovered.error);
            return;
        }
        nodes.push(nodeDiscovered.node);
    });
    return nodes;
}

async function collectFrom(node: Node, sensor: SensorType, config: CollectionConfiguration) {
    const collectorManager = getCollectorManager();
    
    const isReady = await collectorManager.isReady(node, sensor);
    if (!isReady) {
        const prepareError: PrepareError = await collectorManager.prepare(node, sensor);
        if (prepareError) {
            console.log(prepareError.message);
            return;
        }
    }
    
    await collectorManager.startCollecting(node, sensor, config);
}

async function stopCollecingFrom(node: Node, sensor: SensorType) {
    await collectorManager.stopCollecting(node, sensor);
}

Start/stop data collection from smartwatch

The plugin fully handles this for you. You only have to make sure to register at least a listener to receive the collected data.

!IMPORTANT The WearCommands feature must be enabled at plugin initialization.

PlainMessaging

With a system composed by several devices, it is important to have a way to communicate. We provide the PlainMessageClient, which allows to send and receive string based messages. There are two types of received messages: the ones which require a response and the ones which don't. Here's an example on how to use the messaging feature:

import { getPlainMessageClient } from "src/internal/communication/plain-message";

function registerListener(): void {
    // Register a listener to receive messages from the smartwatch
    getPlainMessageClient().registerListener((receivedMessage) => {
        console.log(`received single message ${JSON.stringify(receivedMessage)}`);
    });
}

async function sendMessage(node: Node, message: string): void {
    // Send a message to the smartwatch
    const plainMessage = {message: "You don't have to reply :)"};
    await getPlainMessageClient().send(node, plainMessage);
}

async function sendMessageAndWaitResponse(node: Node, message: string): void {
    // Send a message to the smartwatch and wait for a response
    const plainMessage = {message: "PING!"};
    const receivedMessage = await getPlainMessageClient().sendExpectingResponse(node, plainMessage);
    console.log(`response received: ${JSON.stringify(receivedMessage)}`);
}

!IMPORTANT The PlainMessaging feature must be enabled at plugin initialization.

API

wearosSensors - Methods

NameReturn typeDescription
init(config?: WearosSensorsConfig)Promise<void>Initializes the native components depending on the provided configuration. If no configuration provided, defaults to defaultConfig.

WearosSensorsConfig

PropertyTypeDescription
sensors?SensorType[]Sensors that are going to be used. Default: all sensors.
disablePlainMessaging?booleanDisable plain messaging feature. Default: false.
disableWearCommands?booleanDisable wear commands feature. Default: false.
defaultConfig
export const defaultConfig = {
    sensors: allSensors, // Constant containing all the sensors
    disablePlainMessaging: false,
    disableWearCommands: false
};

NodeDiscoverer

FunctionReturn typeDescription
getLocalNode()Promise<Node>Get a reference to the local node (smartphone).
areConnectedNodes()Promise<boolean>Returns true if there are connected nodes.
getConnectedNodes(timeout: number = 5000)Promise<NodeDiscovered[]Get the currently connected nodes and their available sensors. Timeout indicates the maximum wait time for the connected nodes to communicate with the smartphone.

Node

FieldTypeDescription
namestringName of the device.
idstringId number of the device.
capabilitiesSensorType[]Sensors that are available on the device.

NodeDiscovered

FieldTypeDescription
nodeNodeReference to a Node.
error?anyAn error message. Present if the Node was not able to communicate with the smartphone in the specified timeout.

SensorType

ValueDescription
ACCELEROMETERRepresents the accelerometer sensor.
GYROSCOPERepresents the gyroscope sensor.
MAGNETOMETERRepresents the magnetometer sensor.
HEART_RATERepresents the heart rate sensor.
LOCATIONRepresents the GPS sensor.

CollectorManager

MethodReturn typeDescription
isEnabled(sensor: SensorType)booleanReturns true if the sensor type is enabled in the initial configuration.
isReady(node: Node, sensor: SensorType)Promise<boolean>Return true if the sensor is ready to collect data from.
prepare(node: Node, sensor: SensorType)Promise<PrepareError>Returns a PrepareError if anything failed in the preparation (e.g., sensor unavailable, no permissions, etc). Returns undefined if the preparation was successful.
startCollecting(node: Node, sensor: SensorType, config?: CollectionConfigurationPromise<void>Starts the data collection of a sensor in a node with the specified configuration.
stopCollecting(node: Node, sensor: SensorType)Promise<void>Stops the data collection of a sensor in a node.
addSensorListener(listener: SensorListener, filters?: ListenerFilter)numberAdds a listener with the specified filters and returns a listener identifier.
removeSensorListener(listenerId?: number)voidRemoves the listener specified by the listenerId. If not provided, removes all listeners.

PrepareError

PropertyTypeDescription
nodeNodeReference to the Node where the PrepareError comes from.
messagestringMessage describing the error.

CollectionConfiguration

PropertyTypeDescription
sensorIntervalSensorIntervalTime between each consecutive sample. Can be a NativeSensorInterval or a value in milliseconds.
batchSizenumberAmount of samples to be sent in each record.

SensorListener

(sensorRecord: SensorRecord<any>) => void

ListenerFilter

PropertyTypeDescription
nodes?Node[]For which nodes the related listener applies.
sensors?SensorType[]For which sensors the related listener applies.

!TIP Filter works as follows:

{ 
  nodes: [node1, /* OR */ node2]
  // AND
  sensors: [SensorType.ACCELEROMETER, /* OR */ SensorType.GYROSCOPE]
}

SensorRecord

PropertyTypeDescription
typeSensorTypeType of the collected data.
deviceIdstringId of the device where the collected data comes from.
samplesT[]List of samples, where T is TriAxialSensorSample, HeartRateSensorSample, or LocationSensorSample

TriAxialSensorSample

PropertyTypeDescription
xnumberComponent x.
ynumberComponent y.
znumberComponent z.

HeartRateSensorSample

PropertyTypeDescription
valuenumberHeart rate value.

LocationSensorSample

PropertyTypeDescription
latitudenumberLatitude coordinate component.
longitudenumberLongitude coordinate component.
altitudenumberAltitude coordinate component.
verticalAccuracynumberEstimated error in the latitude.
horizontalAccuracynumberEstimated error in the longitude.
speednumberEstimated device's speed when the location was acquired.
directionnumberEstimated device's direction when the location was acquired.

PlainMessageClient

FunctionReturn typeDescription
enabled()booleanReturns true if the plain message feature is enabled in the initial configuration.
registerListener(listener: PlainMessageListener)voidRegisters the listener for the feature.
unregisterListener()voidUnregisters the listener for the feature.
send(node: Node, plainMessage: PlainMessage)Promise<void>Sends a message to the specified Node.
sendExpectingResponse(node: Node, plainMessage: PlainMessage, timeout?: number)Promise<ReceivedMessage>Sends a message to the specified Node and wait timeout ms for a response.

PlainMessage

PropertyTypeDescription
messagestringContent of the message.
inResponseTo?PlainMessageContains the message at which the current message is responding. undefined means that the message is not responding to other message.

ReceivedMessage

PropertyTypeDescription
senderNodeIdstringId of the node who sent the message.
PlainMessagePlainMessageMessage received.

PlainMessageListener

(receivedMessage: ReceivedMessage) => void

License

Apache License 2.0

See LICENSE.

Author

Acknowledgements

The development of this library has been possible thanks to the Spanish Ministry of Universities (grant FPU19/05352).

1.3.0

3 months ago

1.2.0

2 years ago

1.2.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago