Top Banner
Kick-Start Your Experience with Android Wear +MarioViviani Android GDE @mariuxtheone
59

Kick start your experience with android wear - Codemotion Rome 2015

Jul 15, 2015

Download

Documents

Codemotion
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Kick start your experience with android wear - Codemotion Rome 2015

Kick-Start Your Experiencewith Android Wear

+MarioVivianiAndroid GDE@mariuxtheone

Page 2: Kick start your experience with android wear - Codemotion Rome 2015

Mario VivianiFounder & CEO Mariux AppsAndroid GDE

Page 3: Kick start your experience with android wear - Codemotion Rome 2015

Photo source info here

Page 4: Kick start your experience with android wear - Codemotion Rome 2015

ANDROIDEVERYWHERE

Page 5: Kick start your experience with android wear - Codemotion Rome 2015

ANDROIDEVERYWEAR

Page 6: Kick start your experience with android wear - Codemotion Rome 2015
Page 7: Kick start your experience with android wear - Codemotion Rome 2015
Page 8: Kick start your experience with android wear - Codemotion Rome 2015

Project Setup

Page 9: Kick start your experience with android wear - Codemotion Rome 2015

Use Android Studio!Android Studio is optimized for Wear

http://goo.gl/ZSJH5O

Page 10: Kick start your experience with android wear - Codemotion Rome 2015

Android Wear Project Wizard

Page 11: Kick start your experience with android wear - Codemotion Rome 2015

Android Wear Project Wizard

Page 12: Kick start your experience with android wear - Codemotion Rome 2015

GDE

WatchViewStub

<android.support.wearable.view.WatchViewStub

...

app:rectLayout="@layout/rect_activity_wear"

app:roundLayout="@layout/round_activity_wear"

...>

</android.support.wearable.view.WatchViewStub>

XML

Page 13: Kick start your experience with android wear - Codemotion Rome 2015

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_wear);

final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);

stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

@Override

public void onLayoutInflated(WatchViewStub stub) {

mTextView = (TextView) stub.findViewById(R.id.text);

}

});

WearActivity.java

Page 14: Kick start your experience with android wear - Codemotion Rome 2015

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_wear);

final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);

stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

@Override

public void onLayoutInflated(WatchViewStub stub) {

mTextView = (TextView) stub.findViewById(R.id.text);

}

});

WearActivity.java

Page 15: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Project Structure

Page 16: Kick start your experience with android wear - Codemotion Rome 2015

GDE

MobileModule

WearModule?

Page 17: Kick start your experience with android wear - Codemotion Rome 2015

Establish Connection

Page 18: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Google Play Services

Setup Google Play Services developer.android.com/google/play-services/setup.html

Page 19: Kick start your experience with android wear - Codemotion Rome 2015

GDE

GoogleApiClient

mGoogleApiClient = new GoogleApiClient.Builder(context)

.addApi(Wearable.API)

.addConnectionCallbacks(this)

.addOnConnectionFailedListener(this)

.build();

Page 20: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Establish Connection

@Override

protected void onStart() {

super.onStart();

mGoogleApiClient.connect();

}

@Override

protected void onStop() {

super.onStop();

mGoogleApiClient.disconnect();

}

In Activity

Page 21: Kick start your experience with android wear - Codemotion Rome 2015

GDE

MobileModule

WearModule

GoogleApiClient

GoogleApiClient

Page 22: Kick start your experience with android wear - Codemotion Rome 2015

GDE

MobileModule

WearModule

GoogleApiClient

GoogleApiClient

Page 23: Kick start your experience with android wear - Codemotion Rome 2015

Send & Receive Data

Page 24: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Message

DataItem

Page 25: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Data Layer Interfaces

static interface DataListener {

void onDataChanged(DataEventBuffer dataEvents);

}

static interface MessageListener {

void onMessageReceived(MessageEvent messageEvent);

}

static interface NodeListener {

void onPeerConnected(Node node);

void onPeerDisconnected(Node node);

}

