This tutorial will teach you how to create an Android app to communicate with other systems through Amazon Web Services. Using the concept of Internet of Things, you can create projects that require communication between a mobile app and hardware.
Nowadays, big companies are investing a lot of money on projects to support this concept. Learning the communication protocols to work with AWS and create IoT projects is essential if you plan to develop a career in this area or maybe just want to create a project as described in the first paragraph.
- Some experience with Android development and GitHub. The steps provided here will also have premade code which you can copy and paste into your own project.
Step 1: Installing Android Studio
To start developing Android apps, first, you will need to install Android Studio.
Step 2: Creating a New Project
Firstly, let's create an Android project.
- Open Android Studio.
- Click on "Start a new Android Studio project".
- Type a name for your application (e.g. AWSExample).
- On company domain, create an address for your package (e.g. company.com).
- Click on "Next".
- Select the platform you will develop to.
For this project, we will consider the minimum API as 19. If you want to develop for older Android versions, just select a lower API, but have in mind that some features may not be available.
- Click on "Next".
- Preferrable choose "Empty Activity".
Click on "Next".
- Click on "Finish".
Step 3: Installing AWS SDK
The platform from AWS which will be used to create the communication is AWS IoT. The following steps will guide you to download and install the SDK:
- Create an AWS account (credit card info will be required to complete the registration, but you will not be charged until you overcome the free account limits).
- To get the AWS Mobile SDK for Android, add the following dependencies to your app/build.gradle file:
compile 'com.amazonaws:aws-android-sdk-iot:2.2.+' compile 'com.amazonaws:aws-android-sdk-core:2.2.+'
- Open app/src/main/AndroidManifest.xml and add the following permission:
Step 4: Getting AWS Credentials
To develop your app using AWS, you must obtain AWS credentials with Amazon Cognito Identity, which is a credentials provider. By using it, you will be able to access the services without having to insert private credentials in your application's code.
To get AWS credentials, you must first create an identity pool. The identity pool stores user identity data specifically to your account. It has configurable IAM roles that allow you to specify which services your user will have access to. For more information, check the Step 3 of the official documentation "Set Up the AWS Mobile SDK for Android".
To create an identity pool, follow these steps:
- Log in to the Amazon Cognito Console.
- Click on "Manage Federated Identities".
- Click on "Create new identity pool".
- Insert a name for your identity pool and check the box to allow access to unauthenticated identities.
- Then, click on "Create Pool" to finish.
- Click "Allow" to create two default roles associated with your identity pool (one for authenticated users and the other one to unauthenticated users).
- To integrate Cognito Identity with your app, you should pass your credentials provider object to the constructor of the AWS client you are using. It should look like this:
CognitoCachingCredentialsProvider mCredentialsProvider = new CognitoCachingCredentialsProvider( getApplicationContext(), /* pega o Context da aplicação */ "COGNITO_IDENTITY_POOL", /* ID da Identity Pool */ Regions.MY_REGION /* Região para a sua identity pool --US_EAST_1 or EU_WEST_1*/ );
Step 5: Choose the Services According to Your Project
Now your app and your development environment are ready to use the Amazon Web Services. Read about the Amazon's available services and choose which ones fit best for your project's necessities. To help you to choose, here are some options of helpful links:
- Check the Amazon's step-by-step Getting Started Guides.
- Run the demos with the sample Android apps provided by Amazon to demonstrate common use cases. Before running any demo, check out the README file for each sample.
- Read the API reference to understand how the AWS Mobile SDK for Android works.
- If you have any questions, you can post on AWS Mobile SDK Forums or post on this page under the tutorial at the comments section.