Validic Mobile Library

Prerequisites

In order to use the Validic Mobile Library, you will need a valid organization id and valid user credentials.
If you do not already have an organization id, you will need to first apply for one.
After your organization is set up, you will need a Validic user id and access token.
You can create a new user by referring to the API documentation.

System Requirements

A minimum sdk of 19 is supported (you may choose a higher value):

android {
    defaultConfig {
        minSdkVersion 19
    }
}

And to use VitalSnap requires a minimum sdk version of 21:

android {
    defaultConfig {
        minSdkVersion 21
    }
}

Installation

Copy the aars

Library in Bundle

and place it in your project’s libs folder:

Library in Project

If your project does not currently contain a libs folder, simply create the folder.
Next open your app’s build.gradle file and add the following lines to it:

android{
...
    repositories {
    ...
        flatDir{ dirs 'libs' }
    }
}

Individual modules

Starting with version 1.6.0 the library has been divided into 4 separate modules.
Add the desired dependencies to your app’s build.gradle file:

dependencies {
    ...
    //For all projects
    implementation ':validicmobile-shared@aar'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.jakewharton.threetenabp:threetenabp:1.2.0'
    implementation 'androidx.work:work-runtime:2.2.0'

    // For bluetooth
    implementation ':validicmobile-ble@aar'
    implementation 'com.polidea.rxandroidble2:rxandroidble:1.9.2'

    //for VitalSnap
    implementation ':validicmobile-ocr@aar'

    //for Samsung Health
    implementation ':validicmobile-shealth@aar'
    implementation files('libs/samsung-health-data-v1.4.0.jar')
    ...

}

Finally initialize the library within your Application class’s onCreate:

    @Override
    public void onCreate() {
        super.onCreate();

        ValidicMobile.getInstance().initialize(this);

    }

If using the bluetooth module, a global error handler will be registered via: RxJavaPlugins.setErrorHandler(). This is the fallback error handler called by RxJava.
If you want to customize this handler, it can be overridden by passing a new instance to ValidicBluetoothManager.setErrorHandler() before calling ValidicMobile.getInstance().initialize():

    @Override
    public void onCreate() {
        super.onCreate();

        // Optionally override global RxJava error handler
        ValidicBluetoothManager.setErrorHandler(new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                //Custom error handling here
            }
        });

        // Required initialization
        ValidicMobile.getInstance().initialize(this);
    }

Please review the RxJava 2.x error handling documentation for more information about this handler.

Session

Session is a singleton object and must be accessed by its getInstance() method. The Session instance stores a Validic User and all pending Record uploads.

A Validic user can be provisioned using the Validic API. Further documentation is available for both the Legacy V1 API and the new Inform API.

You will need to provision the user on your server. The mobile app could then authenticate to the server and retrieve the needed Validic credentials.

To use an existing Validic User, create a User object and provide it to the startSessionWithUser(User) method.

User user = new User("Your Organization ID",
                     "A Validic User ID",
                     "A Validic User Access Token");

Session.getInstance().startSessionWithUser(user);

User data is persisted between app launches but is deleted if endSession() is called.

Session.getInstance().endSession();

Records

There are seven different subclasses of Record that will be collected by Validic

Choose a data type to test with and construct a Record object:

Weight testRecord = new Weight();
testRecord.setWeight(new BigDecimal("200"));

Submitting

Records can be submitted individually:

Weight record = new Weight();
record.setWeight(new BigDecimal("200"));

Session.getInstance().submitRecord(record);

or as a group:

List<Record> records = new ArrayList<>();
Weight record1 = new Weight();
Weight record2 = new Weight();
records.add(record1);
records.add(record2);

Session.getInstance().submitRecords(records);

Listener

When Session uploads a record it will notify the stored SessionListener. To receive these notifications, register a SessionListener by calling setSessionListener(SessionListener):

SessionListener listener = new SessionListener() {
    @Override
    public void didSubmitRecord(Record record) {
        // If you want to store a copy locally, now would be the time
    }

    @Override
    public void didFailToSubmitRecord(Record record, Error error) {

        Toast.makeText(getApplicationContext(), "Failed to upload record!", Toast.LENGTH_LONG).show();
    }
};

Session.getInstance().setSessionListener(listener)

If a 400 error is returned from the server, the record will be discarded and SessionListener.didFailToSubmitRecord(Record, Error) will be called. If the record is submitted successfully, didSubmitRecord(Record) will be called.

Bluetooth

Peripherals

A BluetoothPeripheral represents Bluetooth peripheral models that can be discovered or read from by BluetoothPeripheralController.

The class contains various information to be displayed to the user:

To retrieve a List of supported perpherals simply call:

List<BluetoothPeripheral> supportedPeripherals = BluetoothPeripheral.getSupportedPeripherals();

for(BluetoothPeripheral peripheral : supportedPeripherals) {
    Log.v("Test", "Found peripheral: " + peripheral.getName());
}

Pairing

Check if a BluetoothPeripheral requires pairing by calling:

BluetoothPeripheral peripheral = // ...
peripheral.requiresPairing()

