Courtesy: GitHub

WclDemoSample

This is a sample app that shows how one can set up an application to use the WearCompanionLibrary. It has both a phone and a wear component. On your mobile, select a page from the drawer and there, you can open the corresponding page on your wear device by pressing on the FAB button at the lower right corner of the page. On your wear device, you can select an item form the list when you run the app. Features shown here include exchanging data, launching remote app, making HTTP calls from the wear device, showing variations of WearableListView activities and recording voice on the wear and streaming that to the mobile device and playing it there in real-time. In order for each feature to work properly, you have to be on that feature's page on both the mobile app and the wear app at the same time.

Dependencies

  • WearCompanionLibrary (WCL)
  • The phone module also depends on the design support library.
  • Note: if you follow the instructions below, the WCL library will bring in other dependencies that are needed. If you choose to use WCL in a different way (for example using an archive version of that library), then you may need to include the dependencies that are listed for WCL as well.

Setup

  • Checkout this project as WclDemoSample:
    $ git clone https://github.com/googlesamples/android-WclDemoSample.git WclDemoSample
  • Checkout WCL as a sibling to this project under the name WearCompanionLibrary:
    $ git clone https://github.com/googlesamples/android-WearCompanionLibrary.git WearCompanionLibrary
  • To make sure all is working, compile WCL:
    $ cd WearCompanionLibrary && ./gradlew build
  • Compile this project:
    $ cd ../WclDemoSample && ./gradlew build
  • To open the project in Android Studio, you may need to first open the WCL project in Android Studio to build the required "*.iml" file for Android Studio. To do this, start Android Studio and select "Open An Existing Android Studio Project" and navigate to WearCompanionLibrary directory and select the build.gradle in the root of that project. This should create the needed files.
  • Close Android Studio and open that again and follow the same steps but this time navigate to WclDemoSample project and select build.gradle there.

References and How to report bugs

  • WCL has a documentation (in PDF format) in its project. This code has comments that along with the WCL's documentation should be adequate to guide you through the sample.
  • If you find any issues with this sample, please open a bug here on GitHub; if you find any issue with WCL or have a feature request, please open a ticket on that repository.

How to make contributions?

Please read and follow the steps in the CONTRIBUTING

License

See LICENSE

Change List

1.0

  • Initial release

compileSdkVersion 23

minSdkVersion 17

targetSdkVersion 23

versionCode 1

versionName "1.0"

compile fileTree(dir: 'libs', include: ['*.jar'])

compileSdkVersion 23

applicationId "com.example.android.wearable.wcldemo"

minSdkVersion 17

targetSdkVersion 23

versionCode 1

versionName "1.0"

compile fileTree(include: ['*.jar'], dir: 'libs')

compile project(':..:WearCompanionLibrary:WCL')

compile 'com.android.support:design:23.1.+'

compile project(':shared')

compileSdkVersion 23

applicationId "com.example.android.wearable.wcldemo"

minSdkVersion 21

targetSdkVersion 23

versionCode 1

versionName "1.0"

compile fileTree(include: ['*.jar'], dir: 'libs')

compile project(':..:WearCompanionLibrary:WCL')

compile project(':shared')

package com.example.android.wearable.wcldemo.common

package com.example.android.wearable.wcldemo

uses-permission

  • android.permission.INTERNET
  • android.permission.ACCESS_NETWORK_STATE
  • android.permission.ACCESS_WIFI_STATE

application .MobileApplication

package com.example.android.wearable.wcldemo

uses-permission

  • android.permission.WAKE_LOCK
  • android.permission.INTERNET
  • android.permission.ACCESS_NETWORK_STATE
  • android.permission.ACCESS_WIFI_STATE
  • android.permission.RECORD_AUDIO

application .WearApplication

MyListActivity

FileTransferActivity

WclWearableListViewActivity

StockActivity

DataExchangeActivity

StreamingVoiceActivity

WclRecorderActivity

MyListActivity

FileTransferActivity

WclWearableListViewActivity

StockActivity

DataExchangeActivity

StreamingVoiceActivity

WclRecorderActivity

MyListActivity

The entry point to the application. This activity also serves as a demo of the {@link AbstractWearableListViewActivity}; clients need to only implement two methods in this class to have a fully functional ListView. In this activity, we also demo a different way to show a WearableListView by calling {@link WearManager#showWearableList(Activity, WearableListConfig)}; that approach is useful when you want to present a list to the user and close the list after user makes a selection; the result can be captured by {@link Activity#onActivityResult(int, int, Intent)} as shown here. * @see AbstractWearableListViewActivity

This method shows how you can open a full screen list of options for user to make a selection. The result of that selection will be captured by the {@code onActivityResult} below.

Opens the launcher activity of the companion app on the mobile device. We identify the mobile device by the the capability that it provides: {@link #MOBILE_APP_CAPABILITY}.

FileTransferActivity

An activity that shows two different approaches to transferring files between wear nodes. *

In the first approach (called high-level on the UI), we have a text file that we want to transfer across. We use {@link WearFileTransfer#startTransfer()} method to accomplish this. *

The second approach, called low-level, transfers an image from the wear device (as a "raw" asset) to the phone and we use {@link WearFileTransfer} apis to open a channel between the two devices first and then open an {@link OutputStream} on the wear device and an {@link InputStream} on the phone (through the channel). Then we simply read chunks of bytes from the image and write to the {@code OutputStream} opened on the wear and read the transferred bytes from the {@code InputStream} on the mobile. This approach is more useful for real-time communication to transfer bytes as they become available (for example as the microphone on the watch is recording a voice message). In this sample, we show a progress bar on the wear as we transfer bytes of the image to the phone. *

A lot of the complexity involved in the orchestration of this approach is done in the library and is hidden from the developer; developers only request an output stream on one end and will be notified when the channel is established and when an output stream is available on the sender side, and will receive a similar callback on the other end when an input stream is available.

A listener that is called when we have a channel open and an {@code OutputStream} ready

Called on a non-UI thread

Copies a file from the assets directory to the internal application's file storage so we can get a hold of it as a {@link File} object. It returns a {@link File} reference to the file.

WclWearableListViewActivity

StockActivity

A simple activity that makes an HTTP request to get the GOOGLE stock price and if successful, presents the result to the user. The HTTP request is made using the {@link WearHttpHelper} helper class.

DataExchangeActivity

A simple activity that shows the exchange of data between the phone and wear app. When this activity is in front on the wear device, user will see messages as the user navigates between different pages of the companion phone application.

Mapping the id of page to its name and displaying the name

StreamingVoiceActivity

*

Checks the permission that this app needs and if it has not been granted, it will prompt the user to grant it, otherwise it shuts down the app.

WclRecorderActivity

WclWearableListenerService

WclWearableListenerService

WclWearableListenerService