Android

Android SDK allows you to send/receive messages over satellite and call Somewear APIs when on cell/WiFi.

Sample Repo

https://github.com/somewear-labs/hello-space-android

Install

  1. Add Somewear's Maven Repository to your project build.gradle.

maven {
    credentials {
        username somewearArtifactsUsername
        password somewearArtifactsPassword
    }
    url "https://somewear-artifacts.appspot.com"
}
  1. Define the credentials in your home gradle directory properties, ~/.gradle/gradle.properties. Somewear will distribute these credentials to you over email.

somewearArtifactsUsername=
somewearArtifactsPassword=
  1. Add the Somewear UI SDK dependency to your app module's build.gradle.

dependencies {
    ...
    implementation ('com.somewearlabs:somewear-ui:x.x.x')
    ...
}
  1. Initialize the SDK when your application starts up. Your api key is provided to you by Somewear.

public class MyApplication extends Application {

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

        // Initialize SomewearUI
        SomewearUIProperties properties = new SomewearUIProperties(this);
        properties.apiKey = "<API_KEY>";
        SomewearUI.setup(properties);
    }
}
  1. Include our “device pill” in your UI to connect to a Somewear device.

<com.somewearlabs.somewearui.api.SomewearPillView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Send a Message

You can send text messages to Somewear users directly via phone number or email. You can also broadcast messages to your Somewear Workspace.

private SomewearRouter router = SomewearRouter.getInstance();

private void sendSmsMessage() {
    String message = "Hello from space!";
    MessagePayload payload = MessagePayload.build(message, PhoneNumber.build("916-555-1111"));
    router.send(payload);
}

Send a Data Payload

You can send arbitrary byte arrays by sending a DataPayload.

private SomewearRouter router = SomewearRouter.getInstance();

private void sendBytes() {
    String message = "Hello from space!";
    byte[] data = message.getBytes(StandardCharsets.UTF_8);
    
    DataPayload payload = DataPayload.build(data);
    router.send(payload);
}

Receive Inbound Payloads / Outbound Updates

You can subscribe to inbound payload events by subscribing to the payload observable.

private CompositeDisposable disposable = new CompositeDisposable();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    disposable.addAll(
            // Observe payload changes
            router.getPayload()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this::didReceivePayload)
    );
}

@Override
protected void onDestroy() {
    disposable.clear();
    super.onDestroy();
}

private void didReceivePayload(DevicePayload payload) {
    if (payload instanceof MessagePayload) {
        MessagePayload messagePayload = (MessagePayload) payload;
        Log.d("MyActivity", "Did receive message: content=" + messagePayload.getContent() + "; id=" + payload.getParcelId() + "; status=" + payload.getStatus() + "; isOutbound=" + payload.isOutbound());
    }
    else if (payload instanceof DataPayload) {
        DataPayload dataPayload = (DataPayload) payload;
        String base64Data = Base64.encodeToString(dataPayload.getData(), Base64.DEFAULT);
        Log.d("MyActivity", "Did receive data: content=" + base64Data + "; id=" + payload.getParcelId() + "; status=" + payload.getStatus() + "; isOutbound=" + payload.isOutbound());
    }
}

Logging

The SDK uses SLF4J-Android for logging. To enable logging, add a SLF4J compatible logging framework, such as Logback:

dependencies {
    ...
    implementation 'com.github.tony19:logback-android:2.0.1'
    ...
}

Add to src/main/assets/logback.xml:

<configuration>
    <appender name="logcat" class="ch.qos.logback.classic.android.LogcatAppender">
        <tagEncoder>
            <pattern>%logger{12}</pattern>
        </tagEncoder>
        <encoder>
            <pattern>[%-20thread] %msg</pattern>
        </encoder>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="logcat" />
    </root>
</configuration>

Last updated