If it does, the user must pair the perpheral before they can take a reading.

BluetoothPeripheral peripheral = // Choose a perpheral from the supported perpherals list
BluetoothPeripheralControllerListener listener = new BluetoothPeripheralControllerListener(){
    @Override
    public void onFail(BluetoothPeripheralController peripheralController, BluetoothPeripheral peripheral, BluetoothPeripheralController.BluetoothError error) {
        // Tell the user to retry the pairing process
    }

    @Override
    public void onSuccess(BluetoothPeripheralController peripheralController, BluetoothPeripheral peripheral, List<Record> records) {
        // The peripheral is now paired
        // Records received during pairing are not submitted
    }

    @Override
    public void onPeripheralDiscovered(final BluetoothPeripheralController peripheralController, BluetoothPeripheral peripheral) {
        // The peripheral is currently pairing...
    }
};

BluetoothPeripheralController controller = new BluetoothPeripheralController();
controller.pairPeripheral(peripheral, listener);

String pairingInstructions = peripheral.getPairingInstruction();
// Display the pairing Instructions to the user

Reading

Once you are ready to read from a peripheral, the process is fairly similar to the pairing process.
You’ll want to first show the peripheral’s instructions and eventually show the reading instructions once onPeripheralDiscovered is called.

BluetoothPeripheral peripheral = // The peripheral you want to read from
BluetoothPeripheralControllerListener listener = new BluetoothPeripheralControllerListener(){

    @Override
    public void onPeripheralDiscovered(final BluetoothPeripheralController peripheralController, BluetoothPeripheral peripheral) {
        // Display the reading Instructions to the user
    }

    @Override
    public void onFail(final BluetoothPeripheralController peripheralController, BluetoothPeripheral peripheral, BluetoothPeripheralController.BluetoothError error) {
        super.readingFailedForPeripheral(peripheralController, peripheral, error);
        switch (error) {
            case BluetoothError.BluetoothTurnedOff:
                Toast.makeText(getApplicationContext(), "Your bluetooth is off!", Toast.LENGTH_LONG).show();
                break;
            case BluetoothError.NoUser:
                Toast.makeText(getApplicationContext(), "You have not started the session yet!", Toast.LENGTH_LONG).show();
                break;
            case BluetoothError.Cancelled:
                Toast.makeText(getApplicationContext(), "Reading has been cancelled", Toast.LENGTH_LONG).show();
                break;
        }

        Log.e("Error", error.getMessage());
    }

    @Override
    public boolean onSuccess(final BluetoothPeripheralController peripheralController, BluetoothPeripheral peripheral, List<Record> records) {

        // If you want to auto-submit records, return true
        return true;

        // else if you want to require the user to confirm the record, return false
        // return false;
    }
};

String instruction = peripheral.getInstruction();
// Display the instruction to the user

BluetoothPeripheralController controller = new BluetoothPeripheralController();
controller.readFromPeripheral(peripheral, listener);

Auto-submission of Records

You have the option to automatically submit records as soon as they are captured, or you can wait for user confirmation before submitting the record. For auto submission, return true inside your onSuccess() method. If you return false you MUST call submitRecord() or the record will be discarded.

Session.getInstance().submitRecord(record);

Passive Reading

The ValidicMobile library supports reading from bluetooth peripherals without any user interaction once a device has been successfully paired.
The PassiveBluetoothManager manages background reads and interactions with any BluetoothPeripheralController objects in use.
Reading or pairing a peripheral in the foreground will cancel any in progress readings from the background and will restart monitoring in the background once all bluetooth interaction in the foreground has finished.

To set peripherals to be read in the background use the PassiveBluetoothManager.getInstance() singleton as such:

