Created
June 9, 2022 14:14
-
-
Save molidev8/ce7f434f92d6c09d95d6ab786deee9c5 to your computer and use it in GitHub Desktop.
A callback to pair two devices with Nearby Share
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private inner class ConnectingProcessCallback : ConnectionLifecycleCallback() { | |
override fun onConnectionInitiated(endPointId: String, info: ConnectionInfo) { | |
MaterialAlertDialogBuilder(context) | |
.setTitle(Strings.get(R.string.accept_connection, info.endpointName)) | |
.setMessage(Strings.get(R.string.confirm_code, info.authenticationDigits)) | |
.setPositiveButton(Strings.get(R.string.accept)) { _: DialogInterface, _: Int -> | |
Nearby.getConnectionsClient(context) | |
.acceptConnection(endPointId, DataReceivedCallback()) | |
} | |
.setNegativeButton(Strings.get(R.string.cancel)) { _: DialogInterface, _: Int -> | |
Nearby.getConnectionsClient(context).rejectConnection(endPointId) | |
} | |
.setIcon(R.drawable.outline_warning_24) | |
.show() | |
} | |
override fun onConnectionResult(endPointId: String, result: ConnectionResolution) { | |
when (result.status.statusCode) { | |
ConnectionsStatusCodes.STATUS_OK -> { | |
if (emitter) { | |
val gson = Gson() | |
val rec = viewModel.recipeWithIng.value | |
rec?.let { | |
// Changing the id so the receiver saves it as a new recipe and not an updated one | |
val jsonRecipe = gson.toJson( | |
RecipeWithIng( | |
Recipe( | |
-1, | |
rec.domainRecipe.name, | |
rec.domainRecipe.timeToCook, | |
rec.domainRecipe.dishType, | |
rec.domainRecipe.dietType, | |
rec.domainRecipe.instructions, | |
rec.domainRecipe.image, | |
rec.domainRecipe.description | |
), rec.ings | |
) | |
) | |
sendText(jsonRecipe, endPointId) | |
viewModel.recipeWithIng.value?.domainRecipe?.image?.let { uri -> | |
Log.d(GENERAL, "enviando foto") | |
sendImage(uri, endPointId) | |
} | |
} | |
} | |
} | |
ConnectionsStatusCodes.STATUS_CONNECTION_REJECTED -> { | |
Log.d(GENERAL, "conexion rechazada") | |
} | |
ConnectionsStatusCodes.STATUS_ERROR -> { | |
Log.d(GENERAL, "DESCONEXION") | |
} | |
} | |
} | |
override fun onDisconnected(endPointId: String) { | |
Log.d(GENERAL, "DESCONEXION OK") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment