[Android Software Development] Geeks Empire:
Android Wear [Watch-To-Phone][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.)

. 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" />

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 >>

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"
[B] tools:context=".MainWear"[/B]
android:background="@android:color/white"
[B]tools:deviceIds="wear_square" [/B]>

[B]<!--
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.
-->[/B]

</RelativeLayout>

Now I should add functionality to GUI components in Java.Classes.

To Communicate with Phone First of all I should Initiate GoogleApiClient to Get Node (Phone) connected to watch & get it ready for data communication

GoogleApiClient client = new GoogleApiClient.Builder(context).addApi(Wearable.API).build();

new Thread(new Runnable() {
@Override
public void run() {
client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(client).await();
List<Node> nodes = result.getNodes();
if (nodes.size() > 0) {
nodeId = nodes.get(0).getId();
}
client.disconnect();
}
}).start();

Then I should define WatchViewStub Listener & then other components inside it

final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
//Here I should add all other functionalities
}
});

The proper Input Method on Android Wear is Voice Input. The main reason for this is small screens.

To Launch Voice Recognition Engine Call This Method

private static final int SPEECH_REQUEST_CODE = 0;
private void displaySpeechRecognizer() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

After This I need to get result from engine so i should define onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
//Here I should Handle Results
List<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
final String speech = results.get(0).toString();[B]
[COLOR=Red]//Here you can send this data to phone[/COLOR][/B]
}

To send data to app client on phone you can use Wearable.MessageApi.sendMessage()

if (nodeId != null) {
new Thread(new Runnable() {
@Override
public void run() {
client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
Wearable.MessageApi.sendMessage(client, nodeId, MESSAGE, null);
client.disconnect();
}
}).start();
}

. from Bottom to Top /Phone Client

On the Phone Side Open AndroidManifest.xml & Add Service to Handle Received data from Watch with specific <intent-filter/>

<service android:name=".WearReceiver" >

<intent-filter>

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

</intent-filter>

</service>

Then Create Class extends of WearableListenerService to get data and add all operation we want.

public class WearReceiver extends WearableListenerService {

@Override
public void onMessageReceived(MessageEvent messageEvent) {
String getWear = messageEvent.getPath(); [COLOR=Red][B]//Text from Watch[/B][/COLOR]
System.out.println("DEVICE >> " + messageEvent.getPath());
}
}

Done! But You Always you need to Test your App.

If you create Signed APK you can Install it on Phone & It will install on AndroidWear automatically.

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) | G Drive

Keep Note

APK + Source Code (Full)

Don't forget to Hit Thanks
​