Find Nearby ATMS with integration of Huawei Site Kit in Banking app (Kotlin)
Tags: android, kotlin, huawei, developer
Published: 2022-02-10 06:45:51



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u00zyrupm9gqdzy420qv.jpg)
## **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**
- First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a [Huawei ID](https://developer.huawei.com/consumer/en/doc/start/registration-and-verification-0000001053628148).
- Create a project in android studio, refer [Creating an Android Studio Project](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#1).
- Generate a SHA-256 certificate fingerprint.
- To generate SHA-256 certificate fingerprint. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code.
Or use cmd as explained in [SHA256 CODE](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#3)
- [Create an App in AppGallery Connect](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#0).
- Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ys1byc6gof3la05l0jdk.png)
- Enter SHA-256 certificate fingerprint and click Save, as follows.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3wpvccccj01xc18t83vq.png)
- Click Manage APIs tab and enable Site Kit.
- Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer [Add Configuration](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#6).

```
maven { url 'http://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.6.0.300'

```
-  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'```

- Now Sync the gradle.

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

Result

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xt7r2cpwfxq1b207kp14.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4086j8wts1klms4fcbu.png)
   
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](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-sdk-about-the-service-0000001076188604)