build your own remote control. droidcon greece 2016

27
Build your own remote control

Upload: jesus-gumiel

Post on 11-Apr-2017

51 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Build your own remote control. Droidcon greece 2016

Build your own remote control

Page 2: Build your own remote control. Droidcon greece 2016

Who am I?

● Jesus Gumiel○ @jegumi

● Android developer ○ Telefonica R +D (TU Me, TU Go) ○ The Guardian○ Sky (Now TV, Sky Store)

● Entrepreneur ○ Footballtracker → http://www.football-tracker.com

Page 3: Build your own remote control. Droidcon greece 2016

Some context

● Hackday at Now TV○ Develop something for Now TV

● What can I do?○ Only 4 hours of development○ Android of course○ It must be cool to show and easy to understand○ It must be something interesting to develop○ It must be useful

Page 4: Build your own remote control. Droidcon greece 2016

The target

Control Now Tv box with a Moto 360

Page 5: Build your own remote control. Droidcon greece 2016
Page 6: Build your own remote control. Droidcon greece 2016

Types of wearables app

Synced Notifications

Notifications on handhelds can automatically sync to wearables, so design them with both devices in mind.

Voice Actions

Register your app to handle voice actions, like "Ok Google, take a note," for a hands-free experience.

Build Wearable Apps

Create custom experiences with activities, services, sensors, and much more with the Android SDK.

Send Data

Send data and actions between handhelds and wearables with data replication APIs and RPCs

Page 7: Build your own remote control. Droidcon greece 2016

Common mistake

Try to replicate the functionality of your handset on a wearable

Page 8: Build your own remote control. Droidcon greece 2016

The future – Android Wear 2.0

Standalone apps are the biggest change for the Wear ecosystem to date. Starting this autumn, you won't need your phone nearby to use apps on your Android Wear device. Rather, it will be able to communicate through Bluetooth, Wi-Fi or cellular instead of depending on a tethered phone or cloud syncing, using a Multi-APK delivery method.

2.0 won't be available for every Wear smartwatch. Older devices such as the original Moto 360 and the LG G Watch will miss out.

Page 9: Build your own remote control. Droidcon greece 2016

Debugging Bluetooth

1. Enable Debugging over Bluetooth on the Android Wear companion app. You should see a tiny status summary appear under the option:Host: disconnectedTarget: connected

2. Connect the handheld to your machine over USB and run:adb forward tcp:4444 localabstract:/adb-hubadb connect localhost:4444

3. In the Android Wear companion app, you should see the status change to:Host: connectedTarget: connected

Page 10: Build your own remote control. Droidcon greece 2016

Wearable app architecture

Page 11: Build your own remote control. Droidcon greece 2016

Create a wearable project

Page 12: Build your own remote control. Droidcon greece 2016

WEARABLE

Page 13: Build your own remote control. Droidcon greece 2016

Wearable layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?><android.support.wearable.view.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent”>

Page 14: Build your own remote control. Droidcon greece 2016

Wearable layout (activity_main.xml)

<?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/rect_activity_main" app:roundLayout="@layout/round_activity_main" tools:context=".MainActivity" tools:deviceIds="wear"/>

Page 15: Build your own remote control. Droidcon greece 2016

Wearable layout (round_activity_main.xml)

<?xml version="1.0" encoding="utf-8"?><GridLayout 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:background="@color/round_bg" android:columnCount="3” tools:context=".MainActivity" tools:deviceIds="wear_round">

Page 16: Build your own remote control. Droidcon greece 2016

Wearable layout (activity_main.xml) – Post API 23

1. layout-notround/activity_main.xml - layout for square watches

- values-round/dimens.xml

2. layout-round/activity_main.xml - layout for round watches

- values/dimens.xml

Page 17: Build your own remote control. Droidcon greece 2016

Activity

@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFields(); setUpCommunication();}

Page 18: Build your own remote control. Droidcon greece 2016

Init fields

private void initFields() { final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { @Override public void onLayoutInflated(WatchViewStub stub) {

……..} }}

Page 19: Build your own remote control. Droidcon greece 2016

Connect with the device

private void setUpCommunication() { mClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).build(); new Thread(new Runnable() { @Override public void run() { mClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS); NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(mClient).await(); List<Node> nodes = result.getNodes(); if (!nodes.isEmpty()) { mNodeId = nodes.get(0).getId(); } mClient.disconnect(); } }).start(); }

Page 20: Build your own remote control. Droidcon greece 2016

Communicate with the device

private void sendMessageToDevice(final int message) { if (mNodeId != null) { new Thread(new Runnable() { @Override public void run() { mClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS); Wearable.MessageApi.sendMessage(mClient, mNodeId, String.valueOf(message), null); mClient.disconnect(); } }).start(); } }

Page 21: Build your own remote control. Droidcon greece 2016

MOBILE

Page 22: Build your own remote control. Droidcon greece 2016

Declare the listener in the Manifest

<service android:name="com.bskyb.nowtv.ui.ListenerService" > <intent-filter> <action android:name="com.google.android.gms.wearable.BIND_LISTENER" /> </intent-filter></service>

Page 23: Build your own remote control. Droidcon greece 2016

Declare the listener in the Manifest

<service android:name="com.bskyb.nowtv.ui.ListenerService" > <intent-filter>

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

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

<data android:scheme="wear" android:host="*" android:pathPrefix="/prefix" /> </intent-filter></service>

Page 24: Build your own remote control. Droidcon greece 2016

Listener to receive messages

public class ListenerService extends WearableListenerService {

@Override public void onMessageReceived(MessageEvent messageEvent) { String command = messageEvent.getPath();

RemoteHelper.sendMessageToBox(this, command ); }

Page 25: Build your own remote control. Droidcon greece 2016

Sending command to Box

public static void sendMessageToBox(Context context, String command) { RequestQueue queue = Volley.newRequestQueue(context); StringRequest stringRequest = new StringRequest(Request.Method.POST, getRemoteUrl(context, command), new Response.Listener<String>() { @Override public void onResponse(String response) { Log.i(TAG, "onResponse: " + response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(TAG, "onErrorResponse: ", error); } }); queue.add(stringRequest); }

Page 26: Build your own remote control. Droidcon greece 2016

Packaging your app

- Permissions on the handheld manifest- Handled and wearable app with same package and version name- Add gradle dependency

dependencies { wearApp project(':wear’)

}

- Click Build > Generate Signed APK

- Android Studio exports the signed handheld app with the wearable app embedded in it automatically into your project's root folder.

Page 27: Build your own remote control. Droidcon greece 2016

https://github.com/jegumi/londroid2016

Show me the code