Set<BluetoothPeripheral> peripherals = new HashSet<>();
peripherals.add(BluetoothPeripheral.getPeripheralForID(1);
peripherals.add(BluetoothPeripheral.getPeripheralForID(2);
peripherals.add(BluetoothPeripheral.getPeripheralForID(3);

PassiveBluetoothManager.getInstance().setPassivePeripherals(peripherals);

To start the service in the foreground. For Android versions >= 25 a notification must be supplied to allow the background scanning to continue to run while the app is not in the foreground. Note that on Android versions >= 26 a notification channel must be created.
As of Android 26 background scanning without a service will be the default. Please see BluetoothLeScanner documentation for more information.
If a foreground service is preferred it can still be used by setting:

PassiveBluetoothManager.getInstance().setPreferForegroundService(true)

Note that on Android version >= 26 a notification channel must be created.
If the application is targeting at least android-28, the FOREGROUND_SERVICE permission must be added to the application’s manifest as well

<manifest>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
</manifest>

Please review documentation regarding Doze for more information.

Set<BluetoothPeripheral> peripherals = new HashSet<>();
peripherals.add(BluetoothPeripheral.getPeripheralForID(1);
peripherals.add(BluetoothPeripheral.getPeripheralForID(2);
peripherals.add(BluetoothPeripheral.getPeripheralForID(3);

if (Build.VERSION_CODES.O <= Build.VERSION.SDK_INT) {
    // Create Notification Channel
    NotificationChannel channel = ...
}

// Create a notification
Notification notification = ...
NotificationParams params = new NotificationParams(1, notification);

PassiveBluetoothManager.getInstance().setPassivePeripherals(peripherals, params);

To stop monitoring peripherals in the background set the background peripherals to null or an empty set

PassiveBluetoothManager.getInstance().setPassivePeripherals(null);

To stop monitoring peripherals via the Notification when using a foreground service a PendingIntent should be used to notify a BroadcastReceiver to take the appropriate action:

Register the BroadcastReceiver in the Manifest:

<receiver android:name="com.validic.mobile.ble.BluetoothServiceStopReceiver">
    <intent-filter>
        <action android:name="com.validic.mobile.ble.STOP_BLUETOOTH_SERVICE" />
    </intent-filter>
</receiver>

Add a PendingIntent to be used when the notification is interacted with:

Intent intent = new Intent(BluetoothServiceStopReceiver.ACTION_STOP_BLUETOOTH_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 200, intent, 0);

// Create a notification that uses the PendingIntent
Notification notification = new NotificationCompat.Builder(context)
                                .addAction(icon, message, pendingIntent)
                                // Continue setting up notification
                                .build();

Listener

Records will be automatically uploaded as they are read.
In order to receive events from the the PassiveBluetoothManager, use one of the following two methods.
In both cases a PassiveBluetoothManager.BluetoothListener must be created in order to receive the events:

BluetoothPassiveManager.BluetoothListener listener = new BluetoothPassiveManager.BluetoothListener(){

    @Override
    public void onSuccess(BluetoothPeripheral peripheral, List<Record> records) {
        // Records received in the background are automatically uploaded
    }
    @Override
    public void onFail(BluetoothPeripheral peripheral, BluetoothPeripheralController.BluetoothError error) {
        // Reading failed in the background
    }
    @Override
    public void onPeripheralDiscovered(BluetoothPeripheral peripheral){
        // A peripheral was discovered and we have setup a connection
    }

    @Override
    public void onBackgroundStart(){
        // Background scanning has begun
    }

    @Override
    public void onBackgroundStop(){
        // Background scanning has stopped
    }
};

Note: The PassiveBluetoothManager uses a WeakReference to manage the passive listener. Activities and Fragments may be destroyed and then recreated when coming from the Background 
into the foreground. It is recommended to have a singleton maintain a strong reference to your listener or to set the listener in `Activity.onCreate` or `Fragment.onAttach`

This listener can either be set on the PassiveBluetoothManager instance:

PassiveBluetoothManager.getInstance().setBluetoothListener(listener);

or a PassiveBluetoothReceiver can be registered using the LocalBroadcastManager

PassiveBluetoothReceiver receiver = new PassiveBluetoothReceiver(listener);
LocalBroadcastManager.getInstance(context).registerReceiver(receiver, PassiveBluetoothReceiver.getIntentFilter());

Considerations

There are several situations where passive bluetooth reading is cancelled and resumed at a later time:

When passive bluetooth reading is cancelled, any current passive readings will be cancelled, and the PassiveBluetoothManager.BluetoothListener will receive onCancelled for each of the peripherals.

OCR

OCR provides the capability to obtain readings from devices without requiring Bluetooth integration. The recognition process is managed by an instance of ValidicOCRController.
A simple example application, “MiniApps/VitalSnap”, is provided to illustrate the OCR API.

Overview

An instance of the ValidicOCRController class is used to manage the recognition process. An OCRPeripheral represents the peripheral being scanned. The controller is initialized with a peripheral object.
The recognition process involves a fragment that contains the camera preview and an overlay image. The fragment must be injected into your view.
OCR processing begins as soon as the fragment is injected into the view. An instance of ValidicOCRResultListener must be registered to receive intermediate and final results. The camera capture session ends when the fragment is paused.

Peripherals

OCRPeripheral represents peripheral models that can be processed by OCR.

A peripheral object contains various properties which can be displayed to the user:

ValidicOCRActivity

Validic provides an activity to encapsulate the OCRFragment. It will perform the necessary permission checking for Android versions > 23. To start an instance of the activity start by using an intent and Activity.startForResult();

public void startOCRActivity() {
    Intent intent = new Intent(this, ValidicOCRactivity.class);
    OCRPeripheral peripheral = OCRPeripheral.getPeripheralForId(1); // One Touch
        intent.putExtra(ValidicOCRActivity.PERIPHERAL_ID, peripheral.getPeripheralID());
}

Optionally a File path can added to the request if needed:

File f = new File(getFilesDir(), "image.png");
intent.putExtra(ValidicOCRActivity.IMAGE_PATH f.getAbsolutePath());

Then start the activity for a result

activity.startActivityForResult(intent, ValidicOCRActivity.ACTION_READ_OCR);

To receive a converged result in the same activity you launched the ValidicOCRActivity override OnActivityResult()

protected override void OnActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ValidicOCRActivity.ACTION_READ_OCR) {
        if (resultCode. == Activity.RESULT_OK) {
            OCRPeripheral peripheral = (OCRPeripheral) data.getSerializableExtra(ValidicActivity.Peripheral_KEY);
            Record record = (Record) data.getSerializableExtra(ValidicActivity.RECORD_KEY);
            String s = data.getStringExtra(ValidicActivity.IMAGE_KEY);
            File f = new File(s);
            Uri uri = Uri.fromFile(f);
            if (uri != null) {
                bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));
                // Bitmap is now in memory
            }
        } else if (resultCode == ValidicOCRActivity.ERROR_WRITING_FILE) {
            OCRPeripheral peripheral = (OCRPeripheral) data.getSerializableExtra(ValidicActivity.Peripheral_KEY);
            Record record = (Record) data.getSerializableExtra(ValidicActivity.RECORD_KEY);
        } else {
            // Error with activity
        }
    }

}

ValidicOCRController

The ValidicOCRController requires an OCRPeripheral for its initializers.

To obtain an OCRPeripheral, you may choose from a list of all available devices:

List<OCRPeripheral> allSupportedPeripherals = OCRPeripheral.getSupportedPeripherals();

or if you know the specific peripheral type that you want:

final static int ONETOUCH_ULTRAMINI = 1;
OCRPeripheral onetouchPeripheral = OCRPeripheral.getPeripheralForOCRPeripheralType(ONETOUCH_ULTRAMINI);

Once a peripheral is obtained, construct the ValidicOCRController and assign it an instance of ValidicOCRResultListener:

ValidicOCRController ocrController = ValidicOCRController.initWithOCRPeripheral(peripheral);
ocrController.setListener(listener);

Runtime unit selection

For any glucose meter in our lineup of supported meters, you can now specify mmol/l or mg/dL at runtime for a given reading. If no unit is provided, mg/dL is assumed.

An example using the ValidicOCRActivity:

OCRPeripheral peripheral = OCRPeripheral.getPeripheralForId(1); // One Touch

Intent intent = new Intent(this, ValidicOCRActivity.class);
intent.putExtra(ValidicOCRActivity.PERIPHERAL_ID, peripheral.getPeripheralID());
intent.putExtra(ValidicOCRActivity.GLUCOSE_UNIT_KEY, Unit.Glucose.MMOLL);

An example using the ValidicOCRController:

ValidicOCRController ocrController = ValidicOCRController.initWithOCRPeripheral(peripheral, Unit.Glucose.MMOLL);

ValidicOCRFragment

The camera preview and overlay are contained in a fragment that must be injected with an instance of ValidicOCRController. Ensure your view contains a FrameLayout, preferably fullscreen in size, and give it a referencable id such as activity_ocr_fragment_container.

Injection Example:

ocrController.injectOCRFragment(getFragmentManager().beginTransaction(),
                R.id.activity_ocr_fragment_container)
                .commit();

This will immediatly begin the OCR process.

View Structure

The preview and overlay are handled by the library, but showing intermediate results are not. You must provide a fullscreen container for the fragment in order for the OCR functionality to be reliable. The relevent part of the overlay is gaurenteed to be no larger than half the height of the screen. Should you choose to show any other views during the OCR process, please restrict them to the lower half of the screen.

Listener

During OCR processing, methods on an instance of ValidicOCRResultListener will be invoked.

public void didProcessResult(VitalSnapResult vitalSnapResult) is invoked for each camera frame captured and provides intermediate results. The VitalSnapResult object contains the current recognized string. The result string can be displayed to the user as an indication of what portion of the display is not being recognized. Depending on the peripheral, the result string may contain linefeeds representing multiple lines being recognized. The result string also indicates whether glare is affecting OCR of a particular digit by returning the * char, and therefore can be helpful feedback for the user to avoid negative effects of glare on the reading.

Log.v(TAG, "Partial result: " + vitalSnapResult.toString());

public void didCompleteReading(Record record, Bitmap bitmap, ValidicOCRPeripheral validicOCRPeripheral) is invoked when OCR processing has completed with sufficient confidence.

if (validicOCRPeripheral.getType() == Peripheral.PeripheralType.GlucoseMeter) {
    Diabetes diabetesRecord = (Diabetes) record;
    Log.v(TAG, "Blood glucose captured: " + diabetesRecord.getBloodGlucose().toString());
}

The value received from the listener should be verified by the user and then submitted to the Validic server:

// After verification, queue the record and the image to be uploaded to the server
Session.getInstance().submitRecord(record, bitmap);

The listener is passed a Record subclass appropriate for the peripheral and the matching cropped preview image with embedded additional metadata. The recognized values returned in the record should be visually validated by the user. The cropped preview image can be displayed to the user to validate the recognized values before uploading to the server.

OCR Lifecycle

OCR processing commences when the ocr fragment is injected into your view.

Processing is stopped when the fragment is paused or a final value has been converged. If no value has been converged on, the fragment will resume processing onResume. To restart or to take additional readings with the same peripheral, simply call restartOCR on the ValidicOCRController.

ocrController.restartOCR();

Google Fit

This project integrates Google Fit with the Validic Mobile Aggregator to passively collect data on behalf of an end user. Validic ensures that our Mobile SDK makes use of the minimal scopes needed for our supported data types. Additionally, Validic does not request or use any scopes indicated as sensitive or restricted by Google. Avoiding sensitive and restricted scopes ensures that you will not need to complete Google’s sensitive scope verification or restricted scope verification and security assessment - both of which are time-consuming and costly.

Be aware that Google has an extensive and potentially lengthy verification process. Google manages their own verification process and it is subject to change at any time at their discretion. It is your responsiblity to review, understand, and follow Google’s verification process and provide Google with all information they request before sharing your application publicly. Validic does not provide direct support for Google’s verification process, however, Google’s Verification FAQ is detailed and provides you with the information you’ll need to complete the process. See the Customer Requirements section below for highlights on setting up, developing, and verifying for Google Fit.

Customer Requirements

Google Fit is available for Inform (V2) Validic customers. If you are a Legacy customer and are interested in Google Fit, please reach out to your Validic Client Success Manager.

Validic’s Google Fit integration works with Google Fit for Android. Google Fit for Android uses Google’s OAuth API for accessing user data. There is setup that must occur before you implement Google Fit into your mobile project so that your users can consent to sharing and share their data successfully:

Before you start development, in Google Api Console you must:

After completing the above setup, you can start development using the instructions in this tutorial. While you are developing, testing can occur with Google user accounts that are registered to the same domain as the mobile project.

After development and testing are complete and before you launch your app, you will need to submit a request to Google for Oauth API Verification. There are certain instances where this may not be required (see Google’s FAQ for details).

Once you submit your verification request, you may be required to provide some or all of the following:

Google dictates their own verification process so you may be asked to provide additional information as well. See Veification Requirements for more information. Be sure to respond to Google’s requests as quickly as possible to minimize delays in getting your verification approved.

Installation

Add aggregator.aar, aggregator-fit.aar and validicmboile-shared.aar to the libs/ folder of your application

Dependencies

Add the libraries and their dependencies


dependencies{

    //Google fit dependencies
    implementation 'com.google.android.gms:play-services-fitness:17+'
    implementation 'com.google.android.gms:play-services-auth:17+'
    //validic dependencies
    implementation files('libs/validicmobile-shared.aar')
    implementation files('libs/aggregator-fit.aar')
    implementation files('libs/aggregator.aar')

    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
}

Initialization

The ValidicFitManager will be initialized at app startup and will immediately start listening for previously subscribed data types. It is recommended that you set a listener on the ValidicFitManager during your applicatin onCreate() method

    private final ValidicFitManager.AggregatorListener fitListener = new AggregatorManager.AggregatorListener() {
        @Override
        public void onRecords(@NotNull Map<Record.RecordType, Integer> summary) {
            // summary of number of records collected and queued for submission to the Validic Inform API
        }

        @Override
        public void onException(@NotNull AggregatorException exception) {
            // An exception has occurred. Check if the exception can be handled by the sdk
            if (exception.getHasResolution()) {
                // handle the resolution using an activity
                exception.handleResolution(activity, 999);
            }
            //otherwise display error
        }
    };

Requesting Permissions

Before collecting any data from Google Fit, a Google Account must be associated with the ValidicFitManager, and the end user must authorize the app to collect data on their behalf.

Previously granted permissions

To check if permissions have already been granted the hasDataAccess(Set<DataType> dataTypes) method can be used

    Set<DataType> dataTypes = new HashSet<>();
    dataTypes.add(DataType.TYPE_STEP_COUNT_DELTA);
    Map<DataType, Boolean> = ValidicFitManager.INSTANCE.hasDataAccess(dataTypes);

The map returned from hasDataAccess(Set<DataType> dataTypes) associates a DataType with true, if a valid Google account exists and permission has previsouly been granted, or false otherwise.

Requesting new permissions

To request permission from the end user to authorize data collection, first add result handling to your activity


@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ValidicFitManager.INSTANCE.handlePermissionResult(this, requestCode, resultCode, data);
    }

Next, the requestPermissions(Activity, Set<DataType>, Integer, Integer, PermissionHandler) method can be used to authorize your application

// MyActivity
    Set<DataType> dataTypes = new HashSet<>();
    dataTypes.add(DataType.STEP_COUNT_DELTA);
    ValidicFitManager.INSTANCE.requestPermissions(MyActivity.this, dataTypes, new AggregatorManager.PermissionsHandler<DataType>() {
                        @Override
                        public void onResults(Map<DataType, Boolean> dataTypes) {
                          // do work with results. 
                        }
                    });
Android 10

For the Android client Apps that target API 28 the permission below is required in the AndroidManifest.xml. The Validic SDK will add this permission to its manifest so that it will be merged with the client when they add our library to their application.

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

This permission is required to subscribe to the following data types:

For apps targeting Android 10, this permission must be requested from the user at runtime

    if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACTIVITY_RECOGNITION)
              != PackageManager.PERMISSION_GRANTED) {
          // Permission is not granted
    }

    ActivityCompat.requestPermissions(thisActivity,
                      arrayOf(Manifest.permission.ACTIVITY_RECOGNITION),
                      MY_PERMISSIONS_REQUEST_ACTIVITY_RECOGNITION);

Subscriptions

After Permissions have been granted for a DataType the ValidicFitManager can be used to add subscriptions to data change events from Google Fit. Data will automatically will queued for submission to the Validic API when internet is available on the device

    Set<DataType> dataTypes = new HashSet<>();
    dataTypes.add(DataType.TYPE_STEP_COUNT_DELTA);
    ValidicFitManager.INSTANCE.subscribe(dataTypes);

To unregister from receiving data update events use the ValidicFitManager to unsubscribe

    ValidicFitManager.INSTANCE.unsubscribe(DataType.TYPE_STEP_COUNT_DELTA)

Get a list of currently subscribed DataTypes

    List<DataType> subscriptions = ValidicFitManager.INSTANCE.currentSubscriptions()

Currently Supported DataTypes

Records collected are automatically submitted to the Validic Inform API and a summary of records processed will be sent to any AggregatorListener#onRecords(summary) registered using ValidicFitManager.INSTANCE.setListener(fitListener) For an example of setting a listener see Initialization

Logout

To remove all subscriptions and log an end user out simply call

    ValidicFitManager.INSTANCE.disconnect()

S Health

The Validic Mobile library provides a simple way to read and upload data from S Health to Validic. Through the SHealthManager you can subscribe to specific S Health data types and automatically upload them to Validic in the background as new data is recorded.

