安卓智能手表ChannelApi示例?

11

最新的Android Wear更新支持使用ChannelApi来发送/接收可穿戴设备或手持设备上的文件。问题是,我找不到如何使用此功能的一个样例。Android示例中没有包含此功能。因此,如果有人知道如何使用sendFile / receiveFile并可以在此给出一个快速示例,那将不胜感激。


有任何消息吗?我也在寻找同样的东西。 - krakig
3个回答

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));

然后,在手持设备上:
//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();
        }
    });
}

如果您需要更多关于此的信息,请访问谷歌的参考网站

谢谢您提供的示例。我已经设置我的手机将.mp3文件发送到我的可穿戴设备端,但是我没有收到任何东西(onChannelOpened方法未被触发),我可能做错了什么?http://pastebin.com/VjfhwW51 - krakig
你如何检索手持节点的ID? - Reslley Gabriel
1
在接收文件时,有没有一种方法可以查看下载进度? - krakig
@ReslleyGabriel 你可能需要迭代所有节点来查找节点ID。 - Foreverniu
有人在打开通道时遇到问题,但另一端没有收到OnChannelOpen()调用吗?http://stackoverflow.com/questions/39526148/android-wear-channelapi-channel-open-but-not-connecting - Hephaestus

2

这只是对答案的补充:还要检查AndroidManifest中的WearableListenerService。它的意图过滤器应包含com.google.android.gms.wearable.CHANNEL_EVENT操作。


0

我曾经使用过类似这样的代码并且成功了。传输可能会相当缓慢。

手持设备和可穿戴设备应用程序在它们的 gradle 文件中必须具有相同的 applicationId。 可穿戴设备需要一个类似于这样的清单条目

    <service
         android:name="com.me.myWearableListenerService"
        android:enabled="true"
         android:exported="true">

        <intent-filter>
            <!-- listeners receive events that match the action and data filters -->
             <action android:name="com.google.android.gms.wearable.CHANNEL_EVENT"/>
            <data android:scheme="wear" android:host="*" android:pathPrefix="/MyAppPath" />
        </intent-filter>

    </service>

当手持设备发送文件时,启动其WearableListenerService。

private static final String WEARABLE_FILE_COPY = "MyAppPath/FileCopy";
private void copyFileToWearable  (final File file, final String nodeId, Context ctx) {

    new Thread(new Runnable() {
        @Override
        public void run() {

            final ChannelClient cc = Wearable.getChannelClient(ctx);

            ChannelClient.ChannelCallback ccb = new ChannelClient.ChannelCallback() {

                @Override
                public void onChannelClosed(@NonNull ChannelClient.Channel channel, int i, int i1) {
                    super.onChannelClosed(channel, i, i1);
                    Log.d(TAG, "copyFileToWearable " + channel.getNodeId() + " onChannelClosed ");
                    cc.unregisterChannelCallback(this);
                }

                @Override
                public void onOutputClosed(@NonNull ChannelClient.Channel channel, int i, int i1) {
                    super.onOutputClosed(channel, i, i1);
                    Log.d(TAG, "copyFileToWearable " + channel.getNodeId() + " onOutputClosed ");
                    cc.unregisterChannelCallback(this);
                    // this is transfer success callback ...
                }
            };

            ChannelClient.Channel c;

            Log.d(TAG, "copyFileToWearable transfer file " + file.getName() +
                    " size:" + file.length()/1000000 + "Mb");
            try {
                // send the filename to the wearable with the channel open
                c = Tasks.await(cc.openChannel(nodeId, WEARABLE_FILE_COPY + "/" + file.getName()));
                Log.d(TAG, "copyFileToWearable channel opened to " + nodeId);

                Log.d(TAG, "copyFileToWearable register callback");
                Tasks.await(cc.registerChannelCallback(c, ccb));

                Log.d(TAG, "copyFileToWearable sending file " + file.getName());
                Tasks.await(cc.sendFile(c, Uri.fromFile(file)));
                // completion is indicated by onOutputClosed

            } catch (Exception e) {
                Log.w(TAG, "copyFileToWearable exception " + e.getMessage());
                cc.unregisterChannelCallback(ccb);
                // failure
            }                    
        }
    }).start();
}

在 WearableListenerService 的 onChannelOpened 中调用此函数,当 c.getPath() 以 WEARABLE_FILE_COPY 开头时

private void receiveFileFromHandheld(final ChannelClient.Channel c, File myStorageLocation, Context ctx) {

    // filename sent by the handheld is at the end of the path
    String[] bits = c.getPath().split("\\/");

    // store in a suitable spot
    final String receivedFileName = myStorageLocation.getAbsolutePath() + "/" + bits[bits.length-1];

    new Thread(new Runnable() {
        @Override
        public void run() {

            final ChannelClient cc = Wearable.getChannelClient(ctx);
            ChannelClient.ChannelCallback ccb = new ChannelClient.ChannelCallback() {

                boolean mClosed = false;

                @Override
                public void onChannelClosed(@NonNull ChannelClient.Channel channel, int i, int i1) {
                    super.onChannelClosed(channel, i, i1);
                    Log.d(TAG, "receiveFileFromHandheld " + channel.getNodeId() + " onChannelClosed ");
                    if (!mClosed){
                        // failure ...
                    }
                }

                @Override
                public void onInputClosed(@NonNull ChannelClient.Channel channel, int i, int i1) {
                    super.onInputClosed(channel, i, i1);
                    Log.d(TAG, "receiveFileFromHandheld " + channel.getNodeId() + " onInputClosed ");
                    long fs = new File(receivedFileName).length();
                    Log.d(TAG, "receiveFileFromHandheld got " + receivedFileName +
                            " size:" + fs / 1000000 + "Mb");
                    cc.unregisterChannelCallback(this);
                    mClosed = true;
                    // success !
                }
            };

            try {

                Log.d(TAG, "receiveFileFromHandheld register callback");
                Tasks.await(cc.registerChannelCallback(c, ccb));
                Log.d(TAG, "receiveFileFromHandheld receiving file " + receivedFileName);
                Tasks.await(cc.receiveFile(c, Uri.fromFile(new File(receivedFileName)), false));
                // completion is indicated by onInputClosed
            } catch (Exception e) {
                Log.w(TAG, "receiveFileFromHandheld exception " + e.getMessage());
                cc.unregisterChannelCallback(ccb);

                // failure ...
            }
        }
    }
    ).start();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接