Transfer Source

The transfer source is the device that has the data and wants to transfer it to another device.

In terms of Bluetooth, the transfer source is the one that is scanning and searching for another device (target).

Example integration


data class MyTransferData(
    val id: String,
    val token: String
)

class MySampleSource {

    val source: WPTSource

    val config = WPTConfig(
        ParcelUuid.fromString("21f8190c-e4bc-11ed-b5ea-0242ac120001"), 
        WPTCoder.aes(
            "MyApplicationDemoSalt!".toByteArray(), // static data
            "7x9ZEvEVf4IqlBxuYmzKhw==".decodeBase64()!!.toByteArray() // 16 bytes encoded in Base64
        )
    )

    constructor(appContext: Context) {
        source = WPTSource(appContext, config)
        source.listener = object : WPTScannerListener {
            override fun devicesChanged(devices: WPTTargetDevices) {
                val candidate = devices.bestCandidate()
                if (candidate != null) {
                    // device found
                    val dataToTransfer = MyTransferData("123", "10a82fe8d1c4df")
                    val json = Gson().toJson(dataToTransfer)
                    val jsonData = json.toByteArray()
                    val encoded = candidate.encode(jsonData)
                    val base64 = Base64.getEncoder().encodeToString(encoded)
                    // QR code with the base64 string
                    
                } else if (devices.isProcessingDevice()) {
                    // device is processing
                } else {
                    // waiting for a device to be found
                }
            }
        }

        source.start { result ->
            result.onSuccess {
                // waiting for a device to be found
            }.onFailure { error ->
                // process error
                if (error is WPTSourceError) {
                    // process WPTSourceError
                }
            }
        }
    }
}

Classes used for the source

WPTSource

Entrypoint for the source. Just start the source and wait for the device to be discovered.

Kotlin pseudo-interface of the class

class WPTSource {

    /** Listener that will be notified when a target device changes */
    var listener: WPTSourceListener?

    /** Starts the source (in Bluetooth terms - starts scanning for Bluetooth devices) */
    fun start(completion: (Result<Unit>) -> Unit)

    /** Stops the source */
    fun stop(completion: (Result<Unit>) -> Unit)
}

WPTSourceDelegate

A delegate will be called when the status of the source changes or the device is discovered/changed.

Kotlin pseudo-interface of the class

interface WPTSourceListener {

    /**
     * When possible target device was found or was changed.
     *
     * @param devices Devices available for the transfer
     */ 
    fun devicesChanged(devices: WPTTargetDevices)
}

WPTTargetDevices

Devices that are currently found and suitable to target.

Unless you have some internal logic, it’s best to use bestCandidate as a device to target (and generate the QR for).

Kotlin pseudo-interface of the class

class WPTTargetDevices {

    /** All found devices. */
    val allDevices: List<WPTTargetDevice>

    /** The best candidate for the target. */
    fun bestCandidate(): WPTTargetDevice?
    
    /** Returns true if there is at least one device that is in the [WPTTargetDevice.State.DISCOVERED] state. */
    fun isProcessingDevice()
}

WPTTargetDevice

A device that represents a transfer target.

class WPTTargetDevice {

    /** The state of the device. */
    val state: State

    /** Name of the device */
    val name: String

    /** The time when the device was found. */
    val created: Long

    /**
     * When the device is found, encrypted data needs to be transferred via a generated/scanned QR code.
     * Use this method for a particular device to encode your data.
     *
     * @param data Data to be encoded.
     * @return Encoded data.
     */
    @Throws fun encode(data: ByteArray): ByteArray
}

WPTSourceError

An error that can occur during the target start/stop operation

class WPTSourceError: Throwable {

    /** The reason of the error. */
    val reason: Reason
    /** The permission that is missing in case of the [Reason.BLUETOOTH_PERMISSION_NOT_GRANTED]. */
    val permission: String?

    /**
     * The reason of the error.
     */
    enum class Reason {
        /** The scanner is already running/already stopped.*/
        WRONG_STATE,
        /** Bluetooth permission not granted. Use WPTPermissions to ask for permissions. */
        BLUETOOTH_PERMISSION_NOT_GRANTED,
        /** Bluetooth is turned off. */
        BLUETOOTH_NOT_ENABLED,
        /** Bluetooth is not supported by this device. */
        BLUETOOTH_MISSING,
        /**
         * Location is disabled in the system settings. You can navigate the user to the
         * system settings with startActivity(Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)).
         *
         * This might happened only if you're supporting Android 11 or lower.
         */
        LOCATION_NOT_ENABLED,
        /** Unknown error */
        UNKNOWN
    }
}
Last updated on Feb 05, 2024 (11:35) Edit on Github Send Feedback
Search

1.0.1

Proximity Transfer for Android