Page 26: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Add Listeners in onConnected() (Activity Only)

@Override

public void onConnected(Bundle bundle) {

Wearable.DataApi.addListener(mGoogleApiClient, this);

Wearable.MessageApi.addListener(mGoogleApiClient, this);

Wearable.NodeApi.addListener(mGoogleApiClient, this);

}

Page 27: Kick start your experience with android wear - Codemotion Rome 2015

GDE

DataItem / Message

PathString “/path/to/item”

Payload Byte[ ] <=100KB

Page 28: Kick start your experience with android wear - Codemotion Rome 2015

GDE

MobileModule

WearModule

DataItem DataItem

Page 29: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Sync Data using DataMap

PutDataMapRequest dataMap = PutDataMapRequest.create("/path");

dataMap.getDataMap().putString("/itemID", "Hello, World");

PutDataRequest request = dataMap.asPutDataRequest();

PendingResult<DataApi.DataItemResult> pendingResult =

Wearable.DataApi.putDataItem(mGoogleApiClient, request);

Page 30: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Sync Assets (Images)

Asset asset = createAssetFromBitmap(bitmap);

PutDataMapRequest dataMap = PutDataMapRequest.

create("/image");

dataMap.getDataMap()

.putAsset("profileImage", asset)

Page 31: Kick start your experience with android wear - Codemotion Rome 2015

GDE

React to Data Sync

@Override

public void onDataChanged(DataEventBuffer dataEvents) {

for (DataEvent event : dataEvents) {

if (event.getType() == DataEvent.TYPE_DELETED) {

...

} else if (event.getType() == DataEvent.TYPE_CHANGED) {

...

}

}

}

Page 32: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Retrieve the DataMap

@Override

public void onDataChanged(DataEventBuffer dataEvents) {

...

for (DataEvent event : events) {

if (event.getType() == DataEvent.TYPE_CHANGED) {

DataMapItem dataMapItem =

DataMapItem.fromDataItem(event.getDataItem());

DataMap dataMap = dataMapItem.getDataMap();

String s = dataMap.getString("/itemID");

}

Page 33: Kick start your experience with android wear - Codemotion Rome 2015

GDE

MobileModule

WearModule

Message Message

Message Message

Page 34: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Send Message using MessageAPI

SendMessageResult result = Wearable.MessageApi.sendMessage(

mGoogleApiClient, node, "/path/startactivity", payload).await();

if (!result.getStatus().isSuccess()) {

Log.e(TAG, "ERROR: failed to send Message: " +

result.getStatus());

}

Page 35: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Get Connected Nodes

private Collection<String> getNodes() {

HashSet<String> results = new HashSet<String>();

NodeApi.GetConnectedNodesResult nodes =

Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();

for (Node node : nodes.getNodes()) {

results.add(node.getId());

}

return results;

}

Page 36: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Receiving Message

@Override

public void onMessageReceived(MessageEvent messageEvent) {

if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {

Intent startIntent = new Intent(this, MainActivity.class);

startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(startIntent);

}

}

Page 37: Kick start your experience with android wear - Codemotion Rome 2015

Handling Data Layer Events - App Structure

Page 38: Kick start your experience with android wear - Codemotion Rome 2015

GDE

MobileModule

WearModule

Page 39: Kick start your experience with android wear - Codemotion Rome 2015

GDE

WearModule

MobileModule

ForegroundActivity

ForegroundActivity

Page 40: Kick start your experience with android wear - Codemotion Rome 2015

GDE

ForegroundActivity onDataChanged()

onMessageReceived()

onPeerConnected()

onPeerDisconnected()

GoogleApiClient

Page 41: Kick start your experience with android wear - Codemotion Rome 2015

GDE

WearModule

MobileModule

ForegroundActivity

ForegroundActivity

Page 42: Kick start your experience with android wear - Codemotion Rome 2015

GDE

WearModule

MobileModule

ForegroundActivity

ForegroundActivity

WearableListenerService

WearableListenerService

Page 43: Kick start your experience with android wear - Codemotion Rome 2015

GDE

WearableListenerService

onDataChanged()

onMessageReceived()

onPeerConnected()

onPeerDisconnected()

GoogleApiClient

Page 44: Kick start your experience with android wear - Codemotion Rome 2015

Example

Page 45: Kick start your experience with android wear - Codemotion Rome 2015

GDE

WearModule

MobileModule

ForegroundActivity

ForegroundActivity

WearableListenerService

Page 46: Kick start your experience with android wear - Codemotion Rome 2015

MobileSendMessageActivity extends Activity {

...

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mGoogleApiClient = new GoogleApiClient.Builder(this)

.addApi(Wearable.API)

.build();

}

MobileSendMessageActivity.java

Page 47: Kick start your experience with android wear - Codemotion Rome 2015

...

@Override

protected void onStart() {

super.onStart();

mGoogleApiClient.connect();

}

@Override

protected void onStop() {

super.onStop();

mGoogleApiClient.disconnect();

}

MobileSendMessageActivity.java

Page 48: Kick start your experience with android wear - Codemotion Rome 2015

...

private static final String START_ACTIVITY_PATH = "/start-activity";

public void sendMessageButtonClicked(View v){

Collection<String> nodes = getNodes();

for (String node : nodes) {

SendMessageResult result = Wearable.MessageApi.sendMessage(

mGoogleApiClient, node, START_ACTIVITY_PATH , null).await();

if (!result.getStatus().isSuccess()) {...}

}

}

}

MobileSendMessageActivity.java

Page 49: Kick start your experience with android wear - Codemotion Rome 2015

public class StartWearActivityService extends WearableListenerService {

...

@Override

public void onCreate() {

super.onCreate();

mGoogleApiClient = new GoogleApiClient.Builder(this)

.addApi(Wearable.API)

.build();

mGoogleApiClient.connect();

}

...

StartWearActivityService.java

Page 50: Kick start your experience with android wear - Codemotion Rome 2015

private static final String START_ACTIVITY_PATH = "/start-activity";

...

@Override

public void onMessageReceived(MessageEvent messageEvent) {

if (messageEvent.getPath().equals(START_ACTIVITY_PATH)) {

Intent startIntent = new Intent(this, WearActivity.class);

startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(startIntent);

}

}

}

StartWearActivityService.java

Page 51: Kick start your experience with android wear - Codemotion Rome 2015

GDE

Add Service to Android Manifest

<service android:name=".StartWearActivityService">

<intent-filter>

<action

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

</intent-filter>

</service>

Page 52: Kick start your experience with android wear - Codemotion Rome 2015
Page 54: Kick start your experience with android wear - Codemotion Rome 2015

TeleportData Sync and Messaging Libraryfor Android Wear

Githubgithub.com/Mariuxtheone/Teleport

Gradle - Mavencompile 'Teleport:teleportlib:0.1.3'

Page 55: Kick start your experience with android wear - Codemotion Rome 2015

GDE

TeleportClient

TeleportClient mTeleportClient = new TeleportClient(this);

mTeleportClient.connect();

Set Up:

Send Message

mTeleportClient.sendMessage("startActivity", null);

Sync Data

mTeleportClient.syncInt("myInt", 12345)

Page 56: Kick start your experience with android wear - Codemotion Rome 2015

GDE

TeleportService

setOnGetMessageTask(new TeleportService.OnGetMessageTask {

@Override

protected void onPostExecute(String path) {

if (path.equals("startActivity")){

Intent startIntent = new Intent(this, WearActivity.class);

startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(startIntent);

};}

Retrieve a Message

Page 57: Kick start your experience with android wear - Codemotion Rome 2015

Photo source info here

Page 58: Kick start your experience with android wear - Codemotion Rome 2015
Page 59: Kick start your experience with android wear - Codemotion Rome 2015

+MarioVivianiAndroid GDE

[email protected]@mariuxtheone

Thanks! Q&A

Githubgithub.com/Mariuxtheone

Feedbackbit.ly/wearcodemotion