105mins remaining

Wear Engine

105mins remaining
Read mode

1. Introduction

Overview

HUAWEI Wear Engine provides open APIs to help developers quickly integrate the capabilities of Wear Engine into services or apps on phones and watches. By calling these APIs, apps on the phones and watches will be able to communicate and transmit data with each other. Wear Engine provides a simple way for integration in which you do not need to take any action for cross-device interactions.

What You Will Create

This codelab provides a basic app development sample. You can use Wear Engine to implement message communication and data transmission between phones and watches.

What You Will Learn

In this codelab, you will learn how to:

  • Efficiently integrate HUAWEI Wear Engine.
  • Get familiar with message communication and data transmission between phones and watches using HUAWEI Wear Engine.

2. What You Will Need

Hardware Requirements

  • A computer (desktop or laptop) for app development
  • A Huawei phone (with a USB cable) that has logged in to the Huawei Health app, used for debugging and development
  • A Huawei watch (used to receive images sent by the phone)

Software Requirements

  • Java JDK 1.8 or later
  • Android SDK
  • Wear Engine SDK
  • Android Studio V3.3.2 or later

Required Knowledge

  • Android app development basics

3. Getting Started

  1. Click Demo project to download the demo project package of the codelab.
  2. Decompress the downloaded package to a local directory, for example, D:\WearEngineCodeLab.
  3. Use Android Studio to open the decompressed demo project.

4. Demo Development

Integrating the Wear Engine SDK

  1. Configure the Maven repository of the Wear Engine in the project-level build.gradle file.
    buildscript { repositories { maven { url 'http://developer.huawei.com/repo/' } google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.5.3' } } allprojects { repositories { maven { url 'http://developer.huawei.com/repo/' } google() jcenter() } }
  2. Add the following dependency to the project-level build.gradle file in the app directory.
    implementation com.huawei.hms:wearengine:5.0.0.300'

Adding Key Codes

  1. Obtain user authorization.
    Add codes to complete the HiWear authorization in the initData() method of the WearEngineMainActivity class file.
    private void initData() { HiWear.getAuthClient(this).requestPermission(new AuthCallback() { @Override public void onOk(Permission[] permissions) { Log.d(TAG, "getAuthClient onOk"); } @Override public void onCancel() { Log.e(TAG, "getAuthClient onCancel"); } }, Permission.DEVICE_MANAGER); }
  2. Initialize the package and set the package name and fingerprint.
    Add codes to configure p2pClient in the initData() method of the WearEngineMainActivity class file. You need to set the watch app package name and fingerprint to connect to the watch app.
    private void initData() { HiWear.getAuthClient(this).requestPermission(new AuthCallback() { @Override public void onOk(Permission[] permissions) { Log.d(TAG, "getAuthClient onOk"); } @Override public void onCancel() { Log.e(TAG, "getAuthClient onCancel"); } }, Permission.DEVICE_MANAGER); p2pClient = HiWear.getP2pClient(this); deviceClient = HiWear.getDeviceClient(this); p2pClient.setPeerPkgName("com.watch.wearengine"); p2pClient.setPeerFingerPrint("*******"); }

    Fingerprint information on the watch:

    com.watch.wearengine_BC1bSGDxiTcUEkNGNztsD6sLBFgFWQm9lPl05ktD1R8Bx8SvYQNhjlIMJJGRkf8yVYNxMnwNCbxToR5xerVCp+o=
  3. Call the send method to send the file to the watch.
    Add codes to build a file message in sendFile of the WearEngineMainActivity class file and call the send method to send the file to the watch.

    public void sendFile(String sendFilePath) { File sendFile = new File(sendFilePath); Message.Builder builder = new Message.Builder(); builder.setPayload(sendFile); Message fileMessage = builder.build(); String[] fileInfo = sendFilePath.split("/"); final String filename = fileInfo[fileInfo.length - 1]; Log.e(TAG, "file name is " + filename); p2pClient.send(selectedDevice, fileMessage, new SendCallback() { @Override public void onSendResult(int resultCode) { printOperationResult(Calendar.getInstance().getTime() + SEND_MESSAGE_TO + selectedDevice.getName() + DEVICE_NAME_OF + PEER_PKG_NAME + STRING_RESULT + resultCode); } @Override public void onSendProgress(long progress) { printOperationResult(Calendar.getInstance().getTime() + SEND_MESSAGE_TO + selectedDevice.getName() + DEVICE_NAME_OF + PEER_PKG_NAME + " progress:" + progress); if (progress == 100) { sendMessageResult(filename); } } }).addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void success) { printOperationResult( SEND_MESSAGE_TO + selectedDevice.getName() + DEVICE_NAME_OF + PEER_PKG_NAME + SUCCESS); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception error) { printOperationResult( SEND_MESSAGE_TO + selectedDevice.getName() + DEVICE_NAME_OF + PEER_PKG_NAME + FAILURE); } }); }

Creating and Starting an App

After the project is synced, select your device as the deployment target, click the run icon, install and start the sample app on the device, and start the experience test.
See the running result in the figure below.

5. Congratulations

Well done. You have successfully completed this codelab and learned how to:

  • Use HUAWEI Wear Engine.
  • Call HUAWEI Wear Engine for message communication and data transmission.

6. Reference

For more information about HUAWEI Wear Engine, please visit our official website:
Wear Engine
To download the sample code, please click the button below:
Download

Code copied