├── .github ├── ISSUE_TEMPLATE │ └── bug_report.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── DEPRECATED.txt ├── LICENSE ├── NOTICE ├── README.md ├── amazonfreertossdk ├── .gitignore ├── build.gradle ├── gradle.properties ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── software │ │ └── amazon │ │ └── freertos │ │ └── amazonfreertossdk │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── software │ │ │ └── amazon │ │ │ └── freertos │ │ │ └── amazonfreertossdk │ │ │ ├── AmazonFreeRTOSConstants.java │ │ │ ├── AmazonFreeRTOSDevice.java │ │ │ ├── AmazonFreeRTOSManager.java │ │ │ ├── BleCommand.java │ │ │ ├── BleConnectionStatusCallback.java │ │ │ ├── BleScanResultCallback.java │ │ │ ├── DeviceInfoCallback.java │ │ │ ├── MessageType.java │ │ │ ├── NetworkConfigCallback.java │ │ │ ├── deviceinfo │ │ │ ├── BrokerEndpoint.java │ │ │ ├── Mtu.java │ │ │ └── Version.java │ │ │ ├── mqttproxy │ │ │ ├── Connack.java │ │ │ ├── Connect.java │ │ │ ├── MqttProxyControl.java │ │ │ ├── PingReq.java │ │ │ ├── PingResp.java │ │ │ ├── Puback.java │ │ │ ├── Publish.java │ │ │ ├── Suback.java │ │ │ ├── Subscribe.java │ │ │ ├── Unsuback.java │ │ │ └── Unsubscribe.java │ │ │ └── networkconfig │ │ │ ├── DeleteNetworkReq.java │ │ │ ├── DeleteNetworkResp.java │ │ │ ├── EditNetworkReq.java │ │ │ ├── EditNetworkResp.java │ │ │ ├── ListNetworkReq.java │ │ │ ├── ListNetworkResp.java │ │ │ ├── SaveNetworkReq.java │ │ │ └── SaveNetworkResp.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── software │ └── amazon │ └── freertos │ └── amazonfreertossdk │ └── ExampleUnitTest.java ├── app ├── .gitignore ├── README.md ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── software │ │ └── amazon │ │ └── freertos │ │ └── demo │ │ ├── AmazonFreeRTOSAgent.java │ │ ├── AuthenticatorActivity.java │ │ ├── BleDevice.java │ │ ├── DemoConstants.java │ │ ├── DeviceScanActivity.java │ │ ├── DeviceScanFragment.java │ │ ├── SingleFragmentActivity.java │ │ ├── WifiCredentialFragment.java │ │ ├── WifiInfo.java │ │ ├── WifiProvisionActivity.java │ │ └── WifiProvisionFragment.java │ └── res │ ├── drawable-hdpi │ ├── ic_delete.png │ └── ic_menu_logout.png │ ├── drawable-mdpi │ ├── ic_delete.png │ └── ic_menu_logout.png │ ├── drawable-xhdpi │ ├── ic_delete.png │ └── ic_menu_logout.png │ ├── drawable-xxhdpi │ ├── ic_delete.png │ └── ic_menu_logout.png │ ├── drawable-xxxhdpi │ ├── ic_delete.png │ └── ic_menu_logout.png │ ├── layout │ ├── activity_fragment.xml │ ├── fragment_device_scan.xml │ ├── fragment_mqtt_proxy.xml │ ├── fragment_wifi_provision.xml │ ├── list_device.xml │ ├── list_wifi_info.xml │ └── wifi_credential_dialog.xml │ ├── menu │ ├── fragment_device_scan.xml │ └── options_menu.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── raw │ ├── awsconfiguration.json │ └── replace_with_keystore.bks │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── docs ├── allclasses-frame.html ├── allclasses-noframe.html ├── constant-values.html ├── deprecated-list.html ├── help-doc.html ├── index-all.html ├── index.html ├── overview-frame.html ├── overview-summary.html ├── overview-tree.html ├── package-list ├── script.js ├── software │ └── amazon │ │ └── freertos │ │ └── amazonfreertossdk │ │ ├── AmazonFreeRTOSConstants.AmazonFreeRTOSError.html │ │ ├── AmazonFreeRTOSConstants.BleConnectionState.html │ │ ├── AmazonFreeRTOSConstants.MqttConnectionState.html │ │ ├── AmazonFreeRTOSConstants.html │ │ ├── AmazonFreeRTOSDevice.html │ │ ├── AmazonFreeRTOSManager.html │ │ ├── BleCommand.html │ │ ├── BleConnectionStatusCallback.html │ │ ├── BleScanResultCallback.html │ │ ├── DeviceInfoCallback.html │ │ ├── MessageType.html │ │ ├── NetworkConfigCallback.html │ │ ├── deviceinfo │ │ ├── BrokerEndpoint.html │ │ ├── Mtu.html │ │ ├── Version.html │ │ ├── package-frame.html │ │ ├── package-summary.html │ │ └── package-tree.html │ │ ├── mqttproxy │ │ ├── Connack.html │ │ ├── Connect.html │ │ ├── MqttProxyControl.html │ │ ├── PingReq.html │ │ ├── PingResp.html │ │ ├── Puback.html │ │ ├── Publish.html │ │ ├── Suback.html │ │ ├── Subscribe.html │ │ ├── Unsuback.html │ │ ├── Unsubscribe.html │ │ ├── package-frame.html │ │ ├── package-summary.html │ │ └── package-tree.html │ │ ├── networkconfig │ │ ├── DeleteNetworkReq.html │ │ ├── DeleteNetworkResp.html │ │ ├── EditNetworkReq.html │ │ ├── EditNetworkResp.html │ │ ├── ListNetworkReq.html │ │ ├── ListNetworkResp.html │ │ ├── SaveNetworkReq.html │ │ ├── SaveNetworkResp.html │ │ ├── package-frame.html │ │ ├── package-summary.html │ │ └── package-tree.html │ │ ├── package-frame.html │ │ ├── package-summary.html │ │ └── package-tree.html └── stylesheet.css ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **Build info** 14 | What is the branch and commit on the device side? 15 | What is the branch and tag the SDK side? 16 | Does pulling the latest code help resolve the issue? 17 | 18 | **System info** 19 | What is the Android version? 20 | What is the bluetooth version? 21 | 22 | **To Reproduce** 23 | What is the frequency of this issue? 24 | Steps to reproduce the behavior: 25 | 26 | **Log** 27 | Could you provide log on both the device side and SDK side? 28 | 29 | **Additional context** 30 | Add any other context about the problem here. 31 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | *Issue #, if available:* 2 | 3 | *Description of changes:* 4 | 5 | 6 | By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | *.aab 5 | 6 | # Files for the ART/Dalvik VM 7 | *.dex 8 | 9 | # Java class files 10 | *.class 11 | 12 | # Generated files 13 | bin/ 14 | gen/ 15 | out/ 16 | 17 | # Gradle files 18 | .gradle/ 19 | build/ 20 | 21 | # Local configuration file (sdk path, etc) 22 | local.properties 23 | 24 | # Proguard folder generated by Eclipse 25 | proguard/ 26 | 27 | # Log Files 28 | *.log 29 | 30 | # Android Studio Navigation editor temp files 31 | .navigation/ 32 | 33 | # Android Studio captures folder 34 | captures/ 35 | 36 | # IntelliJ 37 | *.iml 38 | .idea 39 | 40 | # Keystore files 41 | # Uncomment the following lines if you do not want to check your keystore files in. 42 | #*.jks 43 | #*.keystore 44 | 45 | # External native build folder generated in Android Studio 2.2 and later 46 | .externalNativeBuild 47 | 48 | # Google Services (e.g. APIs or Firebase) 49 | google-services.json 50 | 51 | # OSX files 52 | .DS_Store 53 | 54 | app/src/main/res/raw/test_keystore.bks 55 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Release notes for FreeRTOS BLE Mobile SDK for Android 2 | ## v1.5.0 5/12/2022 3 | - Backwards compatible change to add end of the list marker for WiFi Provisioning list network response. 4 | - Documentation improvements. 5 | - Upgrade build script to latest gradle version. 6 | ## v1.3.0 1/5/2022 7 | - Adds connect field to SaveNetwork Request. This allows connecting to WiFi SSID immediately or save credentials for connecting to it later. 8 | - Fixes a bug in disconnect callback in demo app. 9 | - Add WPA3 to the list of WiFi Security enum types. 10 | - Enable getters annotation for response classes. 11 | ## v1.2.0 7/7/2020 12 | - Fixes in the connection logic of SDK with BLE devices. 13 | - Add new states and notifications for BLE connection within SDK. 14 | - Modifications in auto-reconnect logic to handle devices with Resolvable Private Address(RPA) enabled. 15 | ## v1.1.0 2/11/2019 16 | Name change from Amazon FreeRTOS to FreeRTOS 17 | ## v1.0.0 6/17/2019 18 | Amazon FreeRTOS BLE Mobile SDK for Android 1.0.0 release. 19 | ### Features 20 | - Scan and connect to AmazonFreeRTOS device 21 | - Provision WiFi credential on AmazonFreeRTOS device 22 | - Proxy MQTT messages between AmazonFreeRTOS device and AWS IoT 23 | ## v0.9.0 11/26/2018 24 | Amazon FreeRTOS BLE Mobile SDK for Android beta release. 25 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check [existing open](https://github.com/aws/amazon-freertos-ble-android-sdk/issues), or [recently closed](https://github.com/aws/amazon-freertos-ble-android-sdk/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aclosed%20), issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *master* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any ['help wanted'](https://github.com/aws/amazon-freertos-ble-android-sdk/labels/help%20wanted) issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](https://github.com/aws/amazon-freertos-ble-android-sdk/blob/master/LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | 61 | We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes. 62 | -------------------------------------------------------------------------------- /DEPRECATED.txt: -------------------------------------------------------------------------------- 1 | # ![image](https://user-images.githubusercontent.com/56273942/202568467-0ee721bb-1424-4efd-88fc-31b4f2a59dc6.png) DEPRECATED 2 | 3 | ## Announcement: 4 | As of July 31st, 2023, this repository is deprecated. The contents of this repository will remain available but we will no longer provide updates nor accept new contributions and pull requests. We recommend instead that you start [here](https://docs.amplify.aws/start/q/integration/android/) for creating interactions between an Android device and AWS. For Bluetooth Low Energy APIs we recommend you start [here](https://developer.android.com/guide/topics/connectivity/bluetooth/ble-overview). 5 | 6 | Since the initial release of this package, several new Android versions have been released bringing with them enhanced security and usability. AWS Amplify has also [released dozens of later versions](https://github.com/aws-amplify/aws-sdk-android/releases) of the [AWS SDK](https://github.com/aws-amplify/aws-sdk-android) with many new features and bug fixes. 7 | 8 | The amazon-freertos-ble-android-sdk is being deprecated to remove an example built on outdated code. 9 | 10 | Have more questions? Post them in the [FreeRTOS forum](https://forums.freertos.org/). -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Android SDK for FreeRTOS Bluetooth Devices 2 | Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | 4 | Licensed under the Apache License Version 2.0 5 | 6 | ********************** 7 | THIRD PARTY COMPONENTS 8 | ********************** 9 | 10 | This software includes third party software subject to the following copyrights: 11 | 12 | - cbor-java 13 | Copyright 2013-2017 Constantin Rack 14 | 15 | Licensed under the Apache License, Version 2.0 (the "License"); 16 | you may not use this file except in compliance with the License. 17 | You may obtain a copy of the License at 18 | 19 | http://www.apache.org/licenses/LICENSE-2.0 20 | 21 | Unless required by applicable law or agreed to in writing, software 22 | distributed under the License is distributed on an "AS IS" BASIS, 23 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 24 | See the License for the specific language governing permissions and 25 | limitations under the License. 26 | 27 | 28 | - projectlombok 29 | Copyright © 2009-2019 The Project Lombok Authors, licensed under the MIT license. 30 | 31 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 32 | 33 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 34 | 35 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ![image](https://user-images.githubusercontent.com/56273942/202568467-0ee721bb-1424-4efd-88fc-31b4f2a59dc6.png) DEPRECATED 2 | 3 | ## Announcement: 4 | As of July 31st, 2023, this repository is deprecated. The contents of this repository will remain available but we will no longer provide updates nor accept new contributions and pull requests. We recommend instead that you start [here](https://docs.amplify.aws/start/q/integration/android/) for creating interactions between an Android device and AWS. For Bluetooth Low Energy APIs we recommend you start [here](https://developer.android.com/guide/topics/connectivity/bluetooth/ble-overview). 5 | 6 | Since the initial release of this package, several new Android versions have been released bringing with them enhanced security and usability. AWS Amplify has also [released dozens of later versions](https://github.com/aws-amplify/aws-sdk-android/releases) of the [AWS SDK](https://github.com/aws-amplify/aws-sdk-android) with many new features and bug fixes. 7 | 8 | The amazon-freertos-ble-android-sdk is being deprecated to remove an example built on outdated code. 9 | 10 | Have more questions? Post them in the [FreeRTOS forum](https://forums.freertos.org/). 11 | 12 | # FreeRTOS BLE Mobile SDK for Android 13 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/software.amazon.freertos/amazonfreertossdk/badge.svg?cacheSeconds=10)](https://maven-badges.herokuapp.com/maven-central/software.amazon.freertos/amazonfreertossdk/) 14 | ## Introduction 15 | 16 | Using the Android SDK for FreeRTOS Bluetooth Devices, you can create mobile applications that do the following: 17 | 18 | - Scan for and connect to nearby BLE devices running FreeRTOS 19 | 20 | - Perform WiFi provisioning of the FreeRTOS BLE devices after connecting to them ( Only supported for Espressif ESP32-DevKitC ) 21 | 22 | - Act as a proxy for transmitting MQTT messages between a device running FreeRTOS and the AWS IoT cloud 23 | 24 | ## System requirements 25 | 26 | - Android 6.0 (API level 23) or higher 27 | 28 | - Bluetooth 4.2 or higher 29 | 30 | - Android Studio 31 | 32 | ## Setting Up the SDK 33 | 34 | 1. Set the SDK as a dependency for the application. 35 | 36 | **Option 1**: install from maven 37 | In your app's `build.gradle` file, add the following into dependencies block: 38 | (replace x.y.z with [![Maven Central](https://maven-badges.herokuapp.com/maven-central/software.amazon.freertos/amazonfreertossdk/badge.svg?cacheSeconds=30)](https://maven-badges.herokuapp.com/maven-central/software.amazon.freertos/amazonfreertossdk/)) 39 | ``` 40 | implementation('software.amazon.freertos:amazonfreertossdk:x.y.z') 41 | ``` 42 | 43 | **Option 2**: Build the sdk locally. 44 | In your app's `build.gradle` file, add the following into dependencies block: 45 | ``` 46 | implementation project(':amazonfreertossdk') 47 | ``` 48 | In project's `settings.gradle` file, add ':amazonfreertossdk' 49 | ``` 50 | include ':app', ':amazonfreertossdk' 51 | ``` 52 | 53 | 2. In your app's `AndroidManifest.xml` file, add following permissions: 54 | 55 | ``` 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | ``` 66 | 67 | 3. Turn on phone's **Location Services**. 68 | 69 | In addition to adding location permissions, this is required by android to perform a successful BLE scan. 70 | 71 | ## Contents 72 | 73 | ### BLE Helper Functions 74 | 75 | The SDK includes some functions that help you perform BLE operations with FreeRTOS devices: 76 | 77 | ``` 78 | startScanDevices 79 | stopScanDevices 80 | connectToDevice 81 | disconnectFromDevice 82 | ``` 83 | 84 | Once the connection to the device is established, you get an AmazonFreeRTOSDevice object, and you can 85 | use this object to do WiFi provisioning or Mqtt proxy. 86 | 87 | ### WiFi Provisioning Service 88 | 89 | Provision the WiFi credential on the FreeRTOS device through the app. It provides 4 functions: 90 | 91 | ``` 92 | ListNetwork 93 | SaveNetwork 94 | EditNetwork 95 | DeleteNetwork 96 | ```` 97 | 98 | ### MQTT Proxy Service 99 | 100 | The MQTT proxy service controls the MQTT proxy. It allows the device to send and receive MQTT messages 101 | from the AWS IoT cloud through the phone, when this feature is enabled. 102 | 103 | 104 | You can find the [API documentation](https://aws.github.io/amazon-freertos-ble-android-sdk/) for these functions in the docs directory of this repository and [on github pages](https://aws.github.io/amazon-freertos-ble-android-sdk/). 105 | 106 | 107 | ## Demo Application 108 | 109 | The SDK includes a demo application that demonstrates some of the main features of the SDK. You can find the demo in [app](app). 110 | 111 | ## License 112 | 113 | This library is licensed under the Apache 2.0 License. 114 | -------------------------------------------------------------------------------- /amazonfreertossdk/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /amazonfreertossdk/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'maven-publish' 3 | apply plugin: 'signing' 4 | android { 5 | compileSdkVersion 26 6 | 7 | defaultConfig { 8 | 9 | minSdkVersion 23 10 | targetSdkVersion 26 11 | 12 | versionCode 1 13 | versionName "1.0" 14 | 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | } 27 | 28 | dependencies { 29 | implementation 'co.nstant.in:cbor:0.8' 30 | implementation ('com.amazonaws:aws-android-sdk-iot:2.13.5') 31 | 32 | implementation fileTree(dir: 'libs', include: ['*.jar']) 33 | implementation 'com.android.support:appcompat-v7:26.1.0' 34 | testImplementation 'junit:junit:4.12' 35 | compileOnly 'org.projectlombok:lombok:1.18.4' 36 | annotationProcessor 'org.projectlombok:lombok:1.18.4' 37 | } 38 | 39 | task androidJavaDocs(type: Javadoc) { 40 | source = android.sourceSets.main.java.srcDirs 41 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 42 | destinationDir = file("../docs/") 43 | failOnError false 44 | } 45 | 46 | task androidJavaDocsJar(type: Jar, dependsOn: androidJavaDocs) { 47 | archiveClassifier = 'javadoc' 48 | from tasks.androidJavaDocs.destinationDir 49 | } 50 | 51 | task androidSourcesJar(type: Jar) { 52 | archiveClassifier = 'sources' 53 | from android.sourceSets.main.java.srcDirs 54 | } 55 | 56 | task copyLibs(type: Copy) { 57 | from configurations.implementation 58 | into 'libs' 59 | } 60 | 61 | configurations { 62 | archives { 63 | extendsFrom configurations.default 64 | } 65 | } 66 | 67 | publishing { 68 | publications { 69 | pluginPublication(MavenPublication) { 70 | groupId publishedGroupId 71 | artifactId artifact 72 | version = versionNumber 73 | artifact androidJavaDocsJar 74 | artifact androidSourcesJar 75 | 76 | pom { 77 | packaging = "aar" 78 | name = libraryName 79 | description = libraryDescription 80 | groupId = publishedGroupId 81 | artifactId = artifact 82 | url = siteUrl 83 | 84 | licenses { 85 | license { 86 | name = licenseName 87 | url = licenseUrl 88 | distribution = "repo" 89 | } 90 | } 91 | developers { 92 | developer { 93 | id = developerId 94 | name = developerName 95 | email = developerEmail 96 | } 97 | } 98 | scm { 99 | connection = gitUrl 100 | developerConnection = gitUrl 101 | url = gitUrl 102 | } 103 | } 104 | 105 | afterEvaluate { 106 | from components.release 107 | } 108 | } 109 | } 110 | 111 | repositories { 112 | 113 | maven { 114 | 115 | def stagingRepoUrl = "https://aws.oss.sonatype.org/service/local/staging/deploy/maven2/" 116 | def snapshotsRepoUrl = "https://aws.oss.sonatype.org/content/repositories/snapshots/" 117 | url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : stagingRepoUrl 118 | 119 | credentials { 120 | username ossrhUsername 121 | password ossrhPassword 122 | 123 | } 124 | } 125 | } 126 | } 127 | signing { 128 | sign configurations.archives 129 | sign publishing.publications.pluginPublication 130 | } -------------------------------------------------------------------------------- /amazonfreertossdk/gradle.properties: -------------------------------------------------------------------------------- 1 | publishedGroupId=software.amazon.freertos 2 | artifact=amazonfreertossdk 3 | versionNumber=1.5.0 4 | libraryName=amazonfreertossdk 5 | libraryDescription=Android SDK for FreeRTOS. 6 | siteUrl=https://github.com/aws/amazon-freertos-ble-android-sdk 7 | licenseName=The Apache Software License, Version 2.0 8 | licenseUrl=http://www.apache.org/licenses/LICENSE-2.0.txt 9 | developerId=amazon freertos 10 | developerName=FreeRTOS 11 | developerEmail=aws-iot-device-ble@amazon.com 12 | gitUrl=https://github.com/aws/amazon-freertos-ble-android-sdk.git 13 | ossrhUsername= 14 | ossrhPassword= 15 | -------------------------------------------------------------------------------- /amazonfreertossdk/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/androidTest/java/software/amazon/freertos/amazonfreertossdk/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.amazonfreertossdk; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.amazon.aws.amazonfreertossdk.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/AmazonFreeRTOSConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk; 17 | 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | 21 | /** 22 | * This class defines some constants used in the SDK. 23 | */ 24 | public class AmazonFreeRTOSConstants { 25 | 26 | /** 27 | * Network security types. 28 | */ 29 | public static final int NETWORK_SECURITY_TYPE_OPEN = 0; 30 | public static final int NETWORK_SECURITY_TYPE_WEP = 1; 31 | public static final int NETWORK_SECURITY_TYPE_WPA = 2; 32 | public static final int NETWORK_SECURITY_TYPE_WPA2 = 3; 33 | public static final int NETWORK_SECURITY_TYPE_NOT_SUPPORTED = 4; 34 | 35 | /** 36 | * MQTT proxy state. 37 | */ 38 | public static final int MQTT_PROXY_CONTROL_OFF = 0; 39 | public static final int MQTT_PROXY_CONTROL_ON = 1; 40 | 41 | /** 42 | * message type. 43 | */ 44 | public static final int MQTT_MSG_CONNECT = 1; 45 | public static final int MQTT_MSG_CONNACK = 2; 46 | public static final int MQTT_MSG_PUBLISH = 3; 47 | public static final int MQTT_MSG_PUBACK = 4; 48 | public static final int MQTT_MSG_PUBREC = 5; 49 | public static final int MQTT_MSG_PUBREL = 6; 50 | public static final int MQTT_MSG_PUBCOMP = 7; 51 | public static final int MQTT_MSG_SUBSCRIBE = 8; 52 | public static final int MQTT_MSG_SUBACK = 9; 53 | public static final int MQTT_MSG_UNSUBSCRIBE = 10; 54 | public static final int MQTT_MSG_UNSUBACK = 11; 55 | public static final int MQTT_MSG_PINGREQ = 12; 56 | public static final int MQTT_MSG_PINGRESP = 13; 57 | public static final int MQTT_MSG_DISCONNECT = 14; 58 | 59 | public static final int LIST_NETWORK_REQ = 1; 60 | public static final int LIST_NETWORK_RESP = 2; 61 | public static final int SAVE_NETWORK_REQ = 3; 62 | public static final int SAVE_NETWORK_RESP = 4; 63 | public static final int EDIT_NETWORK_REQ = 5; 64 | public static final int EDIT_NETWORK_RESP = 6; 65 | public static final int DELETE_NETWORK_REQ = 7; 66 | public static final int DELETE_NETWORK_RESP = 8; 67 | 68 | /** 69 | * Bluetooth connection state. This is matching with BluetoothProfile in the Android SDK. 70 | */ 71 | public enum BleConnectionState { 72 | BLE_DISCONNECTED, // = 0 73 | BLE_CONNECTING, // = 1 74 | BLE_CONNECTED, // = 2 75 | BLE_DISCONNECTING, // = 3 76 | BLE_INITIALIZED, // = 4 77 | BLE_INITIALIZING // = 5 78 | } 79 | 80 | public enum AmazonFreeRTOSError { 81 | BLE_DISCONNECTED_ERROR 82 | } 83 | 84 | /** 85 | * The MQTT connection state. 86 | * Do not change the order of this enum. This is a contract between device library and our sdk. 87 | */ 88 | public enum MqttConnectionState { 89 | MQTT_Unknown, 90 | MQTT_Connecting, 91 | MQTT_Connected, 92 | MQTT_Disconnected, 93 | MQTT_ConnectionRefused, 94 | MQTT_ConnectionError, 95 | MQTT_ProtocolError 96 | } 97 | 98 | /** 99 | * This defines how much time the SDK scans for nearby BLE devices. 100 | */ 101 | public static final long SCAN_PERIOD = 20000; //ms 102 | 103 | /** 104 | * After sending BLE commands to device, the SDK will wait for this amount of time, after which 105 | * it will time out and continue to process the next BLE command. 106 | */ 107 | public static final int BLE_COMMAND_TIMEOUT = 3000; //ms 108 | 109 | public static final String UUID_AmazonFreeRTOS = "8a7f1168-48af-4efb-83b5-e679f932ff00"; 110 | 111 | public static final String UUID_NETWORK_SERVICE = "a9d7166a-d72e-40a9-a002-48044cc30100"; 112 | public static final String UUID_NETWORK_CONTROL = "a9d7166a-d72e-40a9-a002-48044cc30101"; 113 | public static final String UUID_NETWORK_TX = "a9d7166a-d72e-40a9-a002-48044cc30102"; 114 | public static final String UUID_NETWORK_RX = "a9d7166a-d72e-40a9-a002-48044cc30103"; 115 | public static final String UUID_NETWORK_TXLARGE = "a9d7166a-d72e-40a9-a002-48044cc30104"; 116 | public static final String UUID_NETWORK_RXLARGE = "a9d7166a-d72e-40a9-a002-48044cc30105"; 117 | 118 | public static final String UUID_MQTT_PROXY_SERVICE = "a9d7166a-d72e-40a9-a002-48044cc30000"; 119 | public static final String UUID_MQTT_PROXY_CONTROL = "a9d7166a-d72e-40a9-a002-48044cc30001"; 120 | public static final String UUID_MQTT_PROXY_TX = "a9d7166a-d72e-40a9-a002-48044cc30002"; 121 | public static final String UUID_MQTT_PROXY_RX = "a9d7166a-d72e-40a9-a002-48044cc30003"; 122 | public static final String UUID_MQTT_PROXY_TXLARGE = "a9d7166a-d72e-40a9-a002-48044cc30004"; 123 | public static final String UUID_MQTT_PROXY_RXLARGE = "a9d7166a-d72e-40a9-a002-48044cc30005"; 124 | 125 | public static final String UUID_DEVICE_INFORMATION_SERVICE = "8a7f1168-48af-4efb-83b5-e679f932ff00"; 126 | public static final String UUID_DEVICE_VERSION = "8a7f1168-48af-4efb-83b5-e679f932ff01"; 127 | public static final String UUID_IOT_ENDPOINT = "8a7f1168-48af-4efb-83b5-e679f932ff02"; 128 | public static final String UUID_DEVICE_MTU = "8a7f1168-48af-4efb-83b5-e679f932ff03"; 129 | public static final String UUID_DEVICE_PLATFORM = "8a7f1168-48af-4efb-83b5-e679f932ff04"; 130 | public static final String UUID_DEVICE_ID = "8a7f1168-48af-4efb-83b5-e679f932ff05"; 131 | 132 | public static final Map uuidToName = new HashMap() { 133 | { 134 | put(UUID_NETWORK_CONTROL, "NETWORK_CONTROL"); 135 | put(UUID_NETWORK_TX, "NETWORK_TX"); 136 | put(UUID_NETWORK_RX, "NETWORK_RX"); 137 | put(UUID_NETWORK_TXLARGE, "NETWORK_TXLARGE"); 138 | put(UUID_NETWORK_RXLARGE, "NETWORK_RXLARGE"); 139 | put(UUID_MQTT_PROXY_CONTROL, "MQTT_CONTROL"); 140 | put(UUID_MQTT_PROXY_TX, "MQTT_TX"); 141 | put(UUID_MQTT_PROXY_TXLARGE, "MQTT_TXLARGE"); 142 | put(UUID_MQTT_PROXY_RX, "MQTT_RX"); 143 | put(UUID_MQTT_PROXY_RXLARGE, "MQTT_RXLARGE"); 144 | put(UUID_DEVICE_VERSION, "DEVICE_VERSION"); 145 | put(UUID_IOT_ENDPOINT, "IOT_ENDPOINT"); 146 | put(UUID_DEVICE_MTU, "DEVICE_MTU"); 147 | put(UUID_DEVICE_PLATFORM, "DEVICE_PLATFORM"); 148 | put(UUID_DEVICE_ID, "DEVICE_ID"); 149 | } 150 | }; 151 | 152 | } 153 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/BleCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk; 17 | 18 | import java.nio.ByteBuffer; 19 | 20 | import lombok.Getter; 21 | 22 | /** 23 | * This class defines the BLE command that is sent from SDK to device. 24 | */ 25 | @Getter 26 | public class BleCommand { 27 | enum CommandType { 28 | WRITE_DESCRIPTOR, 29 | WRITE_CHARACTERISTIC, 30 | READ_CHARACTERISTIC, 31 | DISCOVER_SERVICES, 32 | REQUEST_MTU, 33 | NOTIFICATION 34 | } 35 | 36 | /** 37 | * The type of the BLE command. 38 | */ 39 | private CommandType type; 40 | 41 | /** 42 | * The characteristic uuid of the BLE command. 43 | */ 44 | private String characteristicUuid; 45 | 46 | /** 47 | * The service uuid of the BLE command. 48 | */ 49 | private String serviceUuid; 50 | 51 | /** 52 | * The data to be sent with the BLE command. 53 | */ 54 | private byte[] data; 55 | 56 | /** 57 | * Construct a BLE command with data. 58 | * @param t the BLE command type. 59 | * @param cUuid the characteristic uuid. 60 | * @param sUuid the service uuid. 61 | * @param d the data to be sent with the BLE command. 62 | */ 63 | public BleCommand(CommandType t, String cUuid, String sUuid, byte[] d) { 64 | type = t; 65 | characteristicUuid = cUuid; 66 | serviceUuid = sUuid; 67 | data = d; 68 | } 69 | 70 | /** 71 | * Construct a BLE command without any data. 72 | * @param t the BLE command type. 73 | * @param cUuid the characteristic uuid. 74 | * @param sUuid the service uuid. 75 | */ 76 | public BleCommand(CommandType t, String cUuid, String sUuid) { 77 | this(t, cUuid, sUuid, null); 78 | } 79 | 80 | public BleCommand(CommandType t) { 81 | this(t, null, null); 82 | } 83 | 84 | public BleCommand(CommandType t, int mtu) { 85 | this(t, null, null, ByteBuffer.allocate(4).putInt(mtu).array()); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/BleConnectionStatusCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk; 17 | 18 | /** 19 | * This is a callback to notify the app of BLE connection state change. 20 | */ 21 | public abstract class BleConnectionStatusCallback { 22 | /** 23 | * This callback is triggered when BLE connection between SDK and device has changed. The app 24 | * @param connectionStatus The BLE connection state. 25 | */ 26 | public void onBleConnectionStatusChanged(AmazonFreeRTOSConstants.BleConnectionState connectionStatus) {} 27 | } 28 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/BleScanResultCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk; 17 | 18 | import android.bluetooth.le.ScanResult; 19 | 20 | /** 21 | * This is a callback to notify app of BLE Scan results. 22 | */ 23 | public abstract class BleScanResultCallback { 24 | /** 25 | * This method is called when a nearby BLE device is found during scanning. 26 | * @param result BLE ScanResult 27 | */ 28 | public void onBleScanResult(ScanResult result){} 29 | 30 | public void onBleScanFailed(int errorcode) {} 31 | } 32 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/DeviceInfoCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk; 17 | 18 | /** 19 | * This is a callback class to notify the app of device information, including mtu, broker endpoint 20 | * and device software version. 21 | */ 22 | public abstract class DeviceInfoCallback { 23 | 24 | /** 25 | * This callback is triggered when device sends the current mtu number in response to getMtu. 26 | * @param mtu the current mtu value negotiated between device and Android phone. 27 | */ 28 | public void onObtainMtu(int mtu){} 29 | /** 30 | * This callback is triggered when device sends its MQTT broker endpoint in response to 31 | * getBrokerEndpoint. 32 | * @param endpoint The current MQTT broker endpoint set on the device. 33 | */ 34 | public void onObtainBrokerEndpoint(String endpoint){} 35 | /** 36 | * This callback is triggered when device sends its current software version in response to 37 | * getDeviceVersion. 38 | * @param version The current device library version on the device. 39 | */ 40 | public void onObtainDeviceSoftwareVersion(String version){} 41 | 42 | public void onError(AmazonFreeRTOSConstants.AmazonFreeRTOSError Error) {} 43 | } 44 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/MessageType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.util.List; 22 | 23 | import co.nstant.in.cbor.CborDecoder; 24 | import co.nstant.in.cbor.CborException; 25 | import co.nstant.in.cbor.model.DataItem; 26 | import co.nstant.in.cbor.model.Map; 27 | import co.nstant.in.cbor.model.UnicodeString; 28 | import co.nstant.in.cbor.model.UnsignedInteger; 29 | 30 | /** 31 | * This class represents the message type. 32 | */ 33 | public class MessageType { 34 | private static final String TAG = "MessageType"; 35 | private static final String TYPE_KEY = "w"; 36 | /** 37 | * MQTT message type. 38 | */ 39 | public int type; 40 | 41 | public boolean decode(byte[] cborEncodedBytes) { 42 | ByteArrayInputStream bais = new ByteArrayInputStream(cborEncodedBytes); 43 | try { 44 | List dataItems = new CborDecoder(bais).decode(); 45 | // process data item 46 | Map map = (Map) dataItems.get(0); 47 | DataItem dataItem = map.get(new UnicodeString(TYPE_KEY)); 48 | type = ((UnsignedInteger) dataItem).getValue().intValue(); 49 | return true; 50 | } catch (CborException e) { 51 | Log.e(TAG,"Failed to decode.", e); 52 | return false; 53 | } catch (IndexOutOfBoundsException e) { 54 | return false; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/NetworkConfigCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk; 17 | 18 | import software.amazon.freertos.amazonfreertossdk.networkconfig.DeleteNetworkResp; 19 | import software.amazon.freertos.amazonfreertossdk.networkconfig.EditNetworkResp; 20 | import software.amazon.freertos.amazonfreertossdk.networkconfig.ListNetworkResp; 21 | import software.amazon.freertos.amazonfreertossdk.networkconfig.SaveNetworkResp; 22 | 23 | public abstract class NetworkConfigCallback { 24 | 25 | public void onListNetworkResponse(ListNetworkResp response){} 26 | 27 | public void onSaveNetworkResponse(SaveNetworkResp response){} 28 | 29 | public void onEditNetworkResponse(EditNetworkResp response){} 30 | 31 | public void onDeleteNetworkResponse(DeleteNetworkResp response){} 32 | } 33 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/deviceinfo/BrokerEndpoint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.deviceinfo; 17 | 18 | /** 19 | * This class represents the broker endpoint object that is transferred between ble device and SDK. 20 | * When SDK sends a read characteristic command to the ble device, this class object is returned in 21 | * the response back to SDK. 22 | * The broker endpoint is the AWS IoT endpoint from AWS IOT Console. 23 | */ 24 | public class BrokerEndpoint { 25 | public String brokerEndpoint; 26 | } 27 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/deviceinfo/Mtu.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.deviceinfo; 17 | 18 | /** 19 | * This class represents the mtu object transferred between ble device and the SDK. 20 | * When SDK sends a read characteristic command to device to get the current mtu size, this object 21 | * is returned in the response. 22 | */ 23 | public class Mtu { 24 | public String mtu; 25 | } 26 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/deviceinfo/Version.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.deviceinfo; 17 | 18 | /** 19 | * This class represents the AmazonFreeRTOS BLE library version running on the device. When SDK 20 | * sends a read characteristic command to get the BLE library version on the device, this object 21 | * is returned in the response. 22 | */ 23 | public class Version { 24 | public String version; 25 | } 26 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/Connack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | 22 | import co.nstant.in.cbor.CborBuilder; 23 | import co.nstant.in.cbor.CborEncoder; 24 | import co.nstant.in.cbor.CborException; 25 | 26 | /** 27 | * This class represents the MQTT CONNACK message. 28 | */ 29 | public class Connack { 30 | private static final String TAG = "MqttConnack"; 31 | 32 | private static final String TYPE_KEY = "w"; 33 | private static final String STATUS_KEY = "s"; 34 | /** 35 | * MQTT message type. 36 | */ 37 | public int type; 38 | /** 39 | * The MQTT connection status defined in {@code MqttConnectionState} enum. 40 | */ 41 | public int status; 42 | 43 | public byte[] encode() { 44 | byte[] connackBytes = null; 45 | try { 46 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 47 | new CborEncoder(baos).encode(new CborBuilder() 48 | .addMap() 49 | .put(TYPE_KEY, type) 50 | .put(STATUS_KEY, status) 51 | .end() 52 | .build()); 53 | connackBytes = baos.toByteArray(); 54 | } catch (CborException e) { 55 | Log.e(TAG, "Failed to encode.", e); 56 | } 57 | return connackBytes; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/Connect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.io.ByteArrayOutputStream; 22 | import java.util.List; 23 | 24 | import co.nstant.in.cbor.CborBuilder; 25 | import co.nstant.in.cbor.CborDecoder; 26 | import co.nstant.in.cbor.CborEncoder; 27 | import co.nstant.in.cbor.CborException; 28 | import co.nstant.in.cbor.model.DataItem; 29 | import co.nstant.in.cbor.model.Map; 30 | import co.nstant.in.cbor.model.SimpleValue; 31 | import co.nstant.in.cbor.model.SimpleValueType; 32 | import co.nstant.in.cbor.model.UnicodeString; 33 | import co.nstant.in.cbor.model.UnsignedInteger; 34 | 35 | /** 36 | * This class represents the MQTT CONNECT message. 37 | */ 38 | public class Connect { 39 | private static final String TAG = "MqttConnect"; 40 | private static final String TYPE_KEY = "w"; 41 | private static final String CLIENTID_KEY = "d"; 42 | private static final String BROKERENDPOINT_KEY = "a"; 43 | private static final String CLEANSESSION_KEY = "c"; 44 | /** 45 | * MQTT message type. 46 | */ 47 | public int type; 48 | /** 49 | * MQTT client id. 50 | */ 51 | public String clientID; 52 | /** 53 | * MQTT broker endpoint. 54 | */ 55 | public String brokerEndpoint; 56 | /** 57 | * MQTT clean session. 58 | */ 59 | public boolean cleanSession; 60 | public String toString() { 61 | return String.format(" Connect message -> \n clientID: %s\n endpoint: %s\n cleansession: %s", 62 | clientID, brokerEndpoint, (cleanSession? "true":"false") ); 63 | } 64 | 65 | public byte[] encode() { 66 | byte[] connectBytes = null; 67 | try { 68 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 69 | new CborEncoder(baos).encode(new CborBuilder() 70 | .addMap() 71 | .put(TYPE_KEY, type) 72 | .put(CLIENTID_KEY, clientID) 73 | .put(BROKERENDPOINT_KEY, brokerEndpoint) 74 | .put(CLEANSESSION_KEY, cleanSession) 75 | .end() 76 | .build()); 77 | connectBytes = baos.toByteArray(); 78 | } catch (CborException e) { 79 | Log.e(TAG, "Failed to encode.", e); 80 | } 81 | return connectBytes; 82 | } 83 | 84 | public boolean decode(byte[] cborEncodedBytes) { 85 | ByteArrayInputStream bais = new ByteArrayInputStream(cborEncodedBytes); 86 | try { 87 | List dataItems = new CborDecoder(bais).decode(); 88 | // process data item 89 | Map map = (Map) dataItems.get(0); 90 | DataItem dataItem = map.get(new UnicodeString(TYPE_KEY)); 91 | type = ((UnsignedInteger) dataItem).getValue().intValue(); 92 | dataItem = map.get(new UnicodeString(CLIENTID_KEY)); 93 | clientID = ((UnicodeString) dataItem).getString(); 94 | dataItem = map.get(new UnicodeString(BROKERENDPOINT_KEY)); 95 | brokerEndpoint = ((UnicodeString) dataItem).getString(); 96 | dataItem = map.get(new UnicodeString(CLEANSESSION_KEY)); 97 | cleanSession = (((SimpleValue) dataItem).getSimpleValueType() == SimpleValueType.TRUE) ? true:false; 98 | return true; 99 | } catch (CborException e) { 100 | Log.e(TAG,"Failed to decode.", e); 101 | return false; 102 | } catch (IndexOutOfBoundsException e) { 103 | return false; 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/MqttProxyControl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | 22 | import co.nstant.in.cbor.CborBuilder; 23 | import co.nstant.in.cbor.CborEncoder; 24 | import co.nstant.in.cbor.CborException; 25 | 26 | /** 27 | * This class represents the MQTT proxy state. SDK sends this object to device to switch on/off 28 | * MQTT proxy. 29 | */ 30 | public class MqttProxyControl { 31 | private static final String TAG = "MqttProxyControl"; 32 | private static final String PROXYSTATE_KEY = "l"; 33 | /** 34 | * The state of MQTT proxy. 35 | */ 36 | public int proxyState; 37 | 38 | public byte[] encode() { 39 | byte[] mqttProxyControlBytes = null; 40 | try { 41 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 42 | new CborEncoder(baos).encode(new CborBuilder() 43 | .addMap() 44 | .put(PROXYSTATE_KEY, proxyState) 45 | .end() 46 | .build()); 47 | mqttProxyControlBytes = baos.toByteArray(); 48 | } catch (CborException e) { 49 | Log.e(TAG, "Failed to encode.", e); 50 | } 51 | return mqttProxyControlBytes; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/PingReq.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 2 | 3 | public class PingReq { 4 | } 5 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/PingResp.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 2 | 3 | import android.util.Log; 4 | 5 | import java.io.ByteArrayOutputStream; 6 | 7 | import co.nstant.in.cbor.CborBuilder; 8 | import co.nstant.in.cbor.CborEncoder; 9 | import co.nstant.in.cbor.CborException; 10 | 11 | import static software.amazon.freertos.amazonfreertossdk.AmazonFreeRTOSConstants.MQTT_MSG_PINGRESP; 12 | 13 | public class PingResp { 14 | private static final String TAG = "PingResp"; 15 | 16 | private static final String TYPE_KEY = "w"; 17 | 18 | public byte[] encode() { 19 | byte[] bytes = null; 20 | try { 21 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 22 | new CborEncoder(baos).encode(new CborBuilder() 23 | .addMap() 24 | .put(TYPE_KEY, MQTT_MSG_PINGRESP) 25 | .end() 26 | .build()); 27 | bytes = baos.toByteArray(); 28 | } catch (CborException e) { 29 | Log.e(TAG, "Failed to encode.", e); 30 | } 31 | return bytes; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/Puback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.io.ByteArrayOutputStream; 22 | import java.util.List; 23 | 24 | import co.nstant.in.cbor.CborBuilder; 25 | import co.nstant.in.cbor.CborDecoder; 26 | import co.nstant.in.cbor.CborEncoder; 27 | import co.nstant.in.cbor.CborException; 28 | import co.nstant.in.cbor.model.DataItem; 29 | import co.nstant.in.cbor.model.Map; 30 | import co.nstant.in.cbor.model.Number; 31 | import co.nstant.in.cbor.model.UnicodeString; 32 | import co.nstant.in.cbor.model.UnsignedInteger; 33 | 34 | /** 35 | * This class represents the MQTT PUBACK message. 36 | */ 37 | public class Puback { 38 | private static final String TAG = "MqttPuback"; 39 | 40 | private static final String TYPE_KEY = "w"; 41 | private static final String MSGID_KEY = "i"; 42 | /** 43 | * MQTT message type. 44 | */ 45 | public int type; 46 | /** 47 | * MQTT message ID. 48 | */ 49 | public int msgID; 50 | 51 | public byte[] encode() { 52 | byte[] pubackBytes = null; 53 | try { 54 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 55 | new CborEncoder(baos).encode(new CborBuilder() 56 | .addMap() 57 | .put(TYPE_KEY, type) 58 | .put(MSGID_KEY, msgID) 59 | .end() 60 | .build()); 61 | pubackBytes = baos.toByteArray(); 62 | } catch (CborException e) { 63 | Log.e(TAG, "Failed to encode.", e); 64 | } 65 | return pubackBytes; 66 | } 67 | 68 | public boolean decode(byte[] cborEncodedBytes) { 69 | ByteArrayInputStream bais = new ByteArrayInputStream(cborEncodedBytes); 70 | try { 71 | List dataItems = new CborDecoder(bais).decode(); 72 | // process data item 73 | Map map = (Map) dataItems.get(0); 74 | DataItem dataItem = map.get(new UnicodeString(TYPE_KEY)); 75 | type = ((UnsignedInteger) dataItem).getValue().intValue(); 76 | dataItem = map.get(new UnicodeString(MSGID_KEY)); 77 | msgID = ((Number) dataItem).getValue().intValue(); 78 | return true; 79 | } catch (CborException e) { 80 | Log.e(TAG,"Failed to decode.", e); 81 | return false; 82 | } catch (IndexOutOfBoundsException e) { 83 | return false; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/Publish.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.io.ByteArrayOutputStream; 22 | import java.util.List; 23 | 24 | import co.nstant.in.cbor.CborBuilder; 25 | import co.nstant.in.cbor.CborDecoder; 26 | import co.nstant.in.cbor.CborEncoder; 27 | import co.nstant.in.cbor.CborException; 28 | import co.nstant.in.cbor.model.ByteString; 29 | import co.nstant.in.cbor.model.DataItem; 30 | import co.nstant.in.cbor.model.Map; 31 | import co.nstant.in.cbor.model.Number; 32 | import co.nstant.in.cbor.model.UnicodeString; 33 | import co.nstant.in.cbor.model.UnsignedInteger; 34 | import lombok.AllArgsConstructor; 35 | import lombok.NoArgsConstructor; 36 | 37 | /** 38 | * This class represents the MQTT PUBLISH message. 39 | */ 40 | @AllArgsConstructor 41 | @NoArgsConstructor 42 | public class Publish { 43 | private static final String TAG = "MqttPublish"; 44 | private static final String TYPE_KEY = "w"; 45 | private static final String TOPIC_KEY = "u"; 46 | private static final String MSGID_KEY = "i"; 47 | private static final String QOS_KEY = "n"; 48 | private static final String PAYLOAD_KEY = "k"; 49 | /** 50 | * MQTT message type. 51 | */ 52 | private int type; 53 | /** 54 | * MQTT PUBLISH message topic. 55 | */ 56 | private String topic; 57 | /** 58 | * MQTT message ID. 59 | */ 60 | private int msgID; 61 | /** 62 | * MQTT PUBLISH message QOS. 63 | */ 64 | private int qoS; 65 | /** 66 | * The data in the MQTT PUBLISH message. 67 | */ 68 | private byte[] payloadBytes; 69 | 70 | public String toString() { 71 | return String.format(" Publish message -> \n topic:%s\n msgID:%d\n qos:%d\n payload:%s", 72 | topic, msgID, qoS, new String(payloadBytes)); 73 | } 74 | 75 | public String getTopic() { 76 | return topic; 77 | } 78 | 79 | public byte[] getPayload() { 80 | return payloadBytes; 81 | } 82 | 83 | public int getMsgID() { 84 | return msgID; 85 | } 86 | 87 | public int getQos() { 88 | return qoS; 89 | } 90 | 91 | public byte[] encode() { 92 | byte[] publishBytes = null; 93 | try { 94 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 95 | new CborEncoder(baos).encode(new CborBuilder() 96 | .addMap() 97 | .put(TYPE_KEY, type) 98 | .put(TOPIC_KEY, topic) 99 | .put(MSGID_KEY, msgID) 100 | .put(QOS_KEY, qoS) 101 | .put(PAYLOAD_KEY, payloadBytes) 102 | .end() 103 | .build()); 104 | publishBytes = baos.toByteArray(); 105 | } catch (CborException e) { 106 | Log.e(TAG, "Failed to encode.", e); 107 | } 108 | return publishBytes; 109 | } 110 | 111 | public boolean decode(byte[] cborEncodedBytes) { 112 | ByteArrayInputStream bais = new ByteArrayInputStream(cborEncodedBytes); 113 | try { 114 | List dataItems = new CborDecoder(bais).decode(); 115 | // process data item 116 | Map map = (Map) dataItems.get(0); 117 | DataItem dataItem = map.get(new UnicodeString(TYPE_KEY)); 118 | type = ((UnsignedInteger) dataItem).getValue().intValue(); 119 | dataItem = map.get(new UnicodeString(TOPIC_KEY)); 120 | topic = ((UnicodeString) dataItem).getString(); 121 | dataItem = map.get(new UnicodeString(QOS_KEY)); 122 | qoS = ((Number) dataItem).getValue().intValue(); 123 | if (qoS != 0) { 124 | dataItem = map.get(new UnicodeString(MSGID_KEY)); 125 | msgID = ((Number) dataItem).getValue().intValue(); 126 | } 127 | dataItem = map.get(new UnicodeString(PAYLOAD_KEY)); 128 | payloadBytes = ((ByteString) dataItem).getBytes(); 129 | return true; 130 | } catch (CborException e) { 131 | Log.e(TAG,"Failed to decode.", e); 132 | return false; 133 | } catch (IndexOutOfBoundsException e) { 134 | return false; 135 | } 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/Suback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | 22 | import co.nstant.in.cbor.CborBuilder; 23 | import co.nstant.in.cbor.CborEncoder; 24 | import co.nstant.in.cbor.CborException; 25 | 26 | /** 27 | * This class represents the MQTT SUBACK message. 28 | */ 29 | public class Suback { 30 | private static final String TAG = "MqttSuback"; 31 | private static final String TYPE_KEY = "w"; 32 | private static final String MSGID_KEY = "i"; 33 | private static final String STATUS_KEY = "s"; 34 | /** 35 | * MQTT message type. 36 | */ 37 | public int type; 38 | /** 39 | * MQTT message ID. 40 | */ 41 | public int msgID; 42 | /** 43 | * MQTT SUBACK status. This is set to the QOS number in the corresponding MQTT SUBSCRIBE 44 | * message, to which this SUBACK is acknowledging. 45 | */ 46 | public int status; 47 | 48 | public byte[] encode() { 49 | byte[] subackBytes = null; 50 | try { 51 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 52 | new CborEncoder(baos).encode(new CborBuilder() 53 | .addMap() 54 | .put(TYPE_KEY, type) 55 | .put(MSGID_KEY, msgID) 56 | .put(STATUS_KEY, status) 57 | .end() 58 | .build()); 59 | subackBytes = baos.toByteArray(); 60 | } catch (CborException e) { 61 | Log.e(TAG, "Failed to encode.", e); 62 | } 63 | return subackBytes; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/Subscribe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.io.ByteArrayOutputStream; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | import co.nstant.in.cbor.CborBuilder; 26 | import co.nstant.in.cbor.CborDecoder; 27 | import co.nstant.in.cbor.CborEncoder; 28 | import co.nstant.in.cbor.CborException; 29 | import co.nstant.in.cbor.builder.ArrayBuilder; 30 | import co.nstant.in.cbor.builder.MapBuilder; 31 | import co.nstant.in.cbor.model.Array; 32 | import co.nstant.in.cbor.model.DataItem; 33 | import co.nstant.in.cbor.model.Map; 34 | import co.nstant.in.cbor.model.UnicodeString; 35 | import co.nstant.in.cbor.model.Number; 36 | import co.nstant.in.cbor.model.UnsignedInteger; 37 | 38 | /** 39 | * This class represents the MQTT SUBSCRIBE message. 40 | */ 41 | public class Subscribe { 42 | private static final String TAG = "MqttSubscribe"; 43 | private static final String TYPE_KEY = "w"; 44 | private static final String TOPICS_KEY = "v"; 45 | private static final String MSGID_KEY = "i"; 46 | private static final String QOSS_KEY = "o"; 47 | /** 48 | * MQTT message type. 49 | */ 50 | public int type; 51 | /** 52 | * Arrary of topics to subscribe to. 53 | */ 54 | public List topics = new ArrayList<>(); 55 | /** 56 | * MQTT message ID. 57 | */ 58 | public int msgID; 59 | /** 60 | * Arrary of QOS for each subscribe topic. 61 | */ 62 | public List qoSs = new ArrayList<>(); 63 | 64 | public String toString() { 65 | StringBuilder stringBuilder = new StringBuilder(); 66 | stringBuilder.append("Subscribe message: "); 67 | stringBuilder.append("\n type: " + type); 68 | stringBuilder.append("\n msgId: " + msgID); 69 | for (int i = 0; i < topics.size(); i++) { 70 | stringBuilder.append("\n topic: " + topics.get(i) + ", qos: " + qoSs.get(i)); 71 | } 72 | return stringBuilder.toString(); 73 | } 74 | 75 | public byte[] encode() { 76 | byte[] subscribeBytes = null; 77 | try { 78 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 79 | 80 | ArrayBuilder> topicsArray = new CborBuilder() 81 | .addMap() 82 | .put(TYPE_KEY, type) 83 | .putArray(TOPICS_KEY); 84 | 85 | for (String topic : topics) { 86 | topicsArray = topicsArray.add(topic); 87 | } 88 | 89 | MapBuilder map = topicsArray.end(); 90 | ArrayBuilder> qosArray = map.put(MSGID_KEY, msgID) 91 | .putArray(QOSS_KEY); 92 | 93 | for (int qos : qoSs) { 94 | qosArray = qosArray.add(qos); 95 | } 96 | List cbordata = qosArray.end().end().build(); 97 | new CborEncoder(baos).encode(cbordata); 98 | subscribeBytes = baos.toByteArray(); 99 | } catch (CborException e) { 100 | Log.e(TAG, "Failed to encode.", e); 101 | } 102 | return subscribeBytes; 103 | } 104 | 105 | public boolean decode(byte[] cborEncodedBytes) { 106 | ByteArrayInputStream bais = new ByteArrayInputStream(cborEncodedBytes); 107 | try { 108 | List dataItems = new CborDecoder(bais).decode(); 109 | // process data item 110 | Map map = (Map) dataItems.get(0); 111 | DataItem dataItem = map.get(new UnicodeString(TYPE_KEY)); 112 | type = ((UnsignedInteger) dataItem).getValue().intValue(); 113 | dataItem = map.get(new UnicodeString(MSGID_KEY)); 114 | msgID = ((Number) dataItem).getValue().intValue(); 115 | 116 | dataItem = map.get(new UnicodeString(TOPICS_KEY)); 117 | List topicDataItems = ((Array) dataItem).getDataItems(); 118 | for (DataItem topicDataItem : topicDataItems) { 119 | topics.add(((UnicodeString) topicDataItem).getString()); 120 | } 121 | dataItem = map.get(new UnicodeString(QOSS_KEY)); 122 | List qosDataItems = ((Array) dataItem).getDataItems(); 123 | for (DataItem qosDataItem : qosDataItems) { 124 | qoSs.add(((UnsignedInteger) qosDataItem).getValue().intValue()); 125 | } 126 | return true; 127 | } catch (CborException e) { 128 | Log.e(TAG,"Failed to decode.", e); 129 | return false; 130 | } catch (IndexOutOfBoundsException e) { 131 | return false; 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/Unsuback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | 22 | import co.nstant.in.cbor.CborBuilder; 23 | import co.nstant.in.cbor.CborEncoder; 24 | import co.nstant.in.cbor.CborException; 25 | 26 | /** 27 | * This class represents the MQTT UNSUBACK message. 28 | */ 29 | public class Unsuback { 30 | private static final String TAG = "MqttUnsubscribe"; 31 | private static final String TYPE_KEY = "w"; 32 | private static final String MSGID_KEY = "i"; 33 | /** 34 | * MQTT message type. 35 | */ 36 | public int type; 37 | /** 38 | * MQTT message ID. 39 | */ 40 | public int msgID; 41 | 42 | public byte[] encode() { 43 | byte[] unsubackBytes = null; 44 | try { 45 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 46 | new CborEncoder(baos).encode(new CborBuilder() 47 | .addMap() 48 | .put(TYPE_KEY, type) 49 | .put(MSGID_KEY, msgID) 50 | .end() 51 | .build()); 52 | unsubackBytes = baos.toByteArray(); 53 | } catch (CborException e) { 54 | Log.e(TAG, "Failed to encode.", e); 55 | } 56 | return unsubackBytes; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/mqttproxy/Unsubscribe.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.mqttproxy; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.io.ByteArrayOutputStream; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | import co.nstant.in.cbor.CborBuilder; 26 | import co.nstant.in.cbor.CborDecoder; 27 | import co.nstant.in.cbor.CborEncoder; 28 | import co.nstant.in.cbor.CborException; 29 | import co.nstant.in.cbor.builder.ArrayBuilder; 30 | import co.nstant.in.cbor.builder.MapBuilder; 31 | import co.nstant.in.cbor.model.Array; 32 | import co.nstant.in.cbor.model.DataItem; 33 | import co.nstant.in.cbor.model.Map; 34 | import co.nstant.in.cbor.model.Number; 35 | import co.nstant.in.cbor.model.UnicodeString; 36 | import co.nstant.in.cbor.model.UnsignedInteger; 37 | 38 | /** 39 | * This class represents the MQTT UNSUBSCRIBE message. 40 | */ 41 | public class Unsubscribe { 42 | private static final String TAG = "MqttUnsubscribe"; 43 | private static final String TYPE_KEY = "w"; 44 | private static final String TOPICS_KEY = "v"; 45 | private static final String MSGID_KEY = "i"; 46 | /** 47 | * MQTT message type. 48 | */ 49 | public int type; 50 | /** 51 | * Arrary of topics to unsubscribe. 52 | */ 53 | public List topics = new ArrayList<>(); 54 | /** 55 | * MQTT message ID. 56 | */ 57 | public int msgID; 58 | 59 | public String toString() { 60 | StringBuilder stringBuilder = new StringBuilder(); 61 | stringBuilder.append("UnSubscribe message: "); 62 | stringBuilder.append("\n type: " + type); 63 | stringBuilder.append("\n msgId: " + msgID); 64 | for (int i = 0; i < topics.size(); i++) { 65 | stringBuilder.append("\n topic: " + topics.get(i)); 66 | } 67 | return stringBuilder.toString(); 68 | } 69 | 70 | public byte[] encode() { 71 | byte[] unsubscribeBytes = null; 72 | try { 73 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 74 | 75 | ArrayBuilder> topicsArray = new CborBuilder() 76 | .addMap() 77 | .put(TYPE_KEY, type) 78 | .putArray(TOPICS_KEY); 79 | 80 | for (String topic : topics) { 81 | topicsArray = topicsArray.add(topic); 82 | } 83 | 84 | List cbordata = topicsArray.end().end().build(); 85 | new CborEncoder(baos).encode(cbordata); 86 | unsubscribeBytes = baos.toByteArray(); 87 | } catch (CborException e) { 88 | Log.e(TAG, "Failed to encode.", e); 89 | } 90 | return unsubscribeBytes; 91 | } 92 | 93 | public boolean decode(byte[] cborEncodedBytes) { 94 | ByteArrayInputStream bais = new ByteArrayInputStream(cborEncodedBytes); 95 | try { 96 | List dataItems = new CborDecoder(bais).decode(); 97 | // process data item 98 | Map map = (Map) dataItems.get(0); 99 | DataItem dataItem = map.get(new UnicodeString(TYPE_KEY)); 100 | type = ((UnsignedInteger) dataItem).getValue().intValue(); 101 | dataItem = map.get(new UnicodeString(MSGID_KEY)); 102 | msgID = ((Number) dataItem).getValue().intValue(); 103 | 104 | dataItem = map.get(new UnicodeString(TOPICS_KEY)); 105 | List topicDataItems = ((Array) dataItem).getDataItems(); 106 | for (DataItem topicDataItem : topicDataItems) { 107 | topics.add(((UnicodeString) topicDataItem).getString()); 108 | } 109 | return true; 110 | } catch (CborException e) { 111 | Log.e(TAG,"Failed to decode.", e); 112 | return false; 113 | } catch (IndexOutOfBoundsException e) { 114 | return false; 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/networkconfig/DeleteNetworkReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.networkconfig; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | 22 | import co.nstant.in.cbor.CborBuilder; 23 | import co.nstant.in.cbor.CborEncoder; 24 | import co.nstant.in.cbor.CborException; 25 | 26 | import static software.amazon.freertos.amazonfreertossdk.AmazonFreeRTOSConstants.DELETE_NETWORK_REQ; 27 | 28 | /** 29 | * Delete network request. 30 | */ 31 | public class DeleteNetworkReq { 32 | /** 33 | * The index of the saved network to be deleted. 34 | */ 35 | public int index; 36 | 37 | private static final String TAG = "DeleteNetworkRequest"; 38 | private static final String INDEX_KEY = "g"; 39 | private static final String TYPE_KEY = "w"; 40 | 41 | public byte[] encode() { 42 | byte[] DeleteNetworkRequestBytes = null; 43 | try { 44 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 45 | new CborEncoder(baos).encode(new CborBuilder() 46 | .addMap() 47 | .put(TYPE_KEY, DELETE_NETWORK_REQ) 48 | .put(INDEX_KEY, index) 49 | .end() 50 | .build()); 51 | DeleteNetworkRequestBytes = baos.toByteArray(); 52 | } catch (CborException e) { 53 | Log.e(TAG, "Failed to encode.", e); 54 | } 55 | return DeleteNetworkRequestBytes; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/networkconfig/DeleteNetworkResp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.networkconfig; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.util.List; 22 | 23 | import co.nstant.in.cbor.CborDecoder; 24 | import co.nstant.in.cbor.CborException; 25 | import co.nstant.in.cbor.model.DataItem; 26 | import co.nstant.in.cbor.model.Map; 27 | import co.nstant.in.cbor.model.UnicodeString; 28 | import co.nstant.in.cbor.model.UnsignedInteger; 29 | import lombok.Getter; 30 | 31 | /** 32 | * Delete network response 33 | */ 34 | @Getter 35 | public class DeleteNetworkResp { 36 | /** 37 | * Status of the operation. 0 for success. 38 | */ 39 | int status; 40 | public String toString() { 41 | return String.format("DeleteNetworkResponse ->\n status: %d", status); 42 | } 43 | 44 | private static final String TAG = "DeleteNetworkResponse"; 45 | private static final String STATUS_KEY = "s"; 46 | 47 | public boolean decode(byte[] cborEncodedBytes) { 48 | ByteArrayInputStream bais = new ByteArrayInputStream(cborEncodedBytes); 49 | try { 50 | List dataItems = new CborDecoder(bais).decode(); 51 | // process data item 52 | Map map = (Map) dataItems.get(0); 53 | DataItem dataItem = map.get(new UnicodeString(STATUS_KEY)); 54 | status = ((UnsignedInteger) dataItem).getValue().intValue(); 55 | return true; 56 | } catch (CborException e) { 57 | Log.e(TAG,"Failed to decode.", e); 58 | return false; 59 | } catch (IndexOutOfBoundsException e) { 60 | return false; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/networkconfig/EditNetworkReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.networkconfig; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | 22 | import co.nstant.in.cbor.CborBuilder; 23 | import co.nstant.in.cbor.CborEncoder; 24 | import co.nstant.in.cbor.CborException; 25 | 26 | import static software.amazon.freertos.amazonfreertossdk.AmazonFreeRTOSConstants.EDIT_NETWORK_REQ; 27 | 28 | /** 29 | * Edit network request 30 | */ 31 | public class EditNetworkReq { 32 | /** 33 | * The index of the saved network to be edited. 34 | */ 35 | public int index; 36 | /** 37 | * The new index of the saved network. Must be one of the existing indices of saved networks. 38 | */ 39 | public int newIndex; 40 | 41 | private static final String TAG = "EditNetworkRequest"; 42 | private static final String INDEX_KEY = "g"; 43 | private static final String NEWINDEX_KEY = "j"; 44 | private static final String TYPE_KEY = "w"; 45 | 46 | public byte[] encode() { 47 | byte[] EditNetworkRequestBytes = null; 48 | try { 49 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 50 | new CborEncoder(baos).encode(new CborBuilder() 51 | .addMap() 52 | .put(TYPE_KEY, EDIT_NETWORK_REQ) 53 | .put(INDEX_KEY, index) 54 | .put(NEWINDEX_KEY, newIndex) 55 | .end() 56 | .build()); 57 | EditNetworkRequestBytes = baos.toByteArray(); 58 | } catch (CborException e) { 59 | Log.e(TAG, "Failed to encode.", e); 60 | } 61 | return EditNetworkRequestBytes; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/networkconfig/EditNetworkResp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.networkconfig; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.util.List; 22 | 23 | import co.nstant.in.cbor.CborDecoder; 24 | import co.nstant.in.cbor.CborException; 25 | import co.nstant.in.cbor.model.DataItem; 26 | import co.nstant.in.cbor.model.Map; 27 | import co.nstant.in.cbor.model.UnicodeString; 28 | import co.nstant.in.cbor.model.UnsignedInteger; 29 | import lombok.Getter; 30 | 31 | /** 32 | * Edit network response 33 | */ 34 | @Getter 35 | public class EditNetworkResp { 36 | /** 37 | * Status of the operation. 0 for success. 38 | */ 39 | int status; 40 | public String toString() { 41 | return String.format("EditNetworkResponse ->\n status: %d", status); 42 | } 43 | 44 | private static final String TAG = "EditNetworkResponse"; 45 | private static final String STATUS_KEY = "s"; 46 | 47 | public boolean decode(byte[] cborEncodedBytes) { 48 | ByteArrayInputStream bais = new ByteArrayInputStream(cborEncodedBytes); 49 | try { 50 | List dataItems = new CborDecoder(bais).decode(); 51 | // process data item 52 | Map map = (Map) dataItems.get(0); 53 | DataItem dataItem = map.get(new UnicodeString(STATUS_KEY)); 54 | status = ((UnsignedInteger) dataItem).getValue().intValue(); 55 | return true; 56 | } catch (CborException e) { 57 | Log.e(TAG,"Failed to decode.", e); 58 | return false; 59 | } catch (IndexOutOfBoundsException e) { 60 | return false; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/networkconfig/ListNetworkReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.networkconfig; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | 22 | import co.nstant.in.cbor.CborBuilder; 23 | import co.nstant.in.cbor.CborEncoder; 24 | import co.nstant.in.cbor.CborException; 25 | 26 | import static software.amazon.freertos.amazonfreertossdk.AmazonFreeRTOSConstants.LIST_NETWORK_REQ; 27 | 28 | /** 29 | * List network request 30 | */ 31 | public class ListNetworkReq { 32 | /** 33 | * Maximum total number of networks to return. 34 | */ 35 | public int maxNetworks; 36 | /** 37 | * Time in seconds for BLE device to scan available networks. 38 | */ 39 | public int timeout; 40 | 41 | private static final String TAG = "ListNetworkRequest"; 42 | private static final String MAXNETWORKS_KEY = "h"; 43 | private static final String TIMEOUT_KEY = "t"; 44 | private static final String TYPE_KEY = "w"; 45 | 46 | public byte[] encode() { 47 | byte[] ListNetworkRequestBytes = null; 48 | try { 49 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 50 | new CborEncoder(baos).encode(new CborBuilder() 51 | .addMap() 52 | .put(TYPE_KEY, LIST_NETWORK_REQ) 53 | .put(MAXNETWORKS_KEY, maxNetworks) 54 | .put(TIMEOUT_KEY, timeout) 55 | .end() 56 | .build()); 57 | ListNetworkRequestBytes = baos.toByteArray(); 58 | } catch (CborException e) { 59 | Log.e(TAG, "Failed to encode.", e); 60 | } 61 | return ListNetworkRequestBytes; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/networkconfig/ListNetworkResp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.networkconfig; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.util.Formatter; 22 | import java.util.List; 23 | 24 | import co.nstant.in.cbor.CborDecoder; 25 | import co.nstant.in.cbor.CborException; 26 | import co.nstant.in.cbor.model.ByteString; 27 | import co.nstant.in.cbor.model.DataItem; 28 | import co.nstant.in.cbor.model.Map; 29 | import co.nstant.in.cbor.model.NegativeInteger; 30 | import co.nstant.in.cbor.model.Number; 31 | import co.nstant.in.cbor.model.SimpleValue; 32 | import co.nstant.in.cbor.model.SimpleValueType; 33 | import co.nstant.in.cbor.model.UnicodeString; 34 | import co.nstant.in.cbor.model.UnsignedInteger; 35 | import lombok.Getter; 36 | 37 | /** 38 | * List network response 39 | */ 40 | @Getter 41 | public class ListNetworkResp { 42 | private static final String TAG = "ListNetworkResponse"; 43 | private static final String STATUS_KEY = "s"; 44 | private static final String SSID_KEY = "r"; 45 | private static final String BSSID_KEY = "b"; 46 | private static final String SECURITY_KEY = "q"; 47 | private static final String HIDDEN_KEY = "f"; 48 | private static final String RSSI_KEY = "p"; 49 | private static final String CONNECTED_KEY = "e"; 50 | private static final String INDEX_KEY = "g"; 51 | private static final String LAST_NETWORK_KEY = "l"; 52 | /** 53 | * Status of the operation. 0 for success. 54 | */ 55 | private int status; 56 | /** 57 | * SSID of the scanned network. 58 | */ 59 | private String ssid; 60 | /** 61 | * BSSID of the scanned network. 62 | */ 63 | private byte[] bssid; 64 | /** 65 | * Network security type. 66 | */ 67 | private int security; 68 | /** 69 | * Whether the network is hidden. 70 | */ 71 | private Boolean hidden; 72 | /** 73 | * RSSI value of the scanned network. 74 | */ 75 | private int rssi; 76 | /** 77 | * Whether BLE device is connected to this network. 78 | */ 79 | private Boolean connected; 80 | /** 81 | * The index of this network. Index is used to indicate the connection preference of each saved 82 | * network. For non-saved networks, the index is negative. 83 | */ 84 | private int index; 85 | 86 | /** 87 | * Whether this network is the last in the list. 88 | */ 89 | private Boolean last = false; 90 | 91 | public String toString() { 92 | return String.format("List network response -> Status: %d ssid: %s bssid: %s security: %d hidden: %s" + 93 | " rssi: %d connected: %s index: %d, last :%s", status, ssid, bytesToHexString(bssid), security, 94 | hidden ? "true":"false", rssi, connected ? "true":"false", index, last ? "true" : "false"); 95 | } 96 | 97 | public boolean decode(byte[] cborEncodedBytes) { 98 | ByteArrayInputStream bais = new ByteArrayInputStream(cborEncodedBytes); 99 | try { 100 | List dataItems = new CborDecoder(bais).decode(); 101 | // process data item 102 | Map map = (Map) dataItems.get(0); 103 | DataItem dataItem = map.get(new UnicodeString(STATUS_KEY)); 104 | status = ((UnsignedInteger) dataItem).getValue().intValue(); 105 | dataItem = map.get(new UnicodeString(SSID_KEY)); 106 | ssid = ((UnicodeString) dataItem).getString(); 107 | dataItem = map.get(new UnicodeString(BSSID_KEY)); 108 | bssid = ((ByteString) dataItem).getBytes(); 109 | dataItem = map.get(new UnicodeString(SECURITY_KEY)); 110 | security = ((UnsignedInteger) dataItem).getValue().intValue(); 111 | dataItem = map.get(new UnicodeString(HIDDEN_KEY)); 112 | hidden = (((SimpleValue) dataItem).getSimpleValueType() == SimpleValueType.TRUE) ? true : false; 113 | dataItem = map.get(new UnicodeString(RSSI_KEY)); 114 | rssi = ((NegativeInteger) dataItem).getValue().intValue(); 115 | dataItem = map.get(new UnicodeString(CONNECTED_KEY)); 116 | connected = (((SimpleValue) dataItem).getSimpleValueType() == SimpleValueType.TRUE) ? true : false; 117 | dataItem = map.get(new UnicodeString(INDEX_KEY)); 118 | index = ((Number) dataItem).getValue().intValue(); 119 | final UnicodeString lastNetworkKey = new UnicodeString(LAST_NETWORK_KEY); 120 | if(map.getKeys().contains(lastNetworkKey)) { 121 | dataItem = map.get(lastNetworkKey); 122 | last = (((SimpleValue) dataItem).getSimpleValueType() == SimpleValueType.TRUE); 123 | } 124 | return true; 125 | } catch (CborException e) { 126 | Log.e(TAG,"Failed to decode.", e); 127 | return false; 128 | } catch (IndexOutOfBoundsException e) { 129 | return false; 130 | } 131 | } 132 | 133 | private static String bytesToHexString(byte[] bytes) { 134 | StringBuilder sb = new StringBuilder(bytes.length * 2); 135 | Formatter formatter = new Formatter(sb); 136 | for (int i =0; i< bytes.length; i++) { 137 | formatter.format("%02x", bytes[i]); 138 | if(i < bytes.length -1) 139 | formatter.format(":"); 140 | } 141 | return sb.toString(); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/networkconfig/SaveNetworkReq.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.networkconfig; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayOutputStream; 21 | 22 | import co.nstant.in.cbor.CborBuilder; 23 | import co.nstant.in.cbor.CborEncoder; 24 | import co.nstant.in.cbor.CborException; 25 | 26 | import static software.amazon.freertos.amazonfreertossdk.AmazonFreeRTOSConstants.SAVE_NETWORK_REQ; 27 | 28 | /** 29 | * Save network request. 30 | */ 31 | public class SaveNetworkReq { 32 | /** 33 | * SSID of the network to be saved. 34 | */ 35 | public String ssid; 36 | /** 37 | * BSSID of the network to be saved. 38 | */ 39 | public byte[] bssid; 40 | /** 41 | * Password of the network to be saved. 42 | */ 43 | public String psk; 44 | /** 45 | * Network security type. 46 | */ 47 | public int security; 48 | /** 49 | * Current index of the network to be saved. 50 | */ 51 | public int index; 52 | /** 53 | * Connect immediately or just save for later. 54 | */ 55 | public boolean connect = true; 56 | 57 | private static final String TAG = "SaveNetworkRequest"; 58 | private static final String INDEX_KEY = "g"; 59 | private static final String SSID_KEY = "r"; 60 | private static final String BSSID_KEY = "b"; 61 | private static final String PSK_KEY = "m"; 62 | private static final String SECURITY_KEY = "q"; 63 | private static final String TYPE_KEY = "w"; 64 | private static final String CONNECT_KEY = "y"; 65 | 66 | public byte[] encode() { 67 | byte[] SaveNetworkRequestBytes = null; 68 | try { 69 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 70 | new CborEncoder(baos).encode(new CborBuilder() 71 | .addMap() 72 | .put(TYPE_KEY, SAVE_NETWORK_REQ) 73 | .put(INDEX_KEY, index) 74 | .put(SSID_KEY, ssid) 75 | .put(BSSID_KEY, bssid) 76 | .put(PSK_KEY, psk) 77 | .put(SECURITY_KEY, security) 78 | .put(CONNECT_KEY, connect) 79 | .end() 80 | .build()); 81 | SaveNetworkRequestBytes = baos.toByteArray(); 82 | } catch (CborException e) { 83 | Log.e(TAG, "Failed to encode.", e); 84 | } 85 | return SaveNetworkRequestBytes; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/networkconfig/SaveNetworkResp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"). 5 | * You may not use this file except in compliance with the License. 6 | * A copy of the License is located at 7 | * 8 | * http://aws.amazon.com/apache2.0 9 | * 10 | * or in the "license" file accompanying this file. This file is distributed 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 | * express or implied. See the License for the specific language governing 13 | * permissions and limitations under the License. 14 | */ 15 | 16 | package software.amazon.freertos.amazonfreertossdk.networkconfig; 17 | 18 | import android.util.Log; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.util.List; 22 | 23 | import co.nstant.in.cbor.CborDecoder; 24 | import co.nstant.in.cbor.CborException; 25 | import co.nstant.in.cbor.model.DataItem; 26 | import co.nstant.in.cbor.model.Map; 27 | 28 | import co.nstant.in.cbor.model.UnicodeString; 29 | import co.nstant.in.cbor.model.UnsignedInteger; 30 | import lombok.Getter; 31 | 32 | /** 33 | * Save network response 34 | */ 35 | @Getter 36 | public class SaveNetworkResp { 37 | /** 38 | * Status of the operation. 0 for success. 39 | */ 40 | int status; 41 | public String toString() { 42 | return String.format("SaveNetworkResponse ->\n status: %d", status); 43 | } 44 | 45 | private static final String TAG = "SaveNetworkResponse"; 46 | private static final String STATUS_KEY = "s"; 47 | 48 | public boolean decode(byte[] cborEncodedBytes) { 49 | ByteArrayInputStream bais = new ByteArrayInputStream(cborEncodedBytes); 50 | try { 51 | List dataItems = new CborDecoder(bais).decode(); 52 | // process data item 53 | Map map = (Map) dataItems.get(0); 54 | DataItem dataItem = map.get(new UnicodeString(STATUS_KEY)); 55 | status = ((UnsignedInteger) dataItem).getValue().intValue(); 56 | return true; 57 | } catch (CborException e) { 58 | Log.e(TAG,"Failed to decode.", e); 59 | return false; 60 | } catch (IndexOutOfBoundsException e) { 61 | return false; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AmazonFreeRTOSsdk 3 | 4 | -------------------------------------------------------------------------------- /amazonfreertossdk/src/test/java/software/amazon/freertos/amazonfreertossdk/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.amazonfreertossdk; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/README.md: -------------------------------------------------------------------------------- 1 | # FreeRTOS BLE Mobile SDK for Android: Demo Application 2 | 3 | ## Introduction 4 | 5 | This demo application demonstrates some of the features of the Android SDK for FreeRTOS Bluetooth devices. 6 | 7 | For information about configuring and using the demo application, see [FreeRTOS BLE Mobile SDK Demo Application](https://docs.aws.amazon.com/freertos/latest/userguide/ble-demo.html#ble-sdk-app). 8 | 9 | ## Usage 10 | 11 | If you don't need the mqtt proxy feature, set boolean `mqttOn` to `false` in `DeviceScanFragment.java`, then skip to step 2. 12 | 1. Replace the constants in `DemoConstants.java` and `res/raw/awsconfiguration.json`. 13 | 2. After you configure the demo app, you can build and install the demo. To build the app from the command line, use the following command: 14 | 15 | ``` 16 | ./gradlew installDebug 17 | ``` 18 | 19 | You can also build the app in Android Studio, after enabling "USB debugging" on your Android device. 20 | 21 | 3. Start the demo app. Register account and sign-in if sign-in page appears. 22 | 4. Click on the "Scan" button to scan for nearby BLE devices. 23 | 5. Toggle the switch to connect to one of them. "More..." menu will be enabled after connection is successful 24 | 6. Click on the "More..." menu for WiFi provisioning. 25 | 26 | **Note** 27 | 28 | 29 | 1. When WiFi provisioning, make sure that you wait for the app to finish refreshing after each network list/save/edit/delete operation. 30 | 2. The current MTU size shows "N/A". This is a limitation of the current version of the app. 31 | 32 | ## License 33 | 34 | This library is licensed under the Apache 2.0 License. 35 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "software.amazon.freertos.demo" 7 | minSdkVersion 23 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation ('software.amazon.freertos:amazonfreertossdk:1.5.0') 23 | implementation('com.amazonaws:aws-android-sdk-mobile-client:2.13.5') 24 | implementation('com.amazonaws:aws-android-sdk-auth-userpools:2.13.5') 25 | implementation('com.amazonaws:aws-android-sdk-auth-ui:2.13.5') 26 | implementation 'com.amazonaws:aws-android-sdk-iot:2.13.5' 27 | implementation fileTree(include: ['*.jar'], dir: 'libs') 28 | implementation 'com.android.support:support-v4:26.1.0' 29 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 30 | implementation 'android.arch.lifecycle:extensions:1.1.1' 31 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { 32 | exclude group: 'com.android.support', module: 'support-annotations' 33 | }) 34 | implementation 'com.android.support:appcompat-v7:26.1.0' 35 | implementation 'com.android.support:recyclerview-v7:26.1.0' 36 | implementation 'com.android.support:cardview-v7:26.1.0' 37 | testImplementation 'junit:junit:4.12' 38 | compileOnly 'org.projectlombok:lombok:1.18.4' 39 | annotationProcessor 'org.projectlombok:lombok:1.18.4' 40 | } 41 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 18 | 19 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /app/src/main/java/software/amazon/freertos/demo/AmazonFreeRTOSAgent.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.demo; 2 | 3 | import android.bluetooth.BluetoothAdapter; 4 | import android.bluetooth.BluetoothManager; 5 | import android.content.Context; 6 | 7 | import software.amazon.freertos.amazonfreertossdk.AmazonFreeRTOSManager; 8 | 9 | /** 10 | * A wrapper class to get the instance of AmazonFreeRTOSManager. 11 | */ 12 | public class AmazonFreeRTOSAgent { 13 | private static AmazonFreeRTOSManager sAmazonFreeRTOSManager; 14 | private static BluetoothAdapter sBluetoothAdapter; 15 | private static final String TAG = "AmazonFreeRTOSAgent"; 16 | 17 | /** 18 | * Return the instance of AmazonFreeRTOSManager. Initialize AmazonFreeRTOSManager if this is 19 | * called for the first time. 20 | * Bluetooth must be enabled before calling this method to get AmazonFreeRTOSManager. 21 | * @param context 22 | * @return The instance of AmazonFreeRTOSManager. 23 | */ 24 | public static AmazonFreeRTOSManager getAmazonFreeRTOSManager(Context context) { 25 | if (sAmazonFreeRTOSManager == null) { 26 | BluetoothManager bluetoothManager 27 | = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); 28 | sBluetoothAdapter = bluetoothManager.getAdapter(); 29 | sAmazonFreeRTOSManager = new AmazonFreeRTOSManager(context, sBluetoothAdapter); 30 | } 31 | return sAmazonFreeRTOSManager; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/software/amazon/freertos/demo/AuthenticatorActivity.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.demo; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.os.Handler; 7 | import android.os.HandlerThread; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.util.Log; 10 | 11 | import com.amazonaws.auth.AWSCredentialsProvider; 12 | import com.amazonaws.mobile.client.AWSMobileClient; 13 | import com.amazonaws.mobile.client.Callback; 14 | import com.amazonaws.mobile.client.SignInUIOptions; 15 | import com.amazonaws.mobile.client.UserStateDetails; 16 | import com.amazonaws.regions.Region; 17 | import com.amazonaws.services.iot.AWSIotClient; 18 | import com.amazonaws.services.iot.model.AttachPolicyRequest; 19 | 20 | import java.util.concurrent.CountDownLatch; 21 | 22 | public class AuthenticatorActivity extends AppCompatActivity { 23 | private final static String TAG = "AuthActivity"; 24 | private HandlerThread handlerThread; 25 | private Handler handler; 26 | public static Intent newIntent(Context packageContext) { 27 | Intent intent = new Intent(packageContext, AuthenticatorActivity.class); 28 | return intent; 29 | } 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.activity_fragment); 35 | if ( handlerThread == null ) { 36 | handlerThread = new HandlerThread("SignInThread"); 37 | handlerThread.start(); 38 | handler = new Handler(handlerThread.getLooper()); 39 | } 40 | final CountDownLatch latch = new CountDownLatch(1); 41 | AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback() { 42 | 43 | @Override 44 | public void onResult(UserStateDetails userStateDetails) { 45 | Log.i(TAG, "AWSMobileClient initialization onResult: " + userStateDetails.getUserState()); 46 | latch.countDown(); 47 | } 48 | 49 | @Override 50 | public void onError(Exception e) { 51 | Log.e(TAG, "Initialization error.", e); 52 | } 53 | } 54 | ); 55 | Log.d(TAG, "waiting for AWSMobileClient Initialization"); 56 | try { 57 | latch.await(); 58 | } catch (InterruptedException e) { 59 | e.printStackTrace(); 60 | } 61 | 62 | handler.post(new Runnable() { 63 | @Override 64 | public void run() { 65 | if (AWSMobileClient.getInstance().isSignedIn()) { 66 | signinsuccessful(); 67 | finish(); 68 | } else { 69 | AWSMobileClient.getInstance().showSignIn( 70 | AuthenticatorActivity.this, 71 | SignInUIOptions.builder() 72 | .nextActivity(null) 73 | .build(), 74 | new Callback() { 75 | @Override 76 | public void onResult(UserStateDetails result) { 77 | Log.d(TAG, "onResult: " + result.getUserState()); 78 | switch (result.getUserState()) { 79 | case SIGNED_IN: 80 | Log.i(TAG, "logged in!"); 81 | signinsuccessful(); 82 | finish(); 83 | break; 84 | case SIGNED_OUT: 85 | Log.i(TAG, "onResult: User did not choose to sign-in"); 86 | break; 87 | default: 88 | AWSMobileClient.getInstance().signOut(); 89 | break; 90 | } 91 | } 92 | 93 | @Override 94 | public void onError(Exception e) { 95 | Log.e(TAG, "onError: ", e); 96 | } 97 | } 98 | ); 99 | } 100 | } 101 | }); 102 | 103 | } 104 | 105 | private void signinsuccessful() { 106 | try { 107 | AWSCredentialsProvider credentialsProvider = AWSMobileClient.getInstance(); 108 | AWSIotClient awsIotClient = new AWSIotClient(credentialsProvider); 109 | awsIotClient.setRegion(Region.getRegion(DemoConstants.AWS_IOT_REGION)); 110 | 111 | AttachPolicyRequest attachPolicyRequest = new AttachPolicyRequest() 112 | .withPolicyName(DemoConstants.AWS_IOT_POLICY_NAME) 113 | .withTarget(AWSMobileClient.getInstance().getIdentityId()); 114 | awsIotClient.attachPolicy(attachPolicyRequest); 115 | Log.i(TAG, "Iot policy attached successfully."); 116 | } catch (Exception e) { 117 | Log.e(TAG, "Exception caught: ", e); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /app/src/main/java/software/amazon/freertos/demo/BleDevice.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.demo; 2 | 3 | import android.bluetooth.BluetoothDevice; 4 | 5 | import java.util.Objects; 6 | 7 | public class BleDevice { 8 | private String name; 9 | private String macAddr; 10 | private BluetoothDevice mBluetoothDevice; 11 | 12 | public BleDevice(String name, String macAddr, BluetoothDevice bluetoothDevice) { 13 | this.name = name; 14 | this.macAddr = macAddr; 15 | mBluetoothDevice = bluetoothDevice; 16 | } 17 | 18 | public BluetoothDevice getBluetoothDevice() { 19 | return mBluetoothDevice; 20 | } 21 | 22 | public void setBluetoothDevice(BluetoothDevice bluetoothDevice) { 23 | mBluetoothDevice = bluetoothDevice; 24 | } 25 | 26 | public String getName() { 27 | return name; 28 | } 29 | 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | 34 | public String getMacAddr() { 35 | return macAddr; 36 | } 37 | 38 | public void setMacAddr(String macAddr) { 39 | this.macAddr = macAddr; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | BleDevice bleDevice = (BleDevice) o; 47 | return Objects.equals(getMacAddr(), bleDevice.getMacAddr()); 48 | } 49 | 50 | @Override 51 | public int hashCode() { 52 | 53 | return Objects.hash(getName(), getMacAddr(), getBluetoothDevice()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /app/src/main/java/software/amazon/freertos/demo/DemoConstants.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.demo; 2 | 3 | public class DemoConstants { 4 | /* 5 | * Replace with your AWS IoT policy name. 6 | */ 7 | final static String AWS_IOT_POLICY_NAME = "Your AWS IoT policy name"; 8 | /* 9 | * Replace with your AWS IoT region, eg: us-west-2. 10 | */ 11 | final static String AWS_IOT_REGION = "us-west-2"; 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/software/amazon/freertos/demo/DeviceScanActivity.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.demo; 2 | 3 | import android.support.v4.app.Fragment; 4 | 5 | public class DeviceScanActivity extends SingleFragmentActivity { 6 | @Override 7 | protected Fragment createFragment() { 8 | return new DeviceScanFragment(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/java/software/amazon/freertos/demo/SingleFragmentActivity.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.demo; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v4.app.FragmentManager; 7 | import android.support.v7.app.AppCompatActivity; 8 | 9 | public abstract class SingleFragmentActivity extends AppCompatActivity { 10 | protected abstract Fragment createFragment(); 11 | 12 | @Override 13 | protected void onCreate(@Nullable Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_fragment); 16 | FragmentManager fm = getSupportFragmentManager(); 17 | Fragment fragment = fm.findFragmentById(R.id.fragment_container); 18 | if (fragment == null) { 19 | fragment = createFragment(); 20 | fm.beginTransaction().add(R.id.fragment_container, fragment).commit(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/software/amazon/freertos/demo/WifiCredentialFragment.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.demo; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.app.Dialog; 6 | import android.content.DialogInterface; 7 | import android.content.Intent; 8 | import android.os.Bundle; 9 | import android.support.annotation.NonNull; 10 | import android.support.v4.app.DialogFragment; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.widget.EditText; 14 | import android.widget.TextView; 15 | 16 | public class WifiCredentialFragment extends DialogFragment { 17 | public static final String EXTRA_WIFI_PW = "com.amazon.freertos.wifipassword"; 18 | public static final String EXTRA_WIFI_BSSID = "com.amazon.freertos.wifibssid"; 19 | private static final String SSID = "ssid"; 20 | private static final String BSSID = "bssid"; 21 | private String mBssid; 22 | 23 | public static WifiCredentialFragment newInstance(String ssid, String bssid) { 24 | Bundle args = new Bundle(); 25 | args.putString(SSID, ssid); 26 | args.putString(BSSID, bssid); 27 | WifiCredentialFragment fragment = new WifiCredentialFragment(); 28 | fragment.setArguments(args); 29 | return fragment; 30 | } 31 | 32 | @NonNull 33 | @Override 34 | public Dialog onCreateDialog(Bundle savedInstanceState) { 35 | String ssid = getArguments().getString(SSID); 36 | mBssid = getArguments().getString(BSSID); 37 | View view = LayoutInflater.from(getActivity()).inflate(R.layout.wifi_credential_dialog, null); 38 | TextView ssidTextView = (TextView) view.findViewById(R.id.ssid_name); 39 | final EditText pwEditText = (EditText) view.findViewById(R.id.wifi_pw); 40 | ssidTextView.setText(ssid); 41 | return new AlertDialog.Builder(getActivity()) 42 | .setView(view) 43 | .setTitle(R.string.wifi_credential_dialog_title) 44 | .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 45 | @Override 46 | public void onClick(DialogInterface dialog, int which) { 47 | sendResult(Activity.RESULT_OK, pwEditText.getText().toString()); 48 | } 49 | }) 50 | .setNegativeButton(android.R.string.cancel, null) 51 | .create(); 52 | } 53 | 54 | private void sendResult(int resultCode, String result) { 55 | if (getTargetFragment() == null) { 56 | return; 57 | } 58 | Intent intent = new Intent(); 59 | intent.putExtra(EXTRA_WIFI_PW, result); 60 | intent.putExtra(EXTRA_WIFI_BSSID, mBssid); 61 | getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/software/amazon/freertos/demo/WifiInfo.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.demo; 2 | 3 | import java.util.Objects; 4 | 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | @Getter 9 | @Setter 10 | public class WifiInfo { 11 | private static String[] NETWORK_TYPES = {"Open", "WEP", "WPA", "WPA2", "WPA2-Enterprise", "WPA3", "Other"}; 12 | private String ssid; 13 | private byte[] bssid; 14 | private int rssi; 15 | private int networkType; 16 | private int index; 17 | private boolean connected; 18 | 19 | public String getNetworkTypeName() { 20 | return NETWORK_TYPES[networkType]; 21 | } 22 | 23 | public WifiInfo(String ssid, byte[] bssid, int rssi, int networkType, int index, boolean connected) { 24 | this.ssid = ssid; 25 | this.bssid = bssid; 26 | this.rssi = rssi; 27 | this.networkType = networkType; 28 | this.index = index; 29 | this.connected = connected; 30 | } 31 | 32 | @Override 33 | public boolean equals(Object obj) { 34 | if (this == obj) return true; 35 | if (obj == null || getClass() != obj.getClass()) return false; 36 | WifiInfo wifiInfo = (WifiInfo) obj; 37 | return Objects.equals(wifiInfo.getSsid(), ssid); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/software/amazon/freertos/demo/WifiProvisionActivity.java: -------------------------------------------------------------------------------- 1 | package software.amazon.freertos.demo; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.v4.app.Fragment; 6 | 7 | public class WifiProvisionActivity extends SingleFragmentActivity { 8 | public static final String EXTRA_DEVICE_MAC = "com.amazonaws.freertosandroid.device_mac"; 9 | @Override 10 | protected Fragment createFragment() { 11 | return new WifiProvisionFragment(); 12 | } 13 | 14 | public static Intent newIntent(Context packageContext, String macAddr) { 15 | Intent intent = new Intent(packageContext, WifiProvisionActivity.class); 16 | intent.putExtra(EXTRA_DEVICE_MAC, macAddr); 17 | return intent; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws/amazon-freertos-ble-android-sdk/1f61f341682a2b5736d246f4aab25dc488cad2f3/app/src/main/res/drawable-hdpi/ic_delete.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_menu_logout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws/amazon-freertos-ble-android-sdk/1f61f341682a2b5736d246f4aab25dc488cad2f3/app/src/main/res/drawable-hdpi/ic_menu_logout.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/ic_delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws/amazon-freertos-ble-android-sdk/1f61f341682a2b5736d246f4aab25dc488cad2f3/app/src/main/res/drawable-mdpi/ic_delete.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/ic_menu_logout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws/amazon-freertos-ble-android-sdk/1f61f341682a2b5736d246f4aab25dc488cad2f3/app/src/main/res/drawable-mdpi/ic_menu_logout.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws/amazon-freertos-ble-android-sdk/1f61f341682a2b5736d246f4aab25dc488cad2f3/app/src/main/res/drawable-xhdpi/ic_delete.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_menu_logout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws/amazon-freertos-ble-android-sdk/1f61f341682a2b5736d246f4aab25dc488cad2f3/app/src/main/res/drawable-xhdpi/ic_menu_logout.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws/amazon-freertos-ble-android-sdk/1f61f341682a2b5736d246f4aab25dc488cad2f3/app/src/main/res/drawable-xxhdpi/ic_delete.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_menu_logout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws/amazon-freertos-ble-android-sdk/1f61f341682a2b5736d246f4aab25dc488cad2f3/app/src/main/res/drawable-xxhdpi/ic_menu_logout.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxxhdpi/ic_delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws/amazon-freertos-ble-android-sdk/1f61f341682a2b5736d246f4aab25dc488cad2f3/app/src/main/res/drawable-xxxhdpi/ic_delete.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxxhdpi/ic_menu_logout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws/amazon-freertos-ble-android-sdk/1f61f341682a2b5736d246f4aab25dc488cad2f3/app/src/main/res/drawable-xxxhdpi/ic_menu_logout.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_fragment.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_device_scan.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 |