build your own remote control. droidcon greece 2016

Post on 11-Apr-2017

51 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Build your own remote control

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

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

The target

Control Now Tv box with a Moto 360

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

Common mistake

Try to replicate the functionality of your handset on a wearable

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.

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

Wearable app architecture

Create a wearable project

WEARABLE

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

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

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

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

Activity

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

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

……..} }}

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(); }

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(); } }

MOBILE

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>

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>

Listener to receive messages

public class ListenerService extends WearableListenerService {

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

RemoteHelper.sendMessageToBox(this, command ); }

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); }

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.

https://github.com/jegumi/londroid2016

Show me the code

top related