[Huawei Developers] berkberber:
View attachment 5290935

Introduction​

With the help of Awareness Kit, we can have more details about the current situation of user’s device. Thus, we can develop more efficient applications and build different logics according to different situations.

Awareness Kit has many features for handling many different situations. If we briefly talk about its features, Awareness Kit has features such as Time Awareness, Location Awareness, Behavior Awareness, Beacon Awareness, Audio Device Status Awareness (Ambient Light Awareness, Weather Awareness) and Phone Status Awareness (Screen Status Awareness, Wi-Fi Status Awareness, Dark Mode Awareness, App Status Awareness).

In this article, I will only give examples about Wi-Fi Status Awareness feature. If you would like to learn more about other features, you can visit developer website to learn more details about them.

https://developer.huawei.com/consum...-Guides/service-introduction-0000001050031140

How to Integrate HMS Dependencies​

First of all, we need to create an app on AppGallery Connect and add related details about HMS Core to our project.If you don’t know about how to integrate HMS Core to our project, you can learn all details from following Medium article.

https://medium.com/huawei-developers/android-integrating-your-apps-with-huawei-hms-core-1f1e2a090e98

When we have added our hms core dependencies and created an app on AGC, we can implement Awareness Kit dependency.To implement Awareness Kit to our app, we need to add the following dependency to our project.

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

Currently, awareness kit version is 1.0.8.301

implementation 'com.huawei.hms:awareness:1.0.8.301'

Note: Don’t forget to enable Awareness Kit on AGC.

With Android 11, making query to other apps on the device and interacting with them has been changed. If our targetSdkVersion is 30 or later, we need to make some changes on AndroidManifest.xml file.

To make query to other apps from our app and interact with them, we need to add queries to AndroidManifest.xml file.

View attachment 5290939

Note: To work with Awareness Kit, our minSdkVersion should be equal to or higher than 24.

View attachment 5290941

Wi-Fi Status Awareness​

With Wi-Fi Status Awareness, we can detect the Wi-Fi status of the device or we can listen Wi-Fi status changes of the device.

There are two different ways to work with Awareness. Let’s examine them:

Capture: With the Capture API, we can obtain the current situation of Wi-Fi status. For example, when user has clicked the button, we can check the Wi-Fi status of the device.

Barrier: With the Barrier API, we can detect changes on Wi-Fi status. For example, when user opened Wi-Fi option or connected to any Wi-Fi point, we can detect it.

Before starting to learn how to work with Wi-Fi Status Awareness by using Capture API, I want to talk about one use case where we can use this awareness.

If I have to give use case example for Wi-Fi Status Awareness, we can use this feature on app which users need to download something. For example, music app, app market and etc.Users make download on these kind applications. Downloading huge size of musics, app and etc. on mobile network can cause problem on our bill. It is better to check that user is connected to Wi-Fi while trying to download huge size file or app. Thus, we can show a dialog to notify the user about that device is not connected to any Wi-Fi network.

Getting Permission​

Before working with Wi-Fi Status Awareness, we need to obtain accessing wi-fi state permission in our AndroidManifest.xml file.

"android.permission.ACCESS_WIFI_STATE"

View attachment 5290943

Setting Data Processing Location​

We need to set data processing location on AppGallery Connect. If we don’t set any data processing location, we will get the following error.

View attachment 5290947

To set data processing location, we can open our project and on general information tab, we can find the following settings. We need to click on Set button.

View attachment 5290949

After we have clicked on the button, following dialog will be shown. We can choose data processing location here and I will choose Germany.

View attachment 5290953

Capture​

With Capture API, we can obtain the current status of the Wi-Fi. While working with Capture API, it returns us 3 different statuses which are Connected, Enabled and Disabled.

If we think according to use case which I have mentioned above as downloading an app or files such as music, we need to check Wi-Fi status when user has been clicked on the download button.

viewBinding.downloadButton.setOnClickListener {
Awareness.getCaptureClient(this).wifiStatus
.addOnSuccessListener { wifiStatusResponse ->
when (wifiStatusResponse.wifiStatus.status) {
WifiStatus.CONNECTED -> {
startToDownload()
}
WifiStatus.ENABLED -> {
navigateToWiFiSettings()
}
WifiStatus.DISABLED -> {
navigateToWiFiSettings()
}
}
}
.addOnFailureListener {
awarenessRequestFailed()
}
}

I want to give code sample in simplest way and that’s why I only give how to work with Capture API of Wi-Fi Status Awareness feature of Awareness Kit.As I mentioned above, there are three different statuses with Capture API.

If user has connected to any Wi-Fi network, status will be ‘Connected’.

If user has enabled the Wi-Fi but hasn’t connected to any Wi-Fi network, status will be ‘Enabled’. We can navigate user to Wi-Fi settings screen or develop different logic according to our app.

If user has not enabled the Wi-Fi settings yet, status will be ‘Disabled’. We can navigate user to Wi-Fi settings screen or develop different logic according to our app.

To navigate user to Wi-Fi settings screen of the device, we can use the following method if we won’t do any other special operations according to logic of our app.

private fun navigateToWiFiSettings() {
startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
}

Tips & Tricks

Don't forget to set data processing location. If you don't set any location, we can have an error which has error code 10008

Conclusion

These features which I have explained Wi-Fi Status Awareness feature of Huawei Awareness Kit. Huawei Awareness Kit has many more Awareness features. I recommend you to examine these features too. If you have any questions, you can reach me out from "berk@berkberber.com"

References

Huawei Awareness Kit Documentation

HMS Core GitHub - Awareness Kit Demo Repository