forked from omni/OmniCards
43 lines
1.2 KiB
Kotlin
43 lines
1.2 KiB
Kotlin
package ru.omni_devel.cards
|
|
|
|
import android.content.Context
|
|
import android.graphics.Bitmap
|
|
import android.widget.Toast
|
|
import com.google.android.gms.wearable.Wearable
|
|
import com.google.zxing.BarcodeFormat
|
|
import com.google.zxing.MultiFormatWriter
|
|
import com.journeyapps.barcodescanner.BarcodeEncoder
|
|
|
|
fun generateCode(value: String, format: BarcodeFormat): Bitmap {
|
|
val writer = MultiFormatWriter()
|
|
val matrix = writer.encode(value, format, 600, 300)
|
|
|
|
return BarcodeEncoder().createBitmap(matrix)
|
|
}
|
|
|
|
fun sendToWatch(context: Context, path: String, message: String): Boolean {
|
|
val nodeClient = Wearable.getNodeClient(context)
|
|
|
|
var isSuccess = false
|
|
|
|
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
|
if (nodes.isEmpty()) {
|
|
Toast.makeText(context, "Watch is not connected", Toast.LENGTH_SHORT).show()
|
|
|
|
return@addOnSuccessListener
|
|
}
|
|
|
|
nodes.forEach { node ->
|
|
Wearable.getMessageClient(context).sendMessage(
|
|
node.id,
|
|
path,
|
|
message.toByteArray(Charsets.UTF_8)
|
|
).addOnSuccessListener {
|
|
isSuccess = true
|
|
}
|
|
}
|
|
}
|
|
|
|
return isSuccess
|
|
}
|