General setup

You can download the jar from the Samsung Health website: http://developer.samsung.com/health

In order to test you must enable developer mode in the S Health app on your phone. As of shealth sdk version 1.2.1, you enable developer mode by going to the More->Settings->About S Health and tapping the version number until “(Developer Mode)” is displayed next to it.

S Health Developer Mode

Note: In order to gain access to S Health data without developer mode enabled you must first be approved by Samsung.
You can request access here: http://developer.samsung.com/health/apiaccess

Before you can use the S Health features of the library, be sure to import the Samsung Digital Health Healthdata jar into your project by adding the following line to your gradle dependencies:

implementation files('libs/samsung-health-data-v1.4.0.jar')

Subscriptions

Samsung provides several data types that can be subscribed to for notification when data changes.

The available data types are:

Unsupported types (see https://developer.samsung.com/health/android/release-note.html for more information): * com.samsung.health.food_info * com.samsung.health.food_intake

NOTE: Data types that are going to be used in your application must be added to the AndroidManifest.xml.

<application>
    // Rest of app

    <meta-data android:name="com.samsung.android.health.platform_type" android:value="rel" />
    <meta-data
        android:name="com.samsung.android.health.permission.read"
        android:value=" com.samsung.health.blood_glucose;
                        com.samsung.health.blood_pressure;
                        com.samsung.health.body_temperature;
                        com.samsung.health.caffeine_intake;
                        com.samsung.health.exercise;
                        com.samsung.health.hba1c;
                        com.samsung.health.heart_rate;
                        com.samsung.health.oxygen_saturation;
                        com.samsung.health.sleep;
                        com.samsung.health.sleep_stage;
                        com.samsung.health.uv_exposure;
                        com.samsung.health.water_intake;
                        com.samsung.health.weight;
                        com.samsung.shealth.step_daily_trend;
                        com.samsung.shealth.nutrition" />

Subscription Sets

Data types are grouped into logical sets; SHealthSubscription.SubscriptionSet is an Enum that groups several data types together to register for related data types. The available subscription sets are:

The group of data types for each subscription set can be found using:

    SHealthSubscription.permissionStringsForSubscriptionSet(SHealthSubscription.SHealthSubscriptionSet.FITNESS);

Requesting Permissions

Before a subscription can be created the related permissions must be granted.

Requesting permissions will immediately display a Samsung Health dialog through which the user may grant or deny requested permissions.
The result of the user’s selections will be delivered through an SHealthManager.SHealthListener callback; specifically the onPermissionChange method will be called.
Please read SHealth Listener for more information regarding registering and implementing this callback.

Request permissions:

    SHealthManager.getInstance().requestPermissionsForDataTypes("com.samsung.health.blood_glucose", "com.samsung.health.blood_pressure");

Subscribing

Be sure to request permissions as described above before subscribing.

Subscribing to S Health updates only needs to be done once for a user. The subscriptions will be persisted across app launches in the Session object.

Data types can be subscribed to individually:

    SHealthManager.getInstance().requestSubscriptionsForDataTypes("com.samsung.health.blood_glucose", "com.samsung.health.blood_pressure");

or as part of a subscription set:

     SHealthManager.getInstance().
     requestSubscriptionsForDataTypes(SHealthSubscription.permissionStringsForSubscriptionSet(SHealthSubscription.SHealthSubscriptionSet.FITNESS));

To start the S Health service in the foreground. For Android versions >= 26 a notification must be supplied to allow the background scanning to continue to run while the app is not in the foreground.
Please note that on Android version >= 26 a notification channel must be created If the application is targeting at least android-28, the FOREGROUND_SERVICE permission must be added to the application’s manifest as well

<manifest>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
</manifest>

Please review documentation regarding Doze for more information.

Individual subscription:

    if (Build.VERSION_CODES.O <= Build.VERSION.SDK_INT) {
        // Create Notification Channel
        NotificationChannel channel = ...
    }

    // Create a notification
    Notification notification = ...
    NotificationParams params = new NotificationParams(1, notification);

    SHealthManager.getInstance().requestSubscriptionsForDataTypes(param, "com.samsung.health.blood_glucose", "com.samsung.health.blood_pressure");

As a subscriptions set:

    if (Build.VERSION_CODES.O <= Build.VERSION.SDK_INT) {
        // Create Notification Channel
        NotificationChannel channel = ...
    }

    // Create a notification
    Notification notification = ...
    NotificationParams params = new NotificationParams(1, notification);

    SHealthManager.getInstance()
        .requestSubscriptionsForDataTypes(
                params, 
                SHealthSubscription.permissionStringsForSubscriptionSet(SHealthSubscription.SHealthSubscriptionSet.FITNESS)
        );

To stop monitoring peripherals via the Notification when using a foreground service a PendingIntent should be used to notify a BroadcastReceiver to take the appropriate action:

Register the BroadcastReceiver in the Manifest:

<receiver android:name="com.validic.mobile.shealth.SHealthServiceStopReceiver">
    <intent-filter>
        <action android:name="com.validic.mobile.shealth.STOP_SHEALTH_SERVICE" />
    </intent-filter>
</receiver>

Add a PendingIntent to be used when the notification is interacted with:

Intent intent = new Intent(SHealthServiceStopReceiver.ACTION_STOP_SHEALTH_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 200, intent, 0);

// Create a notification that uses the pending intent
Notification notification = new NotificationCompat.Builder(context)
                                .addAction(icon, message, pendingIntent)
                                // Continue setting up notification
                                .build();

Historical Fetch

Validic provides the ability to query six months of data for a subset of data types provided by Samsung Health by querying a group of SHealthSubscription.SHealthHistoricalSet

Two historical sets are available:

To execute a historical fetch use the SHealthManager.getInstance() to perform the historical fetch operation

SHealthManager.getInstance().fetchHistoricalSets(SHealthSubscription.SHealthHistoricalSet.FITNESS);

SHealth Listener

To listen for events from SHealth, a SHealthListener can be set to receive events from SHealth.

SHealthManager.getInstance().setSHealthListener(new SHealthListener() {

   @Override
    public void onError(final SHealthError error) {
        // An error specific to SHealth has occured. Check the error type for additional actions.
    }

    @Override
    public void onPermissionChange(String[] acceptedPermissions, String[] deniedPermissions) {
        // Permissions were requested as part of the workflow.
    }

    @Override
    public void onRecords(Map<Record.RecordType, Integer> summary) {
        // Records were gathered by an SHealth data change.
    }

    @Override
    public void onHistoricalFetch(Map<RecordType, Integer> summary) {
        // Records were fetched from SHealth history.
    }
}

Note: The SHealthManager uses a WeakReference to manage the passive listener. Activities and Fragments may be destroyed and then recreated when coming from the Background into the foreground. It is recommended to have a singleton maintain a strong reference to your listener or to set the listener each time in Activity.onCreate or Fragment.onAttach

Example

A typical use of the S Health component would be to create a Switch that adds the required subscriptions when toggled on and removes them when toggled off. Example:

new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        if (isChecked) {

            SHealthManager.getInstance().addSubscriptionsForDataTypes(
                    SHealthSubscription.permissionStringsForSubscriptionSet(SHealthSubscription.SHealthSubscriptionSet.FITNESS));

        } else {
            SHealthManager.getInstance().removeAllSubscriptions();
        }
    }
};

