Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] - File Transfer between Android & Wear OS #20

Open
cip123 opened this issue Mar 4, 2024 · 3 comments
Open

[Feature] - File Transfer between Android & Wear OS #20

cip123 opened this issue Mar 4, 2024 · 3 comments

Comments

@cip123
Copy link

cip123 commented Mar 4, 2024

https://developer.android.com/training/wearables/data/data-layer#send-and-sync-with-API

@fabOnReact fabOnReact changed the title Any plans to support data synhronization through DataItems? [feature] Support data synhronization through DataItems Dec 21, 2024
@fabOnReact fabOnReact changed the title [feature] Support data synhronization through DataItems [feature] - Support data synhronization through DataItems Dec 21, 2024
@fabOnReact fabOnReact reopened this Dec 21, 2024
@fabOnReact fabOnReact changed the title [feature] - Support data synhronization through DataItems [Feature] - Support data synhronization through DataItems Dec 21, 2024
@fabOnReact fabOnReact changed the title [Feature] - Support data synhronization through DataItems [Feature] - File Transfer Between Android & Wear OS Jan 29, 2025
@fabOnReact fabOnReact changed the title [Feature] - File Transfer Between Android & Wear OS [Feature] - File Transfer between Android & Wear OS Jan 29, 2025
@fabOnReact
Copy link
Owner

Here’s the corrected version with proper markdown for the code snippets:

File Transfer Between Android Mobile App (Java) and WearOS App (Jetpack Compose)

Android Mobile App (Java)

In the Android app, we will need to convert the file to an Asset and send it using the DataClient API.

  1. Convert File to Asset in Java
public Asset createAssetFromFile(File file) {
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] byteArray = new byte[(int) file.length()];
        fileInputStream.read(byteArray);
        fileInputStream.close();
        return Asset.createFromBytes(byteArray);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
  1. Send File from Android Mobile to WearOS
public void sendFileToWear(File file) {
    Asset asset = createAssetFromFile(file);
    PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/file_transfer");
    dataMapRequest.getDataMap().putAsset("file", asset);
    dataMapRequest.getDataMap().putLong("timestamp", System.currentTimeMillis());

    PutDataRequest request = dataMapRequest.asPutDataRequest();
    Task<DataItem> task = Wearable.getDataClient(context).putDataItem(request);
    task.addOnSuccessListener(dataItem -> {
        // Handle success
    }).addOnFailureListener(e -> {
        // Handle failure
    });
}

WearOS App (Jetpack Compose)

For the WearOS app written in Jetpack Compose, you’ll need to listen for incoming data changes using the WearableListenerService and handle the asset appropriately.

  1. Listen for File Transfer in WearOS (Jetpack Compose)

In your WearOS app, you can use a WearableListenerService to handle the incoming file transfer. Since Jetpack Compose is used for the UI, this service will run in the background.

class WearDataListenerService : WearableListenerService() {
    override fun onDataChanged(dataEvents: DataEventBuffer) {
        for (event in dataEvents) {
            if (event.type == DataEvent.TYPE_CHANGED) {
                val dataItem = event.dataItem
                if (dataItem.uri.path == "/file_transfer") {
                    val asset = DataMapItem.fromDataItem(dataItem).dataMap.getAsset("file")
                    saveReceivedFile(asset)
                }
            }
        }
    }
}
  1. Convert Asset Back to File in WearOS (Jetpack Compose)

Once you receive the asset, save it to the WearOS device:

fun saveReceivedFile(asset: Asset) {
    val dataClient = Wearable.getDataClient(this)
    val task = dataClient.getFdForAsset(asset)

    task.addOnSuccessListener { assetInputStream ->
        val file = File(filesDir, "received_file.jpg") // Change extension as needed
        assetInputStream.inputStream().use { input ->
            file.outputStream().use { output ->
                input.copyTo(output)
            }
        }
    }.addOnFailureListener {
        Log.e("WearOS", "File transfer failed", it)
    }
}
  1. Trigger a UI Update in Jetpack Compose

If you want to display a UI update once the file is received, you can trigger it in your ViewModel or directly in your composables. For example, you can use LaunchedEffect or rememberCoroutineScope to update the UI after the file is saved.

Summary for Your Setup
• Android Mobile App (Java):
• Use DataClient to send files as Asset objects.
• Convert files to Asset using createAssetFromFile().
• WearOS App (Jetpack Compose):
• Use WearableListenerService to listen for incoming data.
• Convert Asset back to a file and save it locally.

This approach ensures you can transfer files efficiently between the Android mobile app and the WearOS app.

Let me know if you need further details!

@fabOnReact
Copy link
Owner

@cip123 you still need this functionality?

@cip123
Copy link
Author

cip123 commented Jan 31, 2025

@fabOnReact thanks a lot. This is something on my TODO list. I will have to take a look at it again to prioritize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants