-
-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android onboarding & basic usage documentation #339
base: main
Are you sure you want to change the base?
Conversation
Lmk if you think anything is missing :) |
docs/simpledroidble/usage.rst
Outdated
Building from source | ||
-------------------- | ||
|
||
1. Clone `the repo <https://github.com/OpenBluetoothToolbox/SimpleBLE>`_. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to clarify this. Also use proper language.
|
||
2. Build the libraries using gradle (``./simpledroidble/gradlew assembleRelease``) or Android Studio. The following libraries will be built: | ||
- ``simpledroidble/simpledroidble/build/outputs/aar/simpledroidble-release.aar`` | ||
- ``simpleble/src/backends/android/simpleble-bridge/build/outputs/aar/simpleble-bridge-release.aar`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move the simpleble-bridge
documentation to the simpleble/usage.rst
page, as it would be specific for that kind of implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't simpleble-bridge.aar
required to use simpledroidble
? If so, from the POV of the Android programmer, this should just be another requirement imo (i.e.: "You need both these AARs to use simpledroidble").
docs/simpledroidble/usage.rst
Outdated
implementation(files('libs/simpledroidble.aar', 'libs/simpleble-bridge.aar')) | ||
} | ||
|
||
3. You're ready to start using SimpleBLE on Android! Go to `Using SimpleDroidBLE`_ to continue with the onboarding. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the same tone as the other documentation pages.
docs/simpledroidble/usage.rst
Outdated
|
||
1. Place the package on your app's directory, and take note of its path. We'll use ``example-app/app/libs/{simpledroidble,simpleble-bridge}.aar``. | ||
|
||
2. Declare the dependency on your app's ``build.gradle{.kts}`` file (note: relative paths begin from your app's module folder):: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have a separate copy of this specific for simpleble-bridge
inside simpleble/usage.rst
docs/simpledroidble/usage.rst
Outdated
==================== | ||
|
||
First you'll need to provide ``SimpleDroidBLE.contextReference`` with a WeakReference to the app's context. In this step, the native code will be loaded in memory and permissions will be requested to the user. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be a lot more elaborate. Why is this required? How do you do it? Show a few code examples.
docs/simpledroidble/usage.rst
Outdated
----------------- | ||
|
||
The main entry point to SimpleDroidBLE is the Adapter object. You can get an Adapter instance via the static ``Adapter#getAdapters`` method, where the device's main bluetooth adapter will be returned. | ||
|
||
When using SimpleDroidBLE make sure to only reference a single instance of the Adapter object, as every instance creates a new internal referece in the native code. | ||
Having multiple instances (by unbounded calls to ``Adapter#getAdapters``, for example) may cause a memory leak. | ||
This can be accomplished by acquiring said instance either on your app's Application class, or, in a single activity app, on your Activity's viewmodel; and treating the Adapter as a global, singleton object. | ||
For an specific example you can check out `the sample app <https://github.com/OpenBluetoothToolbox/SimpleBLE/tree/main/examples/simpleble-android>`_. | ||
|
||
Connecting to devices | ||
--------------------- | ||
|
||
SimpleDroidBLE is a `flow <https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/>`_-driven interface. | ||
|
||
That means that when looking to connect to a device, you should ``collect`` from ``Adapter#onScanFound`` and then start scanning for devices with ``Adapter#scanStart``. You'll get all found ``Peripheral`` s on said flow, where you can ``Peripheral#connect`` to whichever you need. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good as a very basic approach, but the text needs to go deeper into detail, including examples that should be easy to follow for non-experts.
includeBuild("path/to/simpledroidble") { | ||
dependencySubstitution { | ||
substitute module("org.simpleble.android:simpledroidble") with project(":simpledroidble") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maintain the use of code blocks with the corresponding language specifier.
docs/simpledroidble/usage.rst
Outdated
includeBuild("path/to/simpledroidble") { | ||
dependencySubstitution { | ||
substitute(module("org.simpleble.android:simpledroidble")).using(project(":simpledroidble")) | ||
} | ||
} | ||
} | ||
|
||
```kotlin | ||
includeBuild("path/to/simpledroidble") { | ||
dependencySubstitution { | ||
substitute(module("org.simpleble.android:simpledroidble")).using(project(":simpledroidble")) | ||
Then, inside your ``build.gradle`` or ``build.gradle.kts`` file, you can add the | ||
following dependency:: | ||
|
||
dependencies { | ||
implementation "org.simpleble.android:simpledroidble" | ||
} | ||
} | ||
``` | ||
|
||
Then, inside your `build.gradle` or `build.gradle.kts` file, you can add the | ||
following dependency: | ||
|
||
```groovy | ||
dependencies { | ||
implementation "org.simpleble.android:simpledroidble" | ||
} | ||
``` | ||
|
||
```kotlin | ||
dependencies { | ||
implementation("org.simpleble.android:simpledroidble") | ||
} | ||
``` No newline at end of file | ||
|
||
Kotlin:: | ||
|
||
dependencies { | ||
implementation("org.simpleble.android:simpledroidble") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for all of these
import android.util.Log | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import org.simpleble.android.Adapter | ||
|
||
suspend fun peripheralState() { | ||
// Get an adapter | ||
val adapter = Adapter.getAdapters().first() | ||
|
||
CoroutineScope(Dispatchers.Main).run { | ||
launch { | ||
adapter.onScanUpdated.collect { peripheral -> | ||
adapter.scanStop() | ||
|
||
launch { | ||
peripheral.onConnected.collect { | ||
Log.i("Example", "Successfully connected to ${peripheral.identifier}") | ||
} | ||
} | ||
|
||
launch { | ||
peripheral.onDisconnected.collect { | ||
Log.i("Example", "Successfully disonnected from ${peripheral.identifier}") | ||
} | ||
} | ||
|
||
launch { | ||
peripheral.onConnectionActive.collect { | ||
Log.i("Example", "Connection changed to $it for ${peripheral.identifier}") | ||
} | ||
} | ||
|
||
peripheral.connect() | ||
delay(500) | ||
peripheral.disconnect() | ||
} | ||
} | ||
} | ||
|
||
adapter.scanStart() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use code blocks with language descriptors
Followed SimpleBLE tutorial and converted references and examples to idiomatic Kotlin (i.e. using flows)
This includes a simple setup guide and basic usage for both SimpleDroidBLE and the simpleble C++ interface on Android (using
simpleble-bridge
).