[Huawei Developers] ProManojKumar:
Overview

In this article, I will create a demo app along with the integration of HMS Account & Awareness Kit which is based on Cross platform Technology Xamarin. User can easily login with Huawei Id and get the details of their city weather information. I have implemented Huawei Id for login and Weather Awareness for weather forecasting.

Account Kit Service Introduction

HMS Account Kit allows you to connect to the Huawei ecosystem using your HUAWEI ID from a range of devices, such as mobile phones, tablets, and smart screens.

It’s a simple, secure, and quick sign-in and authorization functions. Instead of entering accounts and passwords and waiting for authentication.

Complies with international standards and protocols such as OAuth2.0 and OpenID Connect, and supports two-factor authentication (password authentication and mobile number authentication) to ensure high security.

Weather Awareness Service Introduction

HMS Weather Awareness Kit allows your app with the ability to obtain contextual information including users' current time, location, behavior, audio device status, ambient light, weather, and nearby beacons. Your app can gain insight into a user's current situation more efficiently, making it possible to deliver a smarter, more considerate user experience.

Prerequisite

1. Xamarin Framework

2. Huawei phone

3. Visual Studio 2019

App Gallery Integration process

1. Sign In and Create or Choose a project on AppGallery Connect portal.

2. Add SHA-256 key.

3. Navigate to Project settings and download the configuration file.

4. Navigate to General Information, and then provide Data Storage location.

5. Navigate to Manage APIs and enable APIs which require by application.

Xamarin Account Kit Setup Process

1. Download Xamarin Plugin all the aar and zip files from below url:

https://developer.huawei.com/consum...y-V1/xamarin-sdk-download-0000001050768441-V1

2. Open the XHwid-5.03.302.sln solution in Visual Studio.

Xamarin Weather Awareness Kit Setup Process

1. Download Xamarin Plugin all the aar and zip files from below url:

https://developer.huawei.com/consum...Plugin-Library-V1/xamarin-0000001061535799-V1

2. Open the XAwarness-1.0.7.303.sln solution in Visual Studio.

3. Navigate to Solution Explore and right click on jar Add > Exsiting Item and choose aar file which download in Step 1.

4. Right click on added aar file, then choose Properties > Build Action > LibraryProjectZip

Note: Repeat Step 3 & 4 for all aar file.

5. Build the Library and make dll files.

Xamarin App Development

1. Open Visual Studio 2019 and Create A New Project.

2. Navigate to Solution Explore > Project > Assets > Add Json file.

3. Navigate to Solution Explore > Project > Add > Add New Folder.

4. Navigate to Folder(created) > Add > Add Existing and add all dll files.

5. Right-click on Properties, choose Build Action > None

6. Navigate to Solution Explore > Project > Reference > Right Click > Add References, then navigate to Browse and add all dll files from recently added folder.

7. Added reference, then click OK.

Account Kit Integration

Development Procedure

1. Call the HuaweiIdAuthParamsHelper.SetAuthorizationCode method to send an authorization request.

1

2

3

4

5	HuaweiIdAutParams mAuthParam;

mAuthParam = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DefaultAuthRequestParam)

.SetProfile()

.SetAuthorizationCode()

.CreateParams();

2. Call the GetService method of HuaweiIdAuthManager to initialize the IHuaweiIdAuthService object.

1

2	IHuaweiIdAuthService mAuthManager;

mAuthManager = HuaweiIdAuthManager.GetService(this, mAuthParam);

3. Call the IHuaweiIdAuthService.SignInIntent method to bring up the HUAWEI ID authorization & sign-in screen.

1	StartActivityForResult(mAuthManager.SignInIntent, 8888);

4. Process the result after the authorization & sign-in is complete.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19	protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)

{

base.OnActivityResult(requestCode, resultCode, data);

if (requestCode == 8888)

{

//login success

Task authHuaweiIdTask = HuaweiIdAuthManager.ParseAuthResultFromIntent(data);

if (authHuaweiIdTask.IsSuccessful)

{

AuthHuaweiId huaweiAccount = (AuthHuaweiId)authHuaweiIdTask.TaskResult();

Log.Info(TAG, "signIn get code success.");

Log.Info(TAG, "ServerAuthCode: " + huaweiAccount.AuthorizationCode);

}

else

{

Log.Info(TAG, "signIn failed: " +((ApiException)authHuaweiIdTask.Exception).StatusCode);

}

}

}

LoginActivity.cs

This activity perform all the operation regarding login with Huawei Id.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137	using Android.App;

using Android.Content;

using Android.Content.PM;

using Android.OS;

using Android.Runtime;

using Android.Support.V4.App;

using Android.Support.V4.Content;

using Android.Support.V7.App;

using Android.Util;

using Android.Views;

using Android.Widget;

using Com.Huawei.Agconnect.Config;

using Com.Huawei.Hmf.Tasks;

using Com.Huawei.Hms.Common;

using Com.Huawei.Hms.Support.Hwid;

using Com.Huawei.Hms.Support.Hwid.Request;

using Com.Huawei.Hms.Support.Hwid.Result;

using Com.Huawei.Hms.Support.Hwid.Service;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace WeatherAppDemo

{

[Activity(Label = "LoginActivity", Theme = "@style/AppTheme", MainLauncher = true)]

public class LoginActivity : AppCompatActivity

{

private static String TAG = "LoginActivity";

private HuaweiIdAuthParams mAuthParam;

public static IHuaweiIdAuthService mAuthManager;

private Button btnLoginWithHuaweiId;

protected override void OnCreate(Bundle savedInstanceState)

{

base.OnCreate(savedInstanceState);

Xamarin.Essentials.Platform.Init(this, savedInstanceState);

SetContentView(Resource.Layout.login_activity);

btnLoginWithHuaweiId = FindViewById<Button>(Resource.Id.btn_huawei_id);

btnLoginWithHuaweiId.Click += delegate

{

// Write code for Huawei id button click

mAuthParam = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DefaultAuthRequestParam)

.SetIdToken().SetEmail()

.SetAccessToken()

.CreateParams();

mAuthManager = HuaweiIdAuthManager.GetService(this, mAuthParam);

StartActivityForResult(mAuthManager.SignInIntent, 1011);

};

checkPermission(new string[] { Android.Manifest.Permission.Internet,

Android.Manifest.Permission.AccessNetworkState,

Android.Manifest.Permission.ReadSms,

Android.Manifest.Permission.ReceiveSms,

Android.Manifest.Permission.SendSms,

Android.Manifest.Permission.BroadcastSms}, 100);

}

public void checkPermission(string[] permissions, int requestCode)

{

foreach (string permission in permissions)

{

if (ContextCompat.CheckSelfPermission(this, permission) == Permission.Denied)

{

ActivityCompat.RequestPermissions(this, permissions, requestCode);

}

}

}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)

{

Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

}

protected override void AttachBaseContext(Context context)

{

base.AttachBaseContext(context);

AGConnectServicesConfig config = AGConnectServicesConfig.FromContext(context);

config.OverlayWith(new HmsLazyInputStream(context));

}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)

{

base.OnActivityResult(requestCode, resultCode, data);

if (requestCode == 1011 || requestCode == 1022)

{

//login success

Task authHuaweiIdTask = HuaweiIdAuthManager.ParseAuthResultFromIntent(data);

if (authHuaweiIdTask.IsSuccessful)

{

AuthHuaweiId huaweiAccount = (AuthHuaweiId)authHuaweiIdTask.TaskResult();

Log.Info(TAG, "signIn get code success.");

Log.Info(TAG, "ServerAuthCode: " + huaweiAccount.AuthorizationCode);

Toast.MakeText(Android.App.Application.Context, "SignIn Success", ToastLength.Short).Show();

navigateToHomeScreen(huaweiAccount);

}

else

{

Log.Info(TAG, "signIn failed: " + ((ApiException)authHuaweiIdTask.Exception).StatusCode);

Toast.MakeText(Android.App.Application.Context, ((ApiException)authHuaweiIdTask.Exception).StatusCode.ToString(), ToastLength.Short).Show();

Toast.MakeText(Android.App.Application.Context, "SignIn Failed", ToastLength.Short).Show();

}

}

}

private void showLogoutButton()

{

/*logout.Visibility = Android.Views.ViewStates.Visible;*/

}

private void hideLogoutButton()

{

/*logout.Visibility = Android.Views.ViewStates.Gone;*/

}

private void navigateToHomeScreen(AuthHuaweiId data)

{

Intent intent = new Intent(this, typeof(MainActivity));

intent.PutExtra("name", data.DisplayName.ToString());

intent.PutExtra("email", data.Email.ToString());

intent.PutExtra("image", data.PhotoUriString.ToString());

StartActivity(intent);

Finish();

}

}

}

Weather Awareness API Integration

Assigning Permissions in the Manifest File

Before calling the weather awareness capability, assign required permissions in the manifest file.

1

2	<!-- Location permission. This permission is sensitive and needs to be dynamically applied for in the code after being declared. -->

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

Developing Capabilities

Call the weather capability API through the Capture Client object.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24	private async void GetWeatherStatus()

{

var weatherTask = Awareness.GetCaptureClient(this).GetWeatherByDeviceAsync();

await weatherTask;

if (weatherTask.IsCompleted && weatherTask.Result != null)

{

IWeatherStatus weatherStatus = weatherTask.Result.WeatherStatus;

WeatherSituation weatherSituation = weatherStatus.WeatherSituation;

Situation situation = weatherSituation.Situation;

string result = $"City:{weatherSituation.City.Name}\n";

result += $"Weather id is {situation.WeatherId}\n";

result += $"CN Weather id is {situation.CnWeatherId}\n";

result += $"Temperature is {situation.TemperatureC}Celcius";

result += $",{situation.TemperatureF}Farenheit\n";

result += $"Wind speed is {situation.WindSpeed}km/h\n";

result += $"Wind direction is {situation.WindDir}\n";

result += $"Humidity is {situation.Humidity}%";

}

else

{

var exception = weatherTask.Exception;

string errorMessage = $"{AwarenessStatusCodes.GetMessage(exception.GetStatusCode())}: {exception.Message}";

}

}

