[Huawei Developers] shikkerimath:
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.

View attachment 5230249

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.

View attachment 5230251

Step 2: Generate Android signature files.

View attachment 5230253

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

View attachment 5230255

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

View attachment 5230257

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

View attachment 5230259

View attachment 5230261

Step 6: Click Wear Engine under App services.

View attachment 5230263

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

View attachment 5230267

Wait for the approval.

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

View attachment 5230269

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

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

Step 10: Navigate to allprojects > repositories and add the Maven repository address.

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

View attachment 5230271

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

implementation 'com.huawei.hms:wearengine:{version}'

View attachment 5230273

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

View attachment 5230275

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

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.

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.

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

Harmony Official document - https://developer.harmonyos.com/en/docs/documentation/doc-guides/harmonyos-overview-0000000000011903

Wear Engine documentation - https://developer.huawei.com/consum...-Guides/service-introduction-0000001050978399

Certificate generation article - https://forums.developer.huawei.com/forumPortal/en/topic/0202465210302250053

P2P generation article - https://forums.developer.huawei.com/forumPortal/en/topic/0202466737940270075

Read full article