-
Notifications
You must be signed in to change notification settings - Fork 2
AferoJavaSDK Overview
- Provides DeviceCollection, DeviceModel, AferoClient, ConclaveClient, etc.
- Required.
- Dependencies: RxJava, Jackson
- 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
- AferoSofthub: Bridge between Afero BLE-enabled devices and Afero Cloud.
- DeviceWifiSetup: Wifi setup for Afero Wifi devices.
- Optional.
- Dependencies: Android
- Basic Android compatibility support classes.
AferoClientRetrofit2 is a concrete implementation of the AferoClient interface using Retrofit2 and okhttp3.
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)
AferoClientRetrofit2.Config aferoClientConfig = new AferoClientRetrofit2.ConfigBuilder()
.oauthClientId(myOAuthClientId)
.oauthClientSecret(myOAuthClientSecret)
.build();
mAferoClient = new AferoClientRetrofit2(aferoClientConfig);
mAferoClient.setOwnerAndActiveAccountId(accountId);
mAferoClient.setToken(new AccessToken(accessToken, refreshToken));
The DeviceModel class represents the state and meta-data of a given device.
deviceModel.getUpdateObservable()
.subscribe(new Action1<DeviceModel>() {
@Override
public void call(DeviceModel deviceModel) {
// device state has changed
}
});
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());
}
});
DeviceProfile.Attribute powerAttribute = deviceModel.getAttributeById(POWER_ATTRIBUTE_ID);
AttributeValue value = deviceModel.readCurrentValue(powerAttribute);
AfLog.d("power value = " + value.toString());
The DeviceCollection manages the devices associated with the account, and provides updates when the collection or individual device state changes.
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
}
});
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());
}
});
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());
}
});
deviceCollection.observeCreates()
.subscribe(new Action1<DeviceModel>() {
@Override
public void call(DeviceModel deviceModel) {
AfLog.i("Device Added: " + deviceModel.getName());
}
});
deviceCollection.observeDeletes()
.subscribe(new Action1<DeviceModel>() {
@Override
public void call(DeviceModel deviceModel) {
AfLog.i("Device Removed: " + deviceModel.getName());
}
});
The AferoSofthub is a singleton that manages the soft hub functionality.
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
}
});
}
This class provides support for the Afero wifi setup process to help bring wifi based devices online.
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());
}
});
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) {
}
})