Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tombertoli
Copy link

This includes a simple setup guide and basic usage for both SimpleDroidBLE and the simpleble C++ interface on Android (using simpleble-bridge).

@tombertoli
Copy link
Author

Lmk if you think anything is missing :)

Building from source
--------------------

1. Clone `the repo <https://github.com/OpenBluetoothToolbox/SimpleBLE>`_.
Copy link
Member

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``
Copy link
Member

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.

Copy link
Author

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").

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.
Copy link
Member

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.


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)::
Copy link
Member

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

====================

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.
Copy link
Member

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.

Comment on lines 89 to 105
-----------------

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.

Copy link
Member

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.

Comment on lines +48 to +56
includeBuild("path/to/simpledroidble") {
dependencySubstitution {
substitute module("org.simpleble.android:simpledroidble") with project(":simpledroidble")
}
}
Copy link
Member

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.

Comment on lines 55 to 82
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")
}
Copy link
Member

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

Comment on lines +222 to +274
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()
}
Copy link
Member

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants