From 4366dda256380483fe647ae800da656fb02fe81c Mon Sep 17 00:00:00 2001 From: Daniel Carter Date: Fri, 8 Sep 2017 22:15:24 +0900 Subject: [PATCH] Initial commit. --- .gitignore | 16 + .travis.yml | 18 + CONTRIBUTING.md | 64 ++ ISSUE_TEMPLATE.md | 48 ++ PULL_REQUEST_TEMPLATE.md | 13 + README.md | 102 +++ RELEASE_NOTES.md | 27 + ROADMAP.md | 43 ++ app/.gitignore | 1 + app/LICENSE-3RD-PARTY.txt | 208 +++++ app/LICENSE.txt | 201 +++++ app/build.gradle | 103 +++ app/proguard-rules.pro | 17 + app/src/main/AndroidManifest.xml | 36 + .../hellobluetooth/BaseBluetoothActivity.java | 85 +++ .../hellobluetooth/DeviceListAdapter.java | 68 ++ .../hellobluetooth/DiscoveryActivity.java | 247 ++++++ .../HelloBluetoothApplication.java | 31 + .../android/hellobluetooth/MainActivity.java | 245 ++++++ .../hellobluetooth/NoopConnectionHandler.java | 51 ++ .../main/res/layout/activity_discovery.xml | 63 ++ app/src/main/res/layout/activity_main.xml | 96 +++ app/src/main/res/layout/device_list_item.xml | 14 + app/src/main/res/menu/main_menu.xml | 12 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10486 bytes app/src/main/res/values-w820dp/dimens.xml | 6 + app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/dimens.xml | 5 + app/src/main/res/values/strings.xml | 13 + app/src/main/res/values/styles.xml | 11 + build.gradle | 37 + gradle.properties | 20 + gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 53637 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 160 ++++ gradlew.bat | 90 +++ lib/.gitignore | 1 + lib/LICENSE-3RD-PARTY.txt | 283 +++++++ lib/LICENSE.txt | 201 +++++ lib/build.gradle | 115 +++ lib/libs/jcip-annotations.jar | Bin 0 -> 2250 bytes lib/proguard-rules.pro | 17 + lib/src/main/AndroidManifest.xml | 14 + .../trueblue/BluetoothStatusListener.java | 35 + .../coiney/android/trueblue/Connection.java | 135 ++++ .../trueblue/ConnectionAttemptCallback.java | 92 +++ .../ConnectionAttemptConfiguration.java | 239 ++++++ .../android/trueblue/ConnectionClient.java | 163 ++++ .../android/trueblue/ConnectionClients.java | 61 ++ .../trueblue/DeviceConnectionListener.java | 44 ++ .../android/trueblue/DiscoveryError.java | 75 ++ .../android/trueblue/DiscoveryListener.java | 53 ++ .../com/coiney/android/trueblue/TrueBlue.java | 719 ++++++++++++++++++ .../trueblue/internal/AdapterManager.java | 177 +++++ .../trueblue/internal/BluetoothCompat.java | 61 ++ .../internal/BluetoothStatusMonitor.java | 115 +++ .../trueblue/internal/BluetoothUtils.java | 91 +++ .../trueblue/internal/CloseableUtils.java | 50 ++ .../trueblue/internal/ConnectTask.java | 347 +++++++++ .../internal/ConnectionClientImpl.java | 210 +++++ .../internal/ConnectionConfiguration.java | 101 +++ .../trueblue/internal/ConnectionImpl.java | 173 +++++ .../trueblue/internal/ConnectionManager.java | 264 +++++++ .../trueblue/internal/ConnectionProxy.java | 279 +++++++ .../internal/ConnectionRetryPolicy.java | 157 ++++ .../trueblue/internal/ContextUtils.java | 47 ++ .../trueblue/internal/DiscoveryManager.java | 232 ++++++ .../android/trueblue/internal/Logger.java | 249 ++++++ .../trueblue/internal/PairingMonitor.java | 159 ++++ .../android/trueblue/internal/ReadThread.java | 97 +++ .../trueblue/internal/ThreadUtils.java | 48 ++ .../trueblue/internal/WriteThread.java | 110 +++ lib/src/main/res/values/strings.xml | 7 + .../android/trueblue/TrueBlueTests.java | 21 + .../internal/AdapterManagerTests.java | 223 ++++++ .../internal/BluetoothCompatTests.java | 31 + .../internal/BluetoothStatusMonitorTests.java | 180 +++++ .../internal/BluetoothUtilsTests.java | 154 ++++ .../internal/CloseableUtilsTests.java | 53 ++ .../trueblue/internal/ConnectTaskTests.java | 180 +++++ .../internal/ConnectionClientImplTests.java | 31 + .../ConnectionConfigurationTests.java | 30 + .../internal/ConnectionImplTests.java | 188 +++++ .../internal/ConnectionManagerTests.java | 96 +++ .../internal/ConnectionProxyTests.java | 174 +++++ .../internal/ConnectionRetryPolicyTests.java | 154 ++++ .../trueblue/internal/ContextUtilsTests.java | 29 + .../internal/DiscoveryManagerTests.java | 324 ++++++++ .../trueblue/internal/LoggerTests.java | 27 + .../internal/PairingMonitorTests.java | 188 +++++ .../trueblue/internal/ReadThreadTests.java | 31 + .../trueblue/internal/ThreadUtilsTests.java | 29 + .../trueblue/internal/WriteThreadTests.java | 31 + settings.gradle | 1 + 98 files changed, 9559 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 CONTRIBUTING.md create mode 100644 ISSUE_TEMPLATE.md create mode 100644 PULL_REQUEST_TEMPLATE.md create mode 100644 README.md create mode 100644 RELEASE_NOTES.md create mode 100644 ROADMAP.md create mode 100644 app/.gitignore create mode 100644 app/LICENSE-3RD-PARTY.txt create mode 100644 app/LICENSE.txt create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/coiney/android/hellobluetooth/BaseBluetoothActivity.java create mode 100644 app/src/main/java/com/coiney/android/hellobluetooth/DeviceListAdapter.java create mode 100644 app/src/main/java/com/coiney/android/hellobluetooth/DiscoveryActivity.java create mode 100644 app/src/main/java/com/coiney/android/hellobluetooth/HelloBluetoothApplication.java create mode 100644 app/src/main/java/com/coiney/android/hellobluetooth/MainActivity.java create mode 100644 app/src/main/java/com/coiney/android/hellobluetooth/NoopConnectionHandler.java create mode 100644 app/src/main/res/layout/activity_discovery.xml create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/layout/device_list_item.xml create mode 100644 app/src/main/res/menu/main_menu.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/values-w820dp/dimens.xml create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/dimens.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 lib/.gitignore create mode 100644 lib/LICENSE-3RD-PARTY.txt create mode 100644 lib/LICENSE.txt create mode 100644 lib/build.gradle create mode 100644 lib/libs/jcip-annotations.jar create mode 100644 lib/proguard-rules.pro create mode 100644 lib/src/main/AndroidManifest.xml create mode 100644 lib/src/main/java/com/coiney/android/trueblue/BluetoothStatusListener.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/Connection.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/ConnectionAttemptCallback.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/ConnectionAttemptConfiguration.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/ConnectionClient.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/ConnectionClients.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/DeviceConnectionListener.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/DiscoveryError.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/DiscoveryListener.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/TrueBlue.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/AdapterManager.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/BluetoothCompat.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/BluetoothStatusMonitor.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/BluetoothUtils.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/CloseableUtils.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/ConnectTask.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/ConnectionClientImpl.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/ConnectionConfiguration.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/ConnectionImpl.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/ConnectionManager.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/ConnectionProxy.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/ConnectionRetryPolicy.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/ContextUtils.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/DiscoveryManager.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/Logger.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/PairingMonitor.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/ReadThread.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/ThreadUtils.java create mode 100644 lib/src/main/java/com/coiney/android/trueblue/internal/WriteThread.java create mode 100644 lib/src/main/res/values/strings.xml create mode 100644 lib/src/test/java/com/coiney/android/trueblue/TrueBlueTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/AdapterManagerTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/BluetoothCompatTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/BluetoothStatusMonitorTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/BluetoothUtilsTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/CloseableUtilsTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/ConnectTaskTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/ConnectionClientImplTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/ConnectionConfigurationTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/ConnectionImplTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/ConnectionManagerTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/ConnectionProxyTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/ConnectionRetryPolicyTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/ContextUtilsTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/DiscoveryManagerTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/LoggerTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/PairingMonitorTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/ReadThreadTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/ThreadUtilsTests.java create mode 100644 lib/src/test/java/com/coiney/android/trueblue/internal/WriteThreadTests.java create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..12e03b9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +.DS_Store +*.keystore +.idea +*.iml +bin +gen +target +gen-external-apklibs +out +proguard_logs +build +.gradle +obj +*.apk +local.properties +captures diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..510eee0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +sudo: false + +language: android +jdk: oraclejdk8 + +before_cache: + - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock + - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ + +cache: + directories: + - $HOME/.gradle/caches/ + - $HOME/.gradle/wrapper/ + +script: + - mkdir -p $ANDROID_HOME/licenses + - echo "8933bad161af4178b1185d1a37fbf41ea5269c55" > $ANDROID_HOME/licenses/android-sdk-license + - ./gradlew --no-daemon clean lib:testRelease diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..ce34ee1 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,64 @@ +# Contributing + +Constructive contributions to the project are very welcome, provided they meet certain requirements. This document details the various ways in which it is possible to contribute, and the requirements for each. + +## Apparent Bug Reports + +Please file an issue in the Github repository [here](https://github.com/Coiney/TrueBlue/issues). + +The repository contains a default template which will be displayed when creating a new issue. Please follow the template to provide the required information. + +Please remember to be as detailed as possible when filing an issue. The more information we have, the more likely we are to be able to investigate and resolve it in a timely fashion. + +Further, wherever possible, please include sample code which provides a Minimum, Complete, and Verifiable example demonstrating the exact issue is very welcome and greatly appreciated. + +We strongly recommend that issues be filed in English. We will certainly do our best to respond to all issues regardless of language, but please understand that we may not be able to respond to issues filed in other languages in a timely fashion or even at all. + + +## Questions, Requests, etc. + +Please file an issue in the Github repository [here](https://github.com/Coiney/TrueBlue/issues). + +The repository contains a default template which will be displayed when creating a new issue. This template is designed primarily for the reporting of apparent bugs, so please do feel free to delete it and enter free text. + +As per apparent bug reports, we would ask that you provide as much information as possible, and strongly recommend that issues be filed in English. + + +## Code (Bug fixes, enhancments, features, etc.) + +We welcome any and all constructive code contributions to the project, provided they meet certain requirements. + +We always appreciate an issue being created before code is submitted. This helps give us the background we need to do things like confirm issues and decide whether or not a feature is something that fits in with the roadmap for this repository. + +If you'd like to contribute code you will need to file a pull request on the original repository. Pull requests must target the `develop` branch. Please see the "Branching" section below for more details on how to prepare and name branches for code changes. + +Please understand that due to time constraints and the roadmap for this repository, we cannot guarantee that all contributions will be accepted. + +### Code Style + +We have not defined a strict code style for the project yet, so please follow what you see in the repository as best you can. + +### Tests + +Wherever reasonably possible, please add/update the tests appropriately as part of the code you submit as a PR. + +### Javadoc + +All public classes, interfaces, enums etc. and any methods on them which are not located under the `internal` package must have Javadoc describing their purpose. + +As per our code style we have not yet defined a strict standard for Javadoc, so please follow what you see in the repository as best you can. + +All documentation must be prepared in English. Please do let us know if you require assistance preparing the documentation - we are more than happy to help. + +### Branching + +We use [git flow](http://nvie.com/posts/a-successful-git-branching-model/) when working on this repository, and highly recommend that contributors do too. + +When preparing a branch for contributing changes: + +1. Make sure that the `develop` branch in your cloned repository is up to date with the `develop` branch in the original repository. +2. Cut a branch from `develop` in your cloned repository with a name fitting the pattern `feature/`. + +The description must be in English and should provide a clear, conscise summary of the content of and/or reason for the code change that will be made in the branch. Please aim to keep the description to no more than 30 characters. + +When ready, raise a PR for the branch on the original repository with `develop` as the target branch. diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..170be48 --- /dev/null +++ b/ISSUE_TEMPLATE.md @@ -0,0 +1,48 @@ +### Library Information + +Version: + + +### Build Environment + +IDE: +Version: + +Build system: +Version: + + +#### Device Information (if applicable) + +Manufacturer: +Model: +OS version: + + +#### Steps to Reproduce + +1. + + +#### Expected Result + +1. + + +#### Actual Result + +1. + + +#### Probability + + + +#### Sample Code + +Please provide a link to a Minimal, Complete and Verifiable working example which clearly demonstrates the issue: + + + +#### Other Comments (if any) + diff --git a/PULL_REQUEST_TEMPLATE.md b/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..ed962cd --- /dev/null +++ b/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,13 @@ +#### Summary + + + +#### Issues + +Please link to any issues which this PR purports to close: + +Closes # + + +#### QA / Testing Methodology + diff --git a/README.md b/README.md new file mode 100644 index 0000000..028d215 --- /dev/null +++ b/README.md @@ -0,0 +1,102 @@ +# TrueBlue + +### BETA WARNING + +This library is current in beta. All APIs are subject to change without notice. + + +## Introduction + +TrueBlue is a small wrapper service around the Android Bluetooth subsystem. It is designed to make +interacting with Bluetooth on Android, which can be quite low level and frustrating at times, simpler. + +In summary, its functionality includes: + +1. Bluetooth adapter/subsystem management; +2. Bluetooth device pairing and connectivity management; and +3. Bluetooth discovery scanning. + +For the sake of clarity, the service does *not* currently support: + +* Bluetooth LE +* Bluetooth profiles +* Bluetooth device connectivity as a "server" (as opposed to as a client) + + +## Installation + +#### Gradle + +Coming soon ... + + +## Initialization + +Before the service can be used it must be initialized with a `Context`. This need (and generally should) only be done once, so one good place to do this is in an `Application` subclass (if you have one). For example: + +``` +public final class MyApplication extends Application { + + @Override + public void onCreate() { + super.onCreate(); + TrueBlue.init(getApplicationContext()); + } +} +``` + +Another good alternative is to initialize via a DI framework such as Dagger 2. See below for a very simple example: + +``` +@Singleton +@Module +public class MyModule { + + @Provides + TrueBlue provideBluetoothService(Context context) { + return TrueBlue.init(context); + } +} +``` + +Once initialized, an instance of the service can be obtained for use as required using `TrueBlue.getInstance()`. + +Be aware that it is always possible to initialize and obtain an instance of the service, even when no Bluetooth subsystem is available (e.g. emulators). In this situation methods pertaining to the availability and status of Bluetooth will work as expected, but the majority of other features will simply be no-ops. + + +## Usage + +Please refer to the [project wiki](https://github.com/Coiney/TrueBlue/wiki/Usage) and Javadoc for +more detailed information regarding usage. + + +## Test Application + +The project contains a simple test application designed to exercise most of the features provided by the service. It is admittedly rather contrived and overly simple in certain places, but should at least provide a basic example of how the service can be used. + + +## Contributing + +Please see [here](CONTRIBUTING.md) for detailed information on contributing to this project. + +Additionally, a project [roadmap](ROADMAP.md) exists which details features we are currently considering implementing. + + +## License + +``` +Copyright 2017 Coiney, Inc. +Copyright 2016 - 2017 Daniel Carter + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +``` diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md new file mode 100644 index 0000000..6e39c5b --- /dev/null +++ b/RELEASE_NOTES.md @@ -0,0 +1,27 @@ +### TrueBlue version 0.1.0 + +Initial release. + +#### Features + +None. + +#### Enhancements + +None. + +#### Bug Fixes + +None. + +#### Changes + +None. + +#### Other + +None. + +#### Requirements + +* Android 2.3 and up (Gingerbread, API 9 and up). diff --git a/ROADMAP.md b/ROADMAP.md new file mode 100644 index 0000000..2eadde1 --- /dev/null +++ b/ROADMAP.md @@ -0,0 +1,43 @@ +# Roadmap for TrueBlue + +This file lists features which are planned for implementation or may be considered for implementation in the future. + +### Bluetooth Profiles + +Full support for these as per the Bluetooth guide. + +### Bluetooth LE + +Full support for this as per the Bluetooth guide. + +### Bluetooth Server Support + +Full support for this as per the Bluetooth guide. + +### Pairing + +* Add a pairing (only) feature. This would be easy on APIs 19+ as the Android Bluetooth subsystem supports this, but on earlier versions we would either need to connect and disconnect (messy), or just connect and explain this in the documentation. +* Add a pairing event listener similar to the existing device connectivity listener. +* Add support for "unpairing" (removing bonds). + +### Connecting + +* Provide a means to obtain the instance of the class implementing the `Connection` interface for a given Bluetooth device which has already been connected to via the service after the fact. Currently this is only available via `ConnectionAttemptCallback` when the device is successfully connected to. +* Attempt to provide a means to prevent pairing with devices which are already paired with. Alternatively, permit this but provide a warning. + +### Connections + +* Decide whether the asynchronous connection client write error and write success callbacks should return the written data or not. +* Potentially limit the amount of data which can be written via the asynchronous connection client to prevent the queues getting too large. +* Add metadata such as the dates and times connections are established. +* Introduce additional monitoring to ensure that connection tasks and so on never get stuck. + +### General + +* Consider simplifying concurrency by using `HandlerThread` behind the scenes instead of `Thread` and `ExecutorService`. A few things to consider: + * This approach could be difficult for certain tasks such as connecting to Bluetooth devices, which is very complex. Mind you, that very complexity could be at least in part due to the current approach to concurrency, and in any event suggests that there is probably a better way. + * The impact on the service interface - in particular, on what is synchronous and what is asynchronous. Methods like `isDeviceConnected` are currently synchronous, and to retain that approach we would probably still need to use synchronisation behind the scenes. The alternative is to go asynchronous, but this seems like it might be overkill (and frustrating?) for simpler tasks. + +### Tests + +* Improve and expand test coverage. diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/LICENSE-3RD-PARTY.txt b/app/LICENSE-3RD-PARTY.txt new file mode 100644 index 0000000..fb45de3 --- /dev/null +++ b/app/LICENSE-3RD-PARTY.txt @@ -0,0 +1,208 @@ +This software package uses the following components licensed under the Apache +License Version 2.0: + +* Android Support Library (Copyright © 2015, The Android Open Source Project) +* Butter Knife (Copyright © 2013, Jake Wharton) + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/app/LICENSE.txt b/app/LICENSE.txt new file mode 100644 index 0000000..8dada3e --- /dev/null +++ b/app/LICENSE.txt @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..7bff5a0 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,103 @@ +apply plugin: 'com.android.application' + +android { + + compileSdkVersion rootProject.ext.COMPILE_SDK_VERSION + buildToolsVersion rootProject.ext.BUILD_TOOLS_VERSION + + compileOptions { + sourceCompatibility rootProject.ext.JAVA_VERSION + targetCompatibility rootProject.ext.JAVA_VERSION + } + + defaultConfig { + applicationId "com.coiney.android.hellobluetooth" + + minSdkVersion 11 + targetSdkVersion rootProject.ext.TARGET_SDK_VERSION + + versionName '1.0.0' + versionCode rootProject.ext.VERSION_CODE + } + + signingConfigs { + + debug {} + + production {} + } + + buildTypes { + + release { + debuggable false + + minifyEnabled true + shrinkResources true + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + + signingConfig signingConfigs.production + } + + debug { + debuggable true + + minifyEnabled false + shrinkResources false + + signingConfig signingConfigs.debug + } + } + + packagingOptions { + merge 'META-INF/LICENCE.md' + merge 'META-INF/LICENCE.txt' + merge 'META-INF/LICENCE' + merge 'META-INF/LICENSE.md' + merge 'META-INF/LICENSE.txt' + merge 'META-INF/LICENSE' + merge 'META-INF/LICENCE-3RD-PARTY.md' + merge 'META-INF/LICENCE-3RD-PARTY.txt' + merge 'META-INF/LICENCE-3RD-PARTY' + merge 'META-INF/LICENSE-3RD-PARTY.md' + merge 'META-INF/LICENSE-3RD-PARTY.txt' + merge 'META-INF/LICENSE-3RD-PARTY' + merge 'META-INF/NOTICE.md' + merge 'META-INF/NOTICE.txt' + merge 'META-INF/NOTICE' + merge 'META-INF/ASL2.0' + merge '/LICENCE.txt' + merge '/LICENCE' + merge '/LICENCE.md' + merge '/LICENSE.txt' + merge '/LICENSE' + merge '/LICENSE.md' + merge '/LICENCE-3RD-PARTY' + merge '/LICENCE-3RD-PARTY.txt' + merge '/LICENCE-3RD-PARTY.md' + merge '/LICENSE-3RD-PARTY' + merge '/LICENSE-3RD-PARTY.txt' + merge '/LICENSE-3RD-PARTY.md' + merge '/NOTICE' + merge '/NOTICE.txt' + merge '/NOTICE.md' + } + + android { + lintOptions { + abortOnError false + } + } +} + +dependencies { + + annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0' + + compile 'com.android.support:appcompat-v7:' + rootProject.ext.ANDROID_SUPPORT_LIBRARY_VERSION + compile 'com.android.support:design:' + rootProject.ext.ANDROID_SUPPORT_LIBRARY_VERSION + compile 'com.jakewharton:butterknife:8.7.0' + + debugCompile project(path: ':lib', configuration: 'debug') + releaseCompile project(path: ':lib', configuration: 'release') +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..e61842c --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/dcarter/Library/Developer/android-sdk-macosx/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..34e6318 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/coiney/android/hellobluetooth/BaseBluetoothActivity.java b/app/src/main/java/com/coiney/android/hellobluetooth/BaseBluetoothActivity.java new file mode 100644 index 0000000..3f014e1 --- /dev/null +++ b/app/src/main/java/com/coiney/android/hellobluetooth/BaseBluetoothActivity.java @@ -0,0 +1,85 @@ +/* + * Copyright 2017 Coiney, Inc. + * Copyright 2016 - 2017 Daniel Carter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.coiney.android.hellobluetooth; + +import android.bluetooth.BluetoothDevice; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.design.widget.Snackbar; +import android.support.v7.app.AppCompatActivity; + +import com.coiney.android.trueblue.BluetoothStatusListener; +import com.coiney.android.trueblue.TrueBlue; + +public class BaseBluetoothActivity extends AppCompatActivity implements BluetoothStatusListener { + + private static final String DEFAULT_UNKNOWN_DEVICE_NAME = ""; + + private TrueBlue mBluetoothService; + private Snackbar mSnackbar; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mBluetoothService = TrueBlue.getInstance(); + } + + @Override + protected void onResume() { + super.onResume(); + mBluetoothService.registerBluetoothStatusListener(this); + } + + @Override + protected void onPause() { + super.onPause(); + mBluetoothService.unregisterBluetoothStatusListener(this); + } + + @Override + public void onBluetoothDisabled() { + handleBluetoothDisabled(); + } + + @Override + public void onBluetoothEnabled() { + handleBluetoothEnabled(); + } + + protected void handleBluetoothDisabled() { /* Empty */ } + + protected void handleBluetoothEnabled() { /* Empty */ } + + protected TrueBlue getBluetoothService() { + return mBluetoothService; + } + + protected String getDeviceName(BluetoothDevice device) { + final String deviceName = device.getName(); + return (deviceName != null) ? deviceName : DEFAULT_UNKNOWN_DEVICE_NAME; + } + + protected void showSnackbar(String message) { + if (mSnackbar != null && mSnackbar.isShown()) { + mSnackbar.dismiss(); + } + mSnackbar = Snackbar.make(findViewById(android.R.id.content), message, + Snackbar.LENGTH_SHORT); + mSnackbar.show(); + } +} diff --git a/app/src/main/java/com/coiney/android/hellobluetooth/DeviceListAdapter.java b/app/src/main/java/com/coiney/android/hellobluetooth/DeviceListAdapter.java new file mode 100644 index 0000000..a1b01f7 --- /dev/null +++ b/app/src/main/java/com/coiney/android/hellobluetooth/DeviceListAdapter.java @@ -0,0 +1,68 @@ +/* + * Copyright 2017 Coiney, Inc. + * Copyright 2016 - 2017 Daniel Carter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.coiney.android.hellobluetooth; + +import android.bluetooth.BluetoothDevice; +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import java.util.List; + +final class DeviceListAdapter extends ArrayAdapter { + + DeviceListAdapter(Context context, int resource) { + super(context, resource); + } + + DeviceListAdapter(Context context, int resource, List objects) { + super(context, resource, objects); + } + + @Override + @NonNull + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + return getItemView(position, convertView, parent); + } + + @Override + public View getDropDownView(int position, @Nullable View convertView, + @NonNull ViewGroup parent) { + return getItemView(position, convertView, parent); + } + + private View getItemView(int position, View convertView, ViewGroup parent) { + LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( + Context.LAYOUT_INFLATER_SERVICE); + if (convertView == null) { + convertView = inflater.inflate(R.layout.device_list_item, null); + } + final BluetoothDevice device = getItem(position); + final String deviceName = (device != null) ? device.getName() : null; + final String deviceAddress = (device != null) ? device.getAddress() : "?"; + final String displayName = ((deviceName != null) ? deviceName : "") + + " (" + deviceAddress + ")"; + ((TextView) convertView.findViewById(android.R.id.text1)).setText(displayName); + return convertView; + } +} diff --git a/app/src/main/java/com/coiney/android/hellobluetooth/DiscoveryActivity.java b/app/src/main/java/com/coiney/android/hellobluetooth/DiscoveryActivity.java new file mode 100644 index 0000000..625c873 --- /dev/null +++ b/app/src/main/java/com/coiney/android/hellobluetooth/DiscoveryActivity.java @@ -0,0 +1,247 @@ +/* + * Copyright 2017 Coiney, Inc. + * Copyright 2016 - 2017 Daniel Carter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.coiney.android.hellobluetooth; + +import android.Manifest; +import android.bluetooth.BluetoothDevice; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.ActivityCompat; +import android.view.View; +import android.widget.AdapterView; +import android.widget.Button; +import android.widget.ListView; +import android.widget.TextView; + +import com.coiney.android.trueblue.ConnectionClient; +import com.coiney.android.trueblue.ConnectionAttemptCallback; +import com.coiney.android.trueblue.ConnectionClients; +import com.coiney.android.trueblue.ConnectionAttemptConfiguration; +import com.coiney.android.trueblue.DiscoveryError; +import com.coiney.android.trueblue.DiscoveryListener; +import com.coiney.android.trueblue.TrueBlue; +import com.coiney.android.trueblue.Connection; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; +import butterknife.OnItemClick; + +public class DiscoveryActivity extends BaseBluetoothActivity implements + DiscoveryListener, ConnectionAttemptCallback { + + private static final int REQUEST_DISCOVERY_PERMISSIONS = 127; + + private TrueBlue mBluetoothService; + @BindView(R.id.bluetooth_warning) TextView mBluetoothWarning; + private DeviceListAdapter mDiscoveryResultsAdapter; + @BindView(R.id.discovery_results_list) ListView mDiscoveryResultsList; + private boolean mEnablingBluetoothShouldStartDiscovery; + @BindView(R.id.start_discovery_button) Button mStartButton; + @BindView(R.id.stop_discovery_button) Button mStopButton; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_discovery); + ButterKnife.bind(this); + mBluetoothService = TrueBlue.getInstance(); + if (!mBluetoothService.isBluetoothAvailable()) { + mBluetoothWarning.setText(R.string.bluetooth_unavailable); + mBluetoothWarning.setVisibility(View.VISIBLE); + return; + } + mDiscoveryResultsAdapter = new DeviceListAdapter(this, R.layout.device_list_item); + mDiscoveryResultsList.setAdapter(mDiscoveryResultsAdapter); + } + + @Override + protected void onResume() { + super.onResume(); + mBluetoothService.registerDiscoveryListener(this); + updateBluetoothStatus(); + updateButtons(mBluetoothService.isServiceDiscoveryScanRunning()); + } + + @Override + protected void onPause() { + super.onPause(); + mBluetoothService.unregisterDiscoveryListener(this); + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], + @NonNull int[] grantResults) { + if (requestCode != REQUEST_DISCOVERY_PERMISSIONS) { + return; + } + updateButtons(false); + if (permissions.length <= 0 || grantResults.length <= 0 || permissions.length != + grantResults.length) { + return; + } + for (int i = 0; i < permissions.length; i++) { + if (permissions[i].equals(Manifest.permission.ACCESS_COARSE_LOCATION) && + PackageManager.PERMISSION_GRANTED == grantResults[i]) { + startDiscovery(); + } + } + } + + @OnClick(R.id.start_discovery_button) + void startDiscovery() { + updateButtons(true); + mDiscoveryResultsAdapter.clear(); + final DiscoveryError error = mBluetoothService.startDiscovery(this); + if (error != null) { + updateButtons(false); + switch (error) { + case BLUETOOTH_DISABLED: + mEnablingBluetoothShouldStartDiscovery = true; + mBluetoothService.requestBluetoothBeEnabled(this); + break; + case COARSE_LOCATION_PERMISSION_REQUIRED: + requestCoarseLocationPermissions(); + break; + default: + showSnackbar("Discovery error."); + break; + } + } + } + + @OnClick(R.id.stop_discovery_button) + void stopDiscovery() { + mStopButton.setEnabled(false); + mBluetoothService.stopDiscovery(); + } + + @OnItemClick(R.id.discovery_results_list) + void connectToDevice(AdapterView parent, int position) { + final BluetoothDevice device = (BluetoothDevice) parent.getItemAtPosition(position); + if (null == device) { + return; + } + // Tailor connection configuration for pairing. + final ConnectionAttemptConfiguration connectionConfiguration = + new ConnectionAttemptConfiguration.Builder().setRetryCount(0) + .setCanInterruptDiscoveryScan(false) + .build(); + mBluetoothService.connect(device, connectionConfiguration, this); + } + + // Bluetooth status listener + + @Override + protected void handleBluetoothDisabled() { + updateBluetoothStatus(); + } + + @Override + protected void handleBluetoothEnabled() { + updateBluetoothStatus(); + if (mEnablingBluetoothShouldStartDiscovery) { + mEnablingBluetoothShouldStartDiscovery = false; + startDiscovery(); + } + } + + // Discovery scan callback + + @Override + public void onDeviceDiscovered(@NonNull BluetoothDevice device) { + // Avoid adding the same device to the adapter multiple times. + if (mDiscoveryResultsAdapter.getPosition(device) < 0) { + mDiscoveryResultsAdapter.add(device); + } + } + + @Override + public void onDiscoveryFinished() { + updateButtons(false); + showSnackbar("Discovery finished."); + } + + @Override + public void onDiscoveryStarted() { + showSnackbar("Discovery started."); + } + + // Device connection attempt callback + + @Override + public void onConnectionAttemptCancelled(@NonNull BluetoothDevice device) { + showSnackbar("Pairing with " + getDeviceName(device) + " cancelled."); + } + + @Override + public void onConnectionAttemptFailed(@NonNull BluetoothDevice device) { + } + + @Override + public void onConnectionAttemptSucceeded(@NonNull BluetoothDevice device, + @NonNull Connection connection) { + // Fake something useful using the connection - required to get close + // notifications (these only trigger upon read/write error). + final NoopConnectionHandler noopHandler = new NoopConnectionHandler(); + final ConnectionClient connectionClient = ConnectionClients.wrap(connection, 256, + noopHandler); + connectionClient.startReading(); + } + + @Override + public void onPairingAttemptFailed(@NonNull BluetoothDevice device) { + showSnackbar("Pairing with " + getDeviceName(device) + " failed."); + } + + @Override + public void onPairingAttemptStarted(@NonNull BluetoothDevice device) { + showSnackbar("Pairing with " + getDeviceName(device) + " ..."); + } + + @Override + public void onPairingAttemptSucceeded(@NonNull BluetoothDevice device) { + showSnackbar("Paired with " + getDeviceName(device) + "."); + } + + private void updateBluetoothStatus() { + if (mBluetoothService.isBluetoothEnabled()) { + mBluetoothWarning.setVisibility(View.GONE); + } else { + mBluetoothWarning.setText(R.string.bluetooth_disabled); + mBluetoothWarning.setVisibility(View.VISIBLE); + } + } + + private void updateButtons(boolean isDiscoveryRunning) { + mStartButton.setEnabled(!isDiscoveryRunning); + mStopButton.setEnabled(isDiscoveryRunning); + } + + private void requestCoarseLocationPermissions() { + if (ActivityCompat.shouldShowRequestPermissionRationale(this, + Manifest.permission.ACCESS_COARSE_LOCATION)) { + // TODO Show rationale. + // return; + } + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, + REQUEST_DISCOVERY_PERMISSIONS); + } +} diff --git a/app/src/main/java/com/coiney/android/hellobluetooth/HelloBluetoothApplication.java b/app/src/main/java/com/coiney/android/hellobluetooth/HelloBluetoothApplication.java new file mode 100644 index 0000000..9bf4d32 --- /dev/null +++ b/app/src/main/java/com/coiney/android/hellobluetooth/HelloBluetoothApplication.java @@ -0,0 +1,31 @@ +/* + * Copyright 2017 Coiney, Inc. + * Copyright 2016 - 2017 Daniel Carter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.coiney.android.hellobluetooth; + +import android.app.Application; + +import com.coiney.android.trueblue.TrueBlue; + +public class HelloBluetoothApplication extends Application { + + @Override + public void onCreate() { + super.onCreate(); + TrueBlue.init(this.getApplicationContext()); + } +} diff --git a/app/src/main/java/com/coiney/android/hellobluetooth/MainActivity.java b/app/src/main/java/com/coiney/android/hellobluetooth/MainActivity.java new file mode 100644 index 0000000..b4ae22d --- /dev/null +++ b/app/src/main/java/com/coiney/android/hellobluetooth/MainActivity.java @@ -0,0 +1,245 @@ +/* + * Copyright 2017 Coiney, Inc. + * Copyright 2016 - 2017 Daniel Carter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.coiney.android.hellobluetooth; + +import android.bluetooth.BluetoothDevice; +import android.content.Intent; +import android.support.annotation.NonNull; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.AdapterView; +import android.widget.Button; +import android.widget.Spinner; +import android.widget.TextView; + +import com.coiney.android.trueblue.ConnectionAttemptCallback; +import com.coiney.android.trueblue.ConnectionClient; +import com.coiney.android.trueblue.ConnectionClients; +import com.coiney.android.trueblue.ConnectionAttemptConfiguration; +import com.coiney.android.trueblue.DeviceConnectionListener; +import com.coiney.android.trueblue.TrueBlue; +import com.coiney.android.trueblue.Connection; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; +import butterknife.OnItemSelected; + +public class MainActivity extends BaseBluetoothActivity implements DeviceConnectionListener, + ConnectionAttemptCallback { + + private TrueBlue mBluetoothService; + @BindView(R.id.bluetooth_warning) TextView mBluetoothWarning; + @BindView(R.id.connect_button) Button mConnectButton; + @BindView(R.id.disconnect_all_button) Button mDisconnectAllButton; + @BindView(R.id.disconnect_button) Button mDisconnectButton; + @BindView(R.id.paired_device_list) Spinner mPairedDeviceList; + DeviceListAdapter mPairedDeviceListAdapter; + @BindView(R.id.update_paired_device_list_button) Button mUpdatePairedDeviceListButton; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + ButterKnife.bind(this); + mBluetoothService = TrueBlue.getInstance(); + if (!getBluetoothService().isBluetoothAvailable()) { + mBluetoothWarning.setText(R.string.bluetooth_unavailable); + mBluetoothWarning.setVisibility(View.VISIBLE); + return; + } + mPairedDeviceListAdapter = new DeviceListAdapter(getApplicationContext(), + R.layout.device_list_item); + mDisconnectAllButton.setEnabled(true); + mUpdatePairedDeviceListButton.setEnabled(true); + mPairedDeviceList.setAdapter(mPairedDeviceListAdapter); + } + + @Override + protected void onResume() { + super.onResume(); + getBluetoothService().registerDeviceConnectionListener(this); + updateBluetoothInformation(); + } + + @Override + protected void onPause() { + super.onPause(); + getBluetoothService().unregisterDeviceConnectionListener(this); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.main_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == R.id.discovery) { + startActivity(new Intent(MainActivity.this, DiscoveryActivity.class)); + } + return super.onOptionsItemSelected(item); + } + + @OnClick(R.id.connect_button) + void connectToDevice(View v) { + v.setEnabled(false); + final BluetoothDevice device = (BluetoothDevice) mPairedDeviceList.getSelectedItem(); + if (device != null) { + final ConnectionAttemptConfiguration connectionConfiguration = + new ConnectionAttemptConfiguration.Builder().setRetryCount(2) + .setInitialRetryDelay(1500) + .setRetryDelayBackoffMultiplier(1.25f) + .build(); + mBluetoothService.connect(device, connectionConfiguration, this); + } + } + + @OnClick(R.id.disconnect_button) + void disconnectFromDevice(View v) { + v.setEnabled(false); + final BluetoothDevice device = (BluetoothDevice) mPairedDeviceList.getSelectedItem(); + if (device != null) { + mBluetoothService.disconnect(device); + } + } + + @OnClick(R.id.disconnect_all_button) + void disconnectFromAllDevices() { + mBluetoothService.disconnectAll(); + } + + @OnClick(R.id.update_paired_device_list_button) + void updatePairedDeviceList() { + if (!mBluetoothService.isBluetoothEnabled()) { + mBluetoothService.requestBluetoothBeEnabled(this); + } else { + updatePairedDevicesList(); + } + } + + @OnItemSelected(R.id.paired_device_list) + void updateConnectivityButtons(AdapterView parent, int position) { + final BluetoothDevice device = (BluetoothDevice) parent.getItemAtPosition(position); + setConnectionButtonStatuses(device); + } + + @Override + public void onConnectionAttemptCancelled(@NonNull BluetoothDevice device) { + setConnectionButtonStatusesIfSelected(device); + showSnackbar("Connection to " + getDeviceName(device) + " cancelled."); + } + + @Override + public void onConnectionAttemptFailed(@NonNull BluetoothDevice device) { + setConnectionButtonStatusesIfSelected(device); + showSnackbar("Connection to " + getDeviceName(device) + " failed."); + } + + @Override + public void onConnectionAttemptSucceeded(@NonNull BluetoothDevice device, + @NonNull Connection connection) { + // Fake something useful using the connection - required to get close + // notifications (these only trigger upon read/write error). + final NoopConnectionHandler noopHandler = new NoopConnectionHandler(); + final ConnectionClient connectionClient = ConnectionClients.wrap(connection, 256, + noopHandler); + connectionClient.startReading(); + setConnectionButtonStatusesIfSelected(device); + showSnackbar("Connected to " + getDeviceName(device) + "."); + } + + @Override + public void onPairingAttemptFailed(@NonNull BluetoothDevice device) { + // Should never happen - this activity only deals with connections to + // devices which have already been paired with. + } + + @Override + public void onPairingAttemptStarted(@NonNull BluetoothDevice device) { + // Should never happen - this activity only deals with connections to + // devices which have already been paired with. + } + + @Override + public void onPairingAttemptSucceeded(@NonNull BluetoothDevice device) { + // Should never happen - this activity only deals with connections to + // devices which have already been paired with. + } + + @Override + public void onDeviceConnected(@NonNull final BluetoothDevice device) { + // Empty - we are connecting so we will handle this in the + // onConnectionAttemptSucceeded callback. + } + + @Override + public void onDeviceDisconnected(@NonNull final BluetoothDevice device, + final boolean wasCausedByError) { + setConnectionButtonStatusesIfSelected(device); + showSnackbar("Disconnected from " + getDeviceName(device)); + } + + @Override + protected void handleBluetoothDisabled() { + updateBluetoothInformation(); + } + + @Override + protected void handleBluetoothEnabled() { + updateBluetoothInformation(); + } + + private void updateBluetoothInformation() { + if (mBluetoothService.isBluetoothEnabled()) { + mBluetoothWarning.setVisibility(View.GONE); + } else { + mBluetoothWarning.setText(R.string.bluetooth_unavailable); + mBluetoothWarning.setVisibility(View.VISIBLE); + } + updatePairedDevicesList(); + } + + private void updatePairedDevicesList() { + mPairedDeviceListAdapter.clear(); + if (!mBluetoothService.isBluetoothEnabled()) { + return; + } + for (BluetoothDevice device : getBluetoothService().getDeviceList()) { + mPairedDeviceListAdapter.add(device); + } + } + + private void setConnectionButtonStatusesIfSelected(final BluetoothDevice device) { + final BluetoothDevice selectedDevice = + (BluetoothDevice) mPairedDeviceList.getSelectedItem(); + if (selectedDevice != null && selectedDevice.equals(device)) { + setConnectionButtonStatuses(device); + } + } + + private void setConnectionButtonStatuses(BluetoothDevice device) { + final boolean isConnectedOrConnecting = getBluetoothService() + .isConnectedOrConnecting(device); + mConnectButton.setEnabled(!isConnectedOrConnecting); + mDisconnectButton.setEnabled(isConnectedOrConnecting); + } +} diff --git a/app/src/main/java/com/coiney/android/hellobluetooth/NoopConnectionHandler.java b/app/src/main/java/com/coiney/android/hellobluetooth/NoopConnectionHandler.java new file mode 100644 index 0000000..70f2d02 --- /dev/null +++ b/app/src/main/java/com/coiney/android/hellobluetooth/NoopConnectionHandler.java @@ -0,0 +1,51 @@ +/* + * Copyright 2017 Coiney, Inc. + * Copyright 2016 - 2017 Daniel Carter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.coiney.android.hellobluetooth; + +import android.support.annotation.NonNull; + +import com.coiney.android.trueblue.ConnectionClient; + +final class NoopConnectionHandler implements ConnectionClient.Callback { + + @Override + public void onConnectionClosed(@NonNull ConnectionClient client, + boolean wasClosedByError) { + // NO-OP + } + + @Override + public void onDataRead(@NonNull ConnectionClient client, @NonNull byte[] data) { + // NO-OP + } + + @Override + public void onDataWritten(@NonNull ConnectionClient client, @NonNull byte[] data) { + // NO-OP + } + + @Override + public void onReadErrorEncountered(@NonNull ConnectionClient client) { + // NO-OP + } + + @Override + public void onWriteErrorEncountered(@NonNull ConnectionClient client, @NonNull byte[] data) { + // NO-OP + } +} diff --git a/app/src/main/res/layout/activity_discovery.xml b/app/src/main/res/layout/activity_discovery.xml new file mode 100644 index 0000000..bb793b0 --- /dev/null +++ b/app/src/main/res/layout/activity_discovery.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + +