Coroutines Alternatives
Beacon Android SDK by default uses Coroutines to handle asynchronous code. If you don't want to use Coroutines in your application, see the alternatives below.
:client-wallet-compat
Beacon Adndroid SDK ships with :client-wallet-compat
module that provides a supplementary callback-based interface for :client-wallet
. See the snippets below to learn how to use it.
tip
See the Installation page for more information about the :client-wallet-compat
module and how to install it.
Create a wallet client
- Callback
- Coroutines
import it.airgap.beaconsdk.client.wallet.BeaconWalletClient
import it.airgap.beaconsdk.client.wallet.compat.BuildCallback
import it.airgap.beaconsdk.client.wallet.compat.build
lateinit var beaconWallet: BeaconWalletClient
BeaconWalletClient.Builder("My App").apply {
support(/* blockchains */)
use(/* connections */)
}.build(object : BuildCallback {
override fun onSuccess(beaconClient: BeaconWalletClient) {
beaconWallet = beaconClient
}
override fun onError(error: Throwable) {
error.printStackTrace()
}
override fun onCancel() {
super.onCancel()
}
})
import it.airgap.beaconsdk.client.wallet.BeaconWalletClient
myCoroutineScope.launch {
val beaconWallet = BeaconWalletClient("My App") {
support(/* blockchains */)
use(/* connections */)
}
}
Subscribe to incoming requests
- Callback
- Coroutines
import it.airgap.beaconsdk.client.wallet.compat.OnNewMessageListener
import it.airgap.beaconsdk.client.wallet.compat.connect
beaconWallet.connect(object : OnNewMessageListener {
override fun onNewMessage(message: BeaconMessage) {
...
}
override fun onError(error: Throwable) {
...
}
})
myCoroutineScope.launch {
beaconWallet.connect().collect { // it: Result<BeaconRequest>
...
}
}
Connect to a dApp
- Callback
- Coroutines
import it.airgap.beaconsdk.client.wallet.compat.SetCallback
import it.airgap.beaconsdk.client.wallet.compat.addPeers
import it.airgap.beaconsdk.core.data.P2pPeer
val dApp = P2pPeer(
id = "id",
name = "name",
publicKey = "publicKey",
relayServer = "relayServer",
version = "version",
)
beaconWallet.addPeers(dApp, callback = object : SetCallback {
override fun onSuccess() {
TODO("Not yet implemented")
}
override fun onError(error: Throwable) {
TODO("Not yet implemented")
}
})
import it.airgap.beaconsdk.core.data.P2pPeer
val dApp = P2pPeer(
id = "id",
name = "name",
publicKey = "publicKey",
relayServer = "relayServer",
version = "version",
)
myCoroutineScope.launch {
try {
beaconWallet.addPeers(dApp)
} catch (e: Exception) {
e.printStackTrace()
}
}
Handle requests from the dApp
- Callback
- Coroutines
import it.airgap.beaconsdk.client.wallet.compat.ResponseCallback
import it.airgap.beaconsdk.client.wallet.compat.respond
beaconWallet.respond(response, object : ResponseCallback {
override fun onSuccess() {
...
}
override fun onError(error: Throwable) {
...
}
})
myCoroutineScope.launch {
try {
beaconWallet.respond(response)
} catch (e: Exception) {
e.printStackTrace()
}
}
Integration with other asynchronous libraries
See the guides below to learn how to integrate Beacon Android SDK with other asynchronous libraries.