Android Wear ChannelApi的例子?

exk*_*ria 9 channel-api wear-os

最新的Android Wear更新支持ChannelApi,可用于向/从可穿戴设备或手持设备发送文件.问题是我找不到如何使用此功能的单个示例.Android示例不包含此功能.因此,如果有人知道如何使用sendFile/receiveFile并且可以在这里给出一个快速示例,那将不胜感激.

小智 7

请查看此答案,了解如何使用Channel API在设备之间创建通道.

在创建googleClient并检索要将文件发送到的设备的nodeId之后,基本上您可以在可穿戴方面使用以下代码:

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

//sending file
channel.sendFile(googleClient, Uri.fromFile(file));
Run Code Online (Sandbox Code Playgroud)

然后,在手持设备上:

//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();
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

如果您需要更多相关信息,请访问Google参考网站