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 = scannedDevice.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
}
Last updated on Feb 01, 2024 (13:48) Edit on Github Send Feedback
Search

1.0.0

Proximity Transfer for Android