Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Wear ChannelApi examples?

The latest Android Wear update comes with support for ChannelApi that can be used for sending files to/from wearable or handheld. The problem is I cannot find a single sample of how to use this functionality. The Android samples doesn't include this feature. So if anyone knows how to use the sendFile/receiveFile and can give a quick example here it would be appreciated.

like image 586
exkoria Avatar asked Jun 07 '15 12:06

exkoria


People also ask

How do you program a smartwatch?

Pairing an Android Wear smartwatch with an Android phoneInstall the "Wear OS by Google Smartwatch" app on your phone, available on the Google Play Store. On your watch, turn on Bluetooth. Open the Wear OS app on your phone and follow the initial setup. Tap on "I agree".


1 Answers

Take a look on this answer to know how to use the Channel API to create the channel between the devices.

After you create the googleClient and retrive the nodeId of the device you want to send the file to, basically you can use the following code on the wearable side:

//opening channel
ChannelApi.OpenChannelResult result = Wearable.ChannelApi.openChannel(googleClient, nodeId, "/mypath").await();
channel = result.getChannel(); 

//sending file
channel.sendFile(googleClient, Uri.fromFile(file));

Then, on the handheld device:

//receiving the file
@Override
public void onChannelOpened(Channel channel) {
    if (channel.getPath().equals("/mypath")) {

        file = new File("/sdcard/file.txt");

        try {
            file.createNewFile();
        } catch (IOException e) {
            //handle error
        }

        channel.receiveFile(mGoogleApiClient, Uri.fromFile(file), false);
    }
}

//when file is ready
@Override
public void onInputClosed(Channel channel, int i, int i1) {
    MainActivity.this.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this, "File received!", Toast.LENGTH_SHORT).show();
        }
    });
}

If you need more information about this, please visit the reference site from Google

like image 59
Reslley Gabriel Avatar answered Sep 30 '22 20:09

Reslley Gabriel