[Huawei Developers] Ribhavmodi:
View attachment 5533267

Introduction

In this article, I will be integrating Huawei Site Kit in an Application.

Site Kit

This kit is used for getting the places and near-by places on keyword search. HUAWEI Site Kit provide users with convenient and secure access to diverse, place-related services.

Use Case

The Site Kit will be used to find nearby ATMs in a Banking Application which can perform basic functions such as Enter Pin, Withdraw Money, Deposit Money and Check Balance.

Requirements

1. Any operating system (MacOS, Linux and Windows).

2. Must have a Huawei phone with HMS 4.0.2.300 or later.

3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

4. Minimum API Level 21 is required.

5. Required EMUI 9.0.0 and later version devices.

Integrate HMS Dependencies

1. First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.

2. Create a project in android studio, refer Creating an Android Studio Project.

3. Generate a SHA-256 certificate fingerprint.

4. To generate SHA-256 certificate fingerprint. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code.

Or use cmd as explained in SHA256 CODE

5. Create an App in AppGallery Connect.

6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.

View attachment 5533247

7. Enter SHA-256 certificate fingerprint and click Save, as follows.

View attachment 5533251

8. Click Manage APIs tab and enable Site Kit.

9. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.

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

classpath 'com.huawei.agconnect:agcp:1.6.0.300'

10. Add the below plugin and dependencies in build.gradle(Module) file.

apply plugin: 'com.huawei.agconnect'

// Huawei AGC

implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'

// Site Kit

implementation 'com.huawei.hms:site:5.2.0.300'

11. Now Sync the gradle.

12. Add the required permission to the Manifestfile.xml file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!--check wifi state-->

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Development

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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:layout_width="wrap_content"

android:layout_height="wrap_content"

tools:context=".MainActivity"

android:id="@+id/activity_main"

android:background="@color/purple_200">

<Button

android:id="@+id/btTrans"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="90dp"

android:layout_marginTop="100dp"

android:text="@string/click_to_start_transaction"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

tools:layout_editor_absoluteY="327dp" />

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="70dp"

android:layout_marginTop="10dp"

android:text="@string/welcome_to_world_s_best_bank"

android:textSize="20dp" />

<ImageView

android:id="@+id/imageView"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:srcCompat="@drawable/hello" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="570dp"

android:layout_marginStart="120dp"

android:text="@string/find_atm"

android:textSize="18sp"

android:textStyle="bold"

android:visibility="visible" />

<EditText

android:id="@+id/search_query"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textAlignment="center"

android:hint="@string/search_atm_here"

android:inputType="text"

android:padding="5dp"

android:layout_marginTop="530dp"/>

<Button

android:id="@+id/button_text_search"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:layout_marginTop="220dp"

android:layout_marginStart="60dp"

android:text="@string/search"

android:textAllCaps="false" />

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#D3D3D3"

android:gravity="center_vertical"

android:padding="5dp"

android:text="@string/result"

android:textSize="16sp"

android:layout_marginTop="600dp"/>

<TextView

android:id="@+id/response"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textIsSelectable="true"

android:layout_marginTop="640dp"

/>

</FrameLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

private var searchService: SearchService? = null

private var resultTextView: TextView? = null

private var queryInput: EditText? = null

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

try {

searchService = SearchServiceFactory.create(

this,

URLEncoder.encode(

"CgB6e3x9Z0Sl/zgHigt2p775VippBazEDe6ujGs/R7iLuwd4Sum+m6aBgecx+gWQtkuVJu/BOp1UeLktd9cyuf66",

"utf-8"

)

)

} catch (e: UnsupportedEncodingException) {

Log.e("MainActivity", "encode apikey error")

}

queryInput = findViewById(R.id.search_query)

resultTextView = findViewById(R.id.response)

}

fun search(view: View?) {

val textSearchRequest = TextSearchRequest()

textSearchRequest.setQuery(queryInput!!.text.toString())

textSearchRequest.setHwPoiType(HwLocationType.TOWER)

searchService.textSearch(

textSearchRequest,

object : SearchResultListener<TextSearchResponse?>() {

fun onSearchResult(textSearchResponse: TextSearchResponse?) {

val response = StringBuilder("\n")

response.append("success\n")

var count = 1

var addressDetail: AddressDetail

if (null != textSearchResponse) {

if (null != textSearchResponse.getSites()) {

for (site in textSearchResponse.getSites()) {

addressDetail = site.getAddress()

response

.append(

String.format(

"[%s] name: %s, formatAddress: %s, country: %s, countryCode: %s \r\n",

"" + count++, site.getName(), site.getFormatAddress(),

if (addressDetail == null) "" else addressDetail.getCountry(),

if (addressDetail == null) "" else addressDetail.getCountryCode()

)

)

}

} else {

response.append("textSearchResponse.getSites() is null!")

}

} else {

response.append("textSearchResponse is null!")

}

Log.d("MainActivity", "search result is : $response")

resultTextView!!.text = response.toString()

}

fun onSearchError(searchStatus: SearchStatus) {

Log.e("MainActivity", "onSearchError is: " + searchStatus.getErrorCode())

}

})

}

}

Cloud Debugging

Use Cloud Debugging in HMS Toolkit to debug the app on a real device.

To use Cloud Debugging, you need to sign in using a HUAWEI ID, complete identity verification, and then authorize the sign-in.

Choose HMS > CloudDebugging.

You can use Available Devices. Select a device and click RUN.

View attachment 5533259

Result

View attachment 5533261 View attachment 5533263

Tips and Tricks

1. Set minSDK version to 24 or later, otherwise you will get AndriodManifest merge issue.

2. Make sure you have added the agconnect-services.json file to app folder.

3. Make sure you have added SHA-256 fingerprint without fail.

4. Make sure all the dependencies are added properly.

Conclusion

In this article, we have learnt integration of Site Kit in Banking application. It will guide you to show nearby ATM based on the user input.

Reference

Site Kit: Documentation