Kotlin Integration

Kotlin Integration

This guide provides instructions for integrating the FleetShare SDK into a Kotlin application. The SDK uses coroutines to run concurrently with your main application logic. Kotlin is a modern programming language that runs on the Java Virtual Machine (JVM) and is fully interoperable with Java. It is used on a wide range of platforms, including Android, web, and server-side applications.

Requirements

Before you begin, ensure you have the following dependencies in your project:

  • kotlinx.coroutines
  • okhttp3
  • org.json

Add these dependencies to your build.gradle file.

Integration Steps

1. Import Required Classes

Ensure you import the required FleetShare SDK Kotlin classes into your project. The FleetShare team will provide you with the necessary files.

2. Define Your Main Function

Define your main function where you will start the FleetShare SDK and run your main program logic.

3. Start the FleetShare SDK

Invoke the FleetShare SDK within a coroutine. Use the provided apiKey and, if necessary, additional parameters like silent and deviceName.

Parameters:

  • apiKey: Your API key from the FleetShare SDK program.
  • silent: (Optional) If set to true, the SDK runs silently without outputting logs.
  • deviceName: (Optional) A unique name for the device running the SDK.

4. Implement Main Program Logic

Include your main program logic within the main function. The FleetShare SDK will continue to share bandwidth in the background while your program runs.

Example Implementation

Here’s an example of integrating the FleetShare SDK into a Kotlin application:

package com.example

import kotlinx.coroutines.*

class App {
    val greeting: String
        get() {
            return "Hello World!"
        }
}

fun main() = runBlocking {
    val apiKey = "YOUR_API_KEY"
    
    // Launch FleetShareSDK in a separate coroutine
    val job = launch(Dispatchers.Default) {
        FleetShareSDK.startFleetShareSDK(apiKey, true, "Username123")
    }

    // Main program continues here
    println(App().greeting)

    // Simulate some work in the main program
    repeat(10) {
        println("Main program is running... (${it + 1})")
        delay(1000) // Wait for 1 second
    }

    // Optionally, you can cancel the FleetShareSDK job if you want to stop it
    // job.cancel()
}

Best Practices

  1. Error Handling: Implement robust error handling within your coroutines to manage potential issues.
  2. User Consent: Make sure to obtain user consent before initiating the bandwidth sharing process.
  3. Updating the SDK: Stay updated with new SDK versions and implement mechanisms to handle SDK updates gracefully, as older versions may become deprecated.

Ensure you follow these best practices to maintain a secure and user-friendly application.

Troubleshooting

If you encounter any issues while integrating or using the FleetShare SDK, consider the following:

  1. Ensure you’re using the latest version of the SDK.
  2. Check that all required dependencies are correctly added to your project.
  3. Verify that your API key is correct and active.
  4. If you’re experiencing connection issues, check your network connectivity.

For further assistance, please contact the FleetShare support team.