Skip to content

AferoJavaSDK Overview

Tony Myles edited this page Oct 5, 2017 · 4 revisions

AferoJavaSDK Overview

Modules

Core

  • Provides DeviceCollection, DeviceModel, AferoClient, ConclaveClient, etc.
  • Required.
  • Dependencies: RxJava, Jackson

Client Retrofit2

  • Provides Afero Cloud API calls for sign in/out, automatic token refresh, writing device attributes, adding/removing devices from a user account, etc.
  • Optional. (Must provide concrete implementation of AferoClient interface)
  • Dependencies: Retrofit2, okhttp3

Soft Hub

  • AferoSofthub: Bridge between Afero BLE-enabled devices and Afero Cloud.
  • DeviceWifiSetup: Wifi setup for Afero Wifi devices.
  • Optional.
  • Dependencies: Android

Android

  • Basic Android compatibility support classes.

AferoClientRetrofit2

AferoClientRetrofit2 is a concrete implementation of the AferoClient interface using Retrofit2 and okhttp3.

Highlights

In addition to the base AferoClient calls, AferoClientRetrofit2 provides calls to fetch an access token, account tracking, password reset, sign out, etc.

public Observable<AccessToken> getAccessToken(String user, String password)

public void setOwnerAndActiveAccountId(String accountId)

public Observable<Void> resetPassword(String email)

public void signOut(String userId, String mobileClientId)

Example

AferoClientRetrofit2.Config aferoClientConfig = new AferoClientRetrofit2.ConfigBuilder()
    .oauthClientId(myOAuthClientId)
    .oauthClientSecret(myOAuthClientSecret)
    .build();

mAferoClient = new AferoClientRetrofit2(aferoClientConfig);

mAferoClient.setOwnerAndActiveAccountId(accountId);

mAferoClient.setToken(new AccessToken(accessToken, refreshToken));

DeviceModel

The DeviceModel class represents the state and meta-data of a given device.

Observing state changes

    deviceModel.getUpdateObservable()
        .subscribe(new Action1<DeviceModel>() {
            @Override
            public void call(DeviceModel deviceModel) {
                // device state has changed
            }
        });

Writing an attribute

    deviceModel.writeAttributes()
        .put(attrId, value)
        .commit()
        .subscribe(new Action1<WriteAttributeOperation.Result>() {
            @Override
            public void call(WriteAttributeOperation.Result writeResult) {
                // results reported here
                AfLog.d("result = " + writeResult.status.toString());
            }
        });

Reading an attribute

    DeviceProfile.Attribute powerAttribute = deviceModel.getAttributeById(POWER_ATTRIBUTE_ID);
    AttributeValue value = deviceModel.readCurrentValue(powerAttribute);
    AfLog.d("power value = " + value.toString());

DeviceCollection

The DeviceCollection manages the devices associated with the account, and provides updates when the collection or individual device state changes.

Setup

mDeviceCollection = new DeviceCollection(mAferoClient, ClientID.get(this));
mDeviceCollection.start()
    .subscribe(new Observer<DeviceCollection>() {
            @Override
            public void onNext(DeviceCollection deviceCollection) {
                // DeviceCollection is fully populated and listening for updates
            }

            @Override
            public void onCompleted() {
                // DeviceCollection is fully populated and listening for updates
            }

            @Override
            public void onError(Throwable e) {
                // may get IllegalStateException if already started
            }
        });

Adding a device to the account

public Observable addDevice(String associationId, boolean isOwnershipVerified)

deviceCollection.addDevice(associationId, false)
    .subscribe(new Action1<DeviceModel>() {
        @Override
        public void call(DeviceModel deviceModel) {
            AfLog.i("New Device Added: " + deviceModel.getName());
        }
    });

Removing a device from the account

public Observable removeDevice(DeviceModel deviceModel)

deviceCollection.removeDevice(deviceModel)
    .subscribe(new Action1<DeviceModel>() {
        @Override
        public void call(DeviceModel deviceModel) {
            AfLog.i("New Device Added: " + deviceModel.getName());
        }
    });    

Observing as devices are added to the account

deviceCollection.observeCreates()
    .subscribe(new Action1<DeviceModel>() {
        @Override
        public void call(DeviceModel deviceModel) {
            AfLog.i("Device Added: " + deviceModel.getName());
        }
    });    

Observing as devices are removed from the account

deviceCollection.observeDeletes()
    .subscribe(new Action1<DeviceModel>() {
        @Override
        public void call(DeviceModel deviceModel) {
            AfLog.i("Device Removed: " + deviceModel.getName());
        }
    });    

AferoSofthub

The AferoSofthub is a singleton that manages the soft hub functionality.

Example

mAferoSofthub = AferoSofthub.acquireInstance(this, mAferoClient, "my-unique-client-identifier");

if (!(mAferoSofthub.isRunning() || mAferoSofthub.isStarting()) {
    mAferoSofthub.start()
        .subscribe(new Observer<AferoSofthub>() {
            @Override
            public void onNext(AferoSofthub aferoSofthub) {
                // AferoSofthub is now running
            }

            @Override
            public void onCompleted() {
                // AferoSofthub is now running
            }

            @Override
            public void onError(Throwable e) {
                // may get IllegalStateException if already starting or running
            }
        });
}

DeviceWifiSetup

This class provides support for the Afero wifi setup process to help bring wifi based devices online.

Get SSID List

DeviceWifiSetup wifiSetup = new DeviceWifiSetup(deviceModel, aferoClient);
wifiSetup.start();

// fetch the wifi network list visible from the Afero device
wifiSetup.getWifiSSIDList()
    .subscribe(new Action1<WifiSSIDEntry>() {
        @Override
        public void call(WifiSSIDEntry wifiSSIDEntry) {
            AfLog.i((wifiSSIDEntry.isSecure() ? "+" : "-") + " " + wifiSSIDEntry.getSSID());
        }
    });

Send Wifi Credentials

DeviceWifiSetup wifiSetup = new DeviceWifiSetup(DeviceModel deviceModel, AferoClient aferoClient);
wifiSetup.sendWifiCredential("ssid", "password")
        .subscribe(new Observer<SetupWifiCallback.SetupWifiState>() {
            @Override
            public void onCompleted() {
                
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(SetupWifiCallback.SetupWifiState setupWifiState) {

            }
        })