Notes

In order to resume listening for subscriptions upon app launch you must call observeCurrentSubscriptions() inside your application’s onCreate function:

public class YourApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        ValidicMobile.getInstance().initialize(context);
        SHealthManager.getInstance().observeCurrentSubscriptions();
    }

}

In order to resume listening for subscriptions upon device reboot, you must add a BroadcastReceiver to your manifest:

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            ValidicMobile.getInstance().initialize(context);
            SHealthManager.getInstance().observeCurrentSubscriptions();
        }
    }
}

In your manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<!-- ... -->

<receiver android:name=".BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Calling Session.getInstance().endSession() will remove all S Health subscriptions and stop listening for new data.

LifeScan

Installation

Add OneTouchMobileConnectivityKit.aar, validicmobile_lifescan.aar, and validicmobile-shared.aar, to the libs/ folder of your project.

Licensing

To properly install the license, please start by: 1) Making sure your file is named onetouch-license.json 1) Moving the file into your project so that its location looks like the following: app/src/main/assets/onetouch-license.json 1) Making sure your application’s app id matches the key Android App Identifiers in the license file.

Note: Modifying the content of the AAR or license file in anyway will invalidate the license.

Dependencies

Add these dependency references to your build.gradle.

    //Validic dependencies
    implementation files('libs/validicmobile-shared.aar')
    implementation files('libs/validicmobile_lifescan.aar')

    implementation 'com.jakewharton.threetenabp:threetenabp:1.2.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31'

    //LifeScan dependencies
    implementation files('libs/OneTouchMobileConnectivityKit.aar')
    implementation 'com.androidx.core:1.2.0'
    implementation 'com.jakewharton.timber:timber:4.7.0'
    implementation "com.fasterxml.jackson.core:jackson-databind:2.9.8"
    implementation 'com.getkeepsafe.relinker:relinker:1.2.2'

