Transfer Target

The transfer target is the device that will receive data from another device.

In terms of Bluetooth, the transfer source is the one that is broadcasting to another device.

Example integration


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

class MySampleTarget {

    val target: WPTTarget
    
    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) {
        this.target = WPTTarget(appContext, config)
        this.target.start { result ->
            result.onSuccess {
                try {
                    // target start success, start QR scanning
                    // following is an example of how to process the scanned QR code
                    val scannedQRCode = "base64encodedStringRetrievedByQRScanner"
                    val plain = qrContent.decodeBase64()!!
                    val decoded = target.decode(plain.toByteArray())
                    val decodedString = String(decoded)
                    val result = Gson().fromJson(decodedString, MyTransferData::class.java)
                } catch (e: Exception) {
                    // process exception
                }
            }.onFailure { error ->
                if (error is WPTTargetError) {
                    // process WPTTargetError
                }
            }
        }
    }
} 

Classes used for the target

WPTTarget

Entrypoint for the target. Just start the target and instruct the user to scan the QR code that will be prepared on the source device.

Kotlin pseudo-interface of the class

class WPTTarget {

    /**
     * Creates a new target.
     *  
     * @param context The application context.   
     * @param config The configuration of the target.
     */
    constructor(context: Context, config: WPTConfig)

    /**
     * Starts the broadcasting.
     *
     * @param completion The completion callback. When an error occurs, it is the type of [WPTTargetError]
     */
    fun start(completion: (Result<Unit>) -> Unit)
    
    /**
     * Stops the broadcasting.
     *
     * @param completion The completion callback. When an error occurs, it is the type of [WPTTargetError].
     */
    fun stop(completion: (Result<Unit>) -> Unit)
    
    /** Returns true if the target is broadcasting. */
    fun isRunning(): Bool
    
    /**
     * Decodes the data scanned via QR scanner
     *
     * @param data Data scanned via QR scanner.
     * @return Decoded data.
     */
    @Throws fun decode(data: ByteArray): ByteArray
}

WPTTargetError

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

class WPTTargetError: 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?

    enum class Reason {
        /** Already started or 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,
        /** Data for the Bluetooth are too large (this is a library/platform error). */
        DATA_TOO_LARGE,
        /** Bluetooth is not supported by this device. */
        FEATURE_UNSUPPORTED,
        /** Internal Bluetooth error */
        INTERNAL_ERROR,
        /** Too many advertisers are advertising */
        TOO_MANY_ADVERTISERS,
        /** Unknown error */
        UNKNOWN
Last updated on Feb 05, 2024 (12:35) Edit on Github Send Feedback
Search

1.0.1

Proximity Transfer for Android