Develop Your Bicycle Rental App in 6 Easy Steps | Part II

Healthy Cycling App: Scan Kit, Health Kit, In-App Purchases

Feyza Ürkut
Huawei Developers
7 min readJan 24, 2023

--

Introduction

Hi everyone✨

In this series of articles, I will explain you how we can develop a Bike Rental Application in 6 easy steps using Huawei Services. In the second part of the series, I will talk about the Scan Kit and In-App Purchases, which are the key points of our application. Let’s proceed with the steps of pay transactions and starting the rental!

Content

Introducing the Healthy Cycling App

1- Add Dependencies & Permissions for Scan Kit and IAP

2- Getting a Result from an Activity

3- Start Renting Using Scan Kit with Customized View Mode

4- Using Health Kit to Record Activity and Read Calorie Data (Bonus)

5- Paying the Amount Using In-App Purchases

Conclusion

Healthy Cycling App

The Healthy Cycling is a sample application with which you can perform bicycle rental operations and see your weekly progress with some data like total distance, time, calorie etc.

With this application, I aim to encourage people to use bicycles that are healthy and environmentally friendly.

The mix of bicycle rental and exercise application. Reduce air pollution and live healthily!

Some of the HMS Kits and Services that we use in our project, which are among the capabilities provided by HUAWEI Mobile Services for efficient development, are as follows:

  • Auth Service
  • Map Kit
  • Location Kit
  • Scan Kit
  • IAP
  • Health Kit
  • Push Kit
  • App Messaging

The Android technologies we use in this project, which we are progressing with MVVM architecture, single activity multiple fragments approach and clean code principles, are as follows:

1- Add Dependencies & Permissions for Scan Kit and IAP

  1. Create an app in App Gallery Connect and integrate the App Gallery Connect SDK into your app. For details, you can follow the preparations codelab: Preparations for Integrating HUAWEI HMS Core
  2. Enable the Merchant Service then enable IAP in Manage APIs bar.

3. Add build dependencies to the dependencies block.

4. Add permissions to the AndroidManifest.xml file.

5. Apply for related permissions. In Android 6.0 and later, you need to apply for location permissions dynamically.

2- Getting a Result from an Activity

While the basic startActivityForResult() and onActivityResult() APIs can be used in the Activity class at all API levels, it is now recommended to use the Activity Result APIs introduced in AndroidX Activity and Fragment.

Activity Result Contracts

The process of using this new Activity Results API looks like this.

Photo by Wajahat Karim

First, you need to define your contract as custom or use the existing one. A contract is the implementation of the Activity Result Contracts interface. Since we are going through Scan Kit and IAP, we can use ActivityResultContracts.StartActivityForResult.

  • With the new method, Activity Result Contracts, we keep the control of each process separate by clearly defining the input parameter for the desired results.
  • In this way, we both increase the readability of the code and avoid problems caused by different types of returns, and possible NPE (NullPointerException) errors.

3- Start Renting Using Scan Kit with Customized View Mode

To start the bicycle rental process, we need to read the QR codes that will be found on the bicycles. For this case, we preferred the Scan Kit’s Customize View mode. Let’s integrate the Scan Kit into our project.

Create Customized View with an Activity

  1. Create a RemoteView and load it to the activity layout in onCreate() method. Also set the barcode formats you want Scan Kit to support.

2. Set a result callback listener for the RemoteView to obtain the scanning result object HmsScan.

3. Bind the camera preview screen to the remote view and set the scanning area in the activity.

4. Set a lifecycle listener for the RemoteView.

Launch Scan Activity for Result

  1. Create an intent for ScanActivity and start all process with launch() method. Then, catch the result in registerForActivityResult() method. Start bicycle renting with bikeId value from the scan.

Add the Flashlight Button

  1. Check whether the system feature has camera flash.

2. Call the switchLight() method of remoteView in listener and replace the button icon with the corresponding one.

Finish Renting & Go to Payment Page

4- Using Health Kit to Record Activity and Read Calorie Data

For developers and partners, Health Kit provides a data platform and activity and health open capabilities, so that they can build related apps and services based on a multitude of data types.

We used these capabilities of the Health Kit to record activity and read calorie data from this record in the application. When the user finished driving, we recorded the relevant data with the Health Kit. On the progress page, we took the calories from the saved data and tracked them weekly along with other data. If it reaches the set calorie target, we have sent a notification, you can see the picture of the page below. And you can access my previous article, where I explained the steps for recording activity in more detail.

5- Paying the Amount Using In-App Purchases

We offer a time-based pricing in the application. We determined these fees at the beginning and created them as consumables products in the console, as you can see in the screenshot.

1. When the ride is completed, we determine the ID of the related product according to the elapsed time and give it to the gotoPay() method.

2. Construct a PurchaseIntentReq object to send a createPurchaseIntent() request. Pass the product ID that has been defined and taken effect in AppGallery Connect to the PurchaseIntentReq object. If the request is successful, your app will receive a PurchaseIntentResult object, and it gives us a Status object.

3. When we got the Status object, we use it’s resolutionIntent parameter to launch the registerForActivityResult() method. After your app opens the checkout screen of HUAWEI IAP and the user completes the payment process, HUAWEI IAP will return the payment result to your app through this method. You can use the parsePurchaseResultInfoFromIntent() method to obtain the PurchaseResultInfo object that contains the result information.

via Sandbox Test

4. If the purchase is successful, obtain the purchase data In App Purchase Data and its signature data from the PurchaseResultInfo object. Use the public key allocated by AppGallery Connect to verify the signature. In addition, we disable the “Pay Amount” button in our project with updateButtonUi() method.

5. After a user pays for a purchase, your app checks whether the payment is successful based on the purchase State field in InAppPurchaseData.

After the consumable is successfully delivered and its purchaseToken is obtained, your app needs to use the consumeOwnedPurchase API to consume the product and tell the Huawei IAP server to update the delivery status of the consumable. purchaseToken is passed in the request to call the consumeOwnedPurchase API. If the consumption is successful, the IAP server will reset the product status to available for purchase.

Conclusion

In this article, I tried to explain the use of Scan Kit and IAP in the bicycle rental application. You can also take advantage of these kits for different use cases. In addition, the startActivityForResult() method, which is used to start an activity in a way that gets results, and the onActivityResult() method, which is used to get results from the started activity and perform operations, had been deprecated. Instead, I tried to explain the Activity Result APIs offered by Android to us and show examples of them in the relevant kits. Good work to everyone!

I will explain the last steps by which we will set surprise areas with the Geofence feature of the Location Kit and inform the user about surprise discounts with App Messaging when the user enters these areas, see you in the third article😊🙌

References

--

--