[Android Software Development] Geeks Empire:
Android Wear [Phone-To-Watch][Data Transferring]

​

### Let Start Coding ###

​

There is just few different between AndroidApp on Phones & AndroidApp on Wearable.

The most important part is Optimizing User-Interface for AndroidWear & then keep in mind which API not available on AndroidWear. These APIs are not available on AndroidWear

android.webkit

android.print

android.app.backup

android.appwidget

android.hardware.usb

NOTE: Do Not set all processes of your app on SmartWatch Module. Cause there is smaller Battery, Memory& weaker Processor.

NOTE: You should use Android Studio to create AndroidWear App.

(However there is tricky way to use eclipse for androidWear But It s better to migrate to AndroidStudio ASAP.)

WHY Should You Transfer Data to AndroidWear Client?

- Beside using Wear Notification Extender to deliver info to SmartWatch you can transfer data to Watch Client of your App directly. In this way you will be able to do some modification on data before displaying to users on watches OR sharing data securely inside your app.

from Bottom to Top /Watch Client

When you want to create New Android Application you can choose AndroidWear App Module then AndroidStudio will add necessary AndroidWear Components and Gradle Config automatically.

But you can add AndroidWear Module manually in your current project.

Go to
File > New > New Module > Select AndroidWear & Next to Finish.

Then you need to add required configuration in your Host Application to Link AndroidWear Module.

Open build.gradle (Module: name-of-project) of Host Application. & Add wearProject to Dependencies

dependencies{
...
wearApp project{':NAME-OF-PROJECT'}
...
}

Then Save & Sync.

Open AndroidManifest.xml & Add Uses-Features & Uses-Permissions

<uses-feature android:name="android.hardware.type.watch" />

NOTE: If you want to set new permission that is not in Phone Module you have to set on both Phone & Watch AndroidManifest. (Check Source Code)

There other components are same But for app that want to communicate between Phone & Watch add this meta-data under Application tag

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

***
​

AndroidWear GUI Guide

There is different between phone layout design & watch. There is 3 layout.xml files for each Activity on SmartWatch.

main.xml Just Hold the View >>

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/watch_view_stub"
android:layout_width="match_parent" android:layout_height="match_parent"
app:rectLayout="@layout/rectangle_main"
app:roundLayout="@layout/round_main"
tools:context=".WearActivity" tools:deviceIds="wear"></android.support.wearable.view.WatchViewStub>

rectangle_main.xml for Rectangle SmartWatch Display that All GUI components must Add here

round_main.xml for Round SmartWatch Display that All GUI components must Add here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainWear"
android:background="@android:color/white"
tools:deviceIds="wear_square" >

<!--
You should set context for activity that handle the components and Id to call on main view.
+
All other components like Buttons, TextViews, EditTextx should call here.
-->
</RelativeLayout>

You should set context for activity that handle the components and Id to call on main view.

+

All other components like Buttons, TextViews, EditTextx should call here.

***
​

I should define an Activity & Service extends WearableListenerService for Wear Module.

The Service must have intent filter to Bind it.

<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />

Inside the listener you can handle the Received Data But in this example I just get the Intent-Filter-Action & launch the activity by getting the data path

if(messageEvent.getPath().equalsIgnoreCase("String Intent Filter Action")){
//Start Activity
}

In this example I used simple text as data so to get text data I can setup data like this

@Override
public void onMessageReceived(MessageEvent messageEvent) {
String receivedTextFromPhoneClient = messageEvent.getData().toString();
}

In case you want to launch an activity you have to setup some implementation to receive data

public class MainActivity extends Activity implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks{}

& put all necessary override functions

public void onMessageReceived( final MessageEvent messageEvent ) {}
public void onConnected(Bundle bundle) {}
public void onConnectionSuspended(int i) {}

Inside activity I must initialize the GoogleApiClient to connect to peer and get data

GoogleApiClient mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.addConnectionCallbacks( this )
.build();

if( mApiClient != null && !( mApiClient.isConnected() || mApiClient.isConnecting() ) )

mApiClient.connect();

Make Sure to release Listener when quitting the activity

@Override
protected void onStop() {
if ( mApiClient != null ) {
Wearable.MessageApi.removeListener( mApiClient, this );
if ( mApiClient.isConnected() ) {
mApiClient.disconnect();
}
}
super.onStop();
}

To get Received Data from peer you should do it inside onMessageReceived function //(Check Source Code)

@Override
public void onMessageReceived( final MessageEvent messageEvent ) {
runOnUiThread( new Runnable() {
@Override
public void run() {
if( messageEvent.getPath().equalsIgnoreCase( WEAR_MESSAGE_PATH ) ) {
mAdapter.add(new String([B]messageEvent.getData()[/B])); //Data from Phone
mAdapter.notifyDataSetChanged();
}
}
});
}

But Where is Data come from? ;)

Let Code the Phone Client to Send Data to SmartWatch.

from Bottom to Top /Phone Client

First of All Don't forget to add <meta-data/> to AndroidManifest.xml under <Application/>

Inside Activity Setup Views as you prefer to get user input & send it to watch.

In this example I set EditText & Button to get input and send data.

Like Watch Client First initialize GoogleApiClient so that I need to add implementation to GoogleApiClient.ConnectionCallbacks & its override functions.

Just Call these three functions at onCreate session.

private void initGoogleApiClient() {
mApiClient = new GoogleApiClient.Builder( this )
.addApi( Wearable.API )
.build();

mApiClient.connect();
}

private void init() {
mSendButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = mEditText.getText().toString();
if (!TextUtils.isEmpty(text)) {
sendMessage(WEAR_MESSAGE_PATH, text);
}
}
});
}

& final Send function to Watch Client

private void [COLOR=Red][B]sendMessage[/B][/COLOR]( final String path, final String text ) {
new Thread( new Runnable() {
@Override
public void run() {
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( mApiClient ).await();
for(Node node : nodes.getNodes()) {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
mApiClient, node.getId(), path, text.getBytes() ).await();
}

runOnUiThread( new Runnable() {
@Override
public void run() {
mEditText.setText( "" );
}
});
}
}).start();
}

NOTE: There is two parameters that the values & names must be same inside both Phone & Watch Client

The String of Intent-Filter-Action & Message-Path

private static final String ACTION = "/start_activity";
private static final String WEAR_MESSAGE_PATH = "/message";

NOTE: buildToolsVersion must be same on both Phone & Watch Module.

If you have AndroidWear Device Just Connect it & install it like normal app on phones. It same if you run Emulator But to use emulator you need to do few steps before

First go to Android Virtual Device (AVD) Manager & Create Emulator for both Square & Round Display Watches.

Install AndroidWear App from Google Play Store

In AndroidWear App select Emulator

Open Terminal & Go to ADB directory & type this command

adb -d forward tcp:5601 tcp:5601

On AndroidWear App Select Connect Emulator

Here is 2 Open Source Projects of Geeks Empire to Help you better for tutorial.

geTranslate

APK + Source Code 1-2-3 (Full)

Keep Note

[URL="http://xdaforums.com/attachment.php?attachmentid=3427464&d=1438637814"]APK + Source Code (Full)[/URL]

NOTE:

Don't forget to Hit Thanks

​