-
Notifications
You must be signed in to change notification settings - Fork 4
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
Comments
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.
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;
}
}
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.
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)
}
}
}
}
}
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)
}
}
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 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! |
@cip123 you still need this functionality? |
@fabOnReact thanks a lot. This is something on my TODO list. I will have to take a look at it again to prioritize it. |
https://developer.android.com/training/wearables/data/data-layer#send-and-sync-with-API
The text was updated successfully, but these errors were encountered: