Beginner: How to send a message from Android smartphone to lite wearable using Wear Engine?

shikkerimath

Member
Nov 30, 2020
32
0
6
Introduction

In this article, will explain how to develop peer to peer communication between Android phone and Lite wearable. To achieve it we have to use Wear Engine library. It will give us the solution for communication between Harmony wearable and android smartphone.

1614081698812.png



Requirements

1) DevEco IDE

2) Lite wearable watch

3) Android Smart phone

4) Huawei developer account

Integration process

The integration process contains two parts. Android smart phone side and Wear app side.

Android side

Step 1: Create the android project in Android Studio.

1614081717122.png


Step 2: Generate Android signature files.

1614081731918.png


Step 3: Generate SHA -256 from the keystore generated. Please refer this link: https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#0

1614081760747.png


Step 4: Navigate to Huawei developer console. Click on Huawei ID https://developer.huawei.com/consumer/en/console#/productlist/32.

1614081778232.png


Step 5: Create new product. Add the SHA-256 as first signed certificate.

1614081792073.png


1614081799247.png


Step 6: Click Wear Engine under App services.

1614081813699.png


Step 7: Click Apply for Wear Engine, agree to the agreement, and the screen for data permission application is displayed.

1614081830561.png



Wait for the approval.

Step 8: Open the project level build gradle of your Android project.

1614081843061.png


Step 9: Navigate to buildscript > repositories and add the Maven repository configurations.

Code:
maven {url 'https://developer.huawei.com/repo/'}
Step 10: Navigate to allprojects > repositories and add the Maven repository address.

Code:
maven {url 'https://developer.huawei.com/repo/'}
1614081914453.png


Step 11: Add wear engine sdk on the build gradle.

Code:
implementation 'com.huawei.hms:wearengine:{version}'
1614081947292.png


Step 12: Add the proguard rules in proguard-rules.pro

1614081962299.png


Code:
-keepattributes *Annotation*
 -keepattributes Signature
 -keepattributes InnerClasses
 -keepattributes EnclosingMethod
 -keep class com.huawei.wearengine.**{*;}
Step 13: Add code snippet to search for available device on the MainActivity.


Code:
private void searchAvailableDevices() {
    DeviceClient deviceClient = HiWear.getDeviceClient(this);
    deviceClient.hasAvailableDevices().addOnSuccessListener(new OnSuccessListener<Boolean>() {
        @Override
        public void onSuccess(Boolean result) {
            checkPermissionGranted();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
        }
    });
}
Step 14: If the devices are available call for device permissions granted or not.


Code:
private void checkPermissionGranted() {
     AuthClient authClient = HiWear.getAuthClient(this);
     authClient.checkPermission(Permission.DEVICE_MANAGER).addOnSuccessListener(new OnSuccessListener<Boolean>() {
         @Override
         public void onSuccess(Boolean aBoolean) {
             if (!aBoolean) {
                 askPermission();
             }
         }
     }).addOnFailureListener(new OnFailureListener() {
         @Override
         public void onFailure(Exception e) {
         }
     });
 }
Step 15: If permission is not granted, ask for the permission.


Code:
private void askPermission() {
     AuthClient authClient = HiWear.getAuthClient(this);
     AuthCallback authCallback = new AuthCallback() {
         @Override
         public void onOk(Permission[] permissions) {
             if (permissions.length != 0) {
                 checkCurrentConnectedDevice();
             }
         }
 
         @Override
         public void onCancel() {
         }
     };
 
     authClient.requestPermission(authCallback, Permission.DEVICE_MANAGER)
             .addOnSuccessListener(new OnSuccessListener<Void>() {
                 @Override
                 public void onSuccess(Void successVoid) {
                 }
             })
             .addOnFailureListener(new OnFailureListener() {
                 @Override
                 public void onFailure(Exception e) {
                 }
             });
 }
Read full article

Tips & Tricks

  • Make sure you are generated the SHA - 256 fingerprint of proper keystore.
  • Follow the P2P generation steps properly.
Conclusion

In this article, we have learnt how to integrate Wear Engine library on Android application side and wearable side. Wear engine will allow us to communicate between Android application and Harmony Wear application without any barrier.

Reference

Read full article