MainActivity.cs

This activity perform all the operation regarding Weather Awareness api like current city weather and other information.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135	using System;

using Android;

using Android.App;

using Android.OS;

using Android.Runtime;

using Android.Support.Design.Widget;

using Android.Support.V4.View;

using Android.Support.V4.Widget;

using Android.Support.V7.App;

using Android.Views;

using Com.Huawei.Hms.Kit.Awareness;

using Com.Huawei.Hms.Kit.Awareness.Status;

using Com.Huawei.Hms.Kit.Awareness.Status.Weather;

namespace WeatherAppDemo

{

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar")]

public class MainActivity : AppCompatActivity, NavigationView.IOnNavigationItemSelectedListener

{

protected override void OnCreate(Bundle savedInstanceState)

{

base.OnCreate(savedInstanceState);

Xamarin.Essentials.Platform.Init(this, savedInstanceState);

SetContentView(Resource.Layout.activity_main);

Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

SetSupportActionBar(toolbar);

DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);

drawer.AddDrawerListener(toggle);

toggle.SyncState();

NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);

navigationView.SetNavigationItemSelectedListener(this);

}

private async void GetWeatherStatus()

{

var weatherTask = Awareness.GetCaptureClient(this).GetWeatherByDeviceAsync();

await weatherTask;

if (weatherTask.IsCompleted && weatherTask.Result != null)

{

IWeatherStatus weatherStatus = weatherTask.Result.WeatherStatus;

WeatherSituation weatherSituation = weatherStatus.WeatherSituation;

Situation situation = weatherSituation.Situation;

string result = $"City:{weatherSituation.City.Name}\n";

result += $"Weather id is {situation.WeatherId}\n";

result += $"CN Weather id is {situation.CnWeatherId}\n";

result += $"Temperature is {situation.TemperatureC}Celcius";

result += $",{situation.TemperatureF}Farenheit\n";

result += $"Wind speed is {situation.WindSpeed}km/h\n";

result += $"Wind direction is {situation.WindDir}\n";

result += $"Humidity is {situation.Humidity}%";

}

else

{

var exception = weatherTask.Exception;

string errorMessage = $"{AwarenessStatusCodes.GetMessage(exception.GetStatusCode())}: {exception.Message}";

}

}

public override void OnBackPressed()

{

DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);

if(drawer.IsDrawerOpen(GravityCompat.Start))

{

drawer.CloseDrawer(GravityCompat.Start);

}

else

{

base.OnBackPressed();

}

}

public override bool OnCreateOptionsMenu(IMenu menu)

{

MenuInflater.Inflate(Resource.Menu.menu_main, menu);

return true;

}

public override bool OnOptionsItemSelected(IMenuItem item)

{

int id = item.ItemId;

if (id == Resource.Id.action_settings)

{

return true;

}

return base.OnOptionsItemSelected(item);

}

public bool OnNavigationItemSelected(IMenuItem item)

{

int id = item.ItemId;

if (id == Resource.Id.nav_camera)

{

// Handle the camera action

}

else if (id == Resource.Id.nav_gallery)

{

}

else if (id == Resource.Id.nav_slideshow)

{

}

else if (id == Resource.Id.nav_manage)

{

}

else if (id == Resource.Id.nav_share)

{

}

else if (id == Resource.Id.nav_send)

{

}

DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);

drawer.CloseDrawer(GravityCompat.Start);

return true;

}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)

{

Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

}

}

}

Xamarin App Build Result

1. Navigate to Solution Explore > Project > Right Click > Archive/View Archive to generate SHA-256 for build release and Click on Distribute.

2. Choose Distribution Channel > Ad Hoc to sign apk.

3. Choose Demo keystore to release apk.

4. Build succeed and Save apk file.

5. Finally here is the result.

Tips and Tricks

1. Awareness Kit supports wearable Android devices, but HUAWEI HMS Core 4.0 is not deployed on devices other than mobile phones. Therefore, wearable devices are not supported currently.

2. Cloud capabilities are required to sense time information and weather.

3. 10012: HMS Core does not have the behaviour recognition permission.

Conclusion

In this article, we have learned how to integrate HMS Weather Awareness and Account Kit in Xamarin based Android application. User can easily login and check weather forecast.

Thanks for reading this article.

Be sure to like and comments to this article, if you found it helpful. It means a lot to me.

References

https://developer.huawei.com/consum...lugin-Guides/sign-in-idtoken-0000001051086088

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