Initialization

Initialize the ValidicMobile SDK as detailed in the Installation section at the beginning of this guide. Once initialized and and active Session has been started and instance of ValidicLifeScan can be retrieved:

    ValidicLifeScan.getInstance(context);

Scanning

Scanning can be started with scan():

    ValidicLifeScan.getInstance(context).scan(TimeUnit.SECONDS.toMillis(timeout), new LifescanScanningListener() {
        @Override
        public void onScanComplete(final List<OneTouchDevice> devices, OneTouchError error) {
        }

        @Override
        public void onDeviceDiscovered(OneTouchDevice device) {
        }
    });

Scanning can be stopped with:

    ValidicLifeScan.getInstance(context).stopScan()

Pairing

In order to read from a device the device must first be paired. Pairing requests require a previously discovered device from scan().

    ValidicLifeScan.getInstance(context).pair(discoveredDevice, new LifescanPairingListener() {
        @Override
        public void onSuccess(OneTouchDevice device) {
        }

        @Override
        public void onError(OneTouchDevice device, OneTouchError error) {
        }
    });

Paired devices can be retrieved with getPairedDevices()

    List<OneTouchDevice> pairedDevices = ValidicLifeScan.getInstance(context).getPairedDevices();

Reading

Once a device is paired a read can be requested:

    ValidicLifeScan.getInstance(context).read(mSelectedDevice, new LifescanReadListener() {
        @Override
        public void onConnected(OneTouchDevice device) {
        }

        @Override
        public void onResults(OneTouchDevice device, final List<Diabetes> records, OneTouchError error) {
            // records may be an empty list if there were not any new measurements to read.
        }
    });

Note that previously read records are not returned to the same paired device again.
Subsequent reads from the same device without taking new measurements will return an empty list of records.

Background Reading

Background reads can be performed with devices that have been successfully paired. To do so a foreground Service will be started that handles device connections and reads.

Reading can be started by calling:

    ValidicLifeScan.getInstance(context).startBackgroundRead(params);

By passing nulls a default Notification will be used. A Notification can be provided, but note that if providing a custom notification its PendingIntent must be configured correctly to offer a stop action from the notification, or the service must be stopped manually elsewhere.

Here is an example of building a Notification that has a PendingIntent action that will stop the background read service:

    private void startBackgroundReadService(Context context) {
        String notificationId = UUID.randomUUID().toString();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(notificationId, "LifeScan Background " + 
                    "Notification", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        Notification notification = getBackgroundReadNotification(context, notificationId);
        ValidicLifeScan.getInstance(context).startBackgroundRead(new NotificationParams(123123, notification));
    }

    private Notification getBackgroundReadNotification(Context context, String notificationChannelId) {
        Intent serviceIntent = new Intent(context, LifescanBackgroundReadService.class);
        serviceIntent.putExtra(LifescanBackgroundReadService.INTENT_KEY_STOP, true);
        PendingIntent stopServicePendingIntent = PendingIntent.getService(context, 0, serviceIntent, 0);

        return new NotificationCompat.Builder(context, notificationChannelId)
                .addAction(R.drawable.stop_action_icon_visible_pre_nougat, "Stop Service", stopServicePendingIntent)
                .setContentTitle("Custom Content Title")
                .setContentText("Helpful notification content.")
                .setColor(ContextCompat.getColor(context, R.color.your_accent_color_perhaps))
                .setSmallIcon(R.drawable.ic_icon_notification)
                .build();
    }

To stop background reading call:

    ValidicLifeScan.getInstance(context).stopBackgroundRead();

DeviceManager

To access properties and methods from the Lifescan device manager, use getNativeManager()

    OneTouchDeviceManager deviceManager = ValidicLifescan.getInstance(context).getNativeManager();
    OneTouchManagerState state = deviceManager.getManagerState();
    if(state == READY){
        //do something
    }

This can be used to add listeners for events as well.

    ValidicLifeScan.getInstance(context).getNativeManager().addListener(new OneTouchManagerListener(){

        @Override
        void onStateChanged(OneTouchDeviceManagerState newState){
            if(newState == READY){
                //do something
            }        
        }
    });