从手机到机器人分享对象 [英] Share objects from phone to android wear

查看:146
本文介绍了从手机到机器人分享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用。在此应用程序中,您有包含2个字符串(名称和年龄)和位图(头像)的对象。所有内容都保存到sqlite数据库。

I created an app. Within this app, you have objects which contains 2 strings (name and age) and a bitmap (avatar). Everything is saved to a sqlite database.

现在我想在智能手表上访问这些对象。所以我想实现你可以开始,启动应用程序并向左和向右滚动以查看这些对象。

Now I want these objects to be accessible on my smart watch. So I want to achieve that you can go to start, start the application and scroll to the left and right to see these objects.

这意味着我必须检索对象从电话中获取它们。

This means I have to retrieve the objects from the phone and get them at the watch.

我目前想知道我是否做得对,或者我应该采取不同的做法。每当您在手表上启动应用程序时,我都会向手机发送我想要对象的请求。

I am currently wondering if I did everything right, or that I should do stuff differently. Whenever you start the application on your watch, I am sending a request to the phone that I want the objects.

private void sendMessage() {
    if(mGoogleApiClient.isConnected()) {
        new Thread( new Runnable() {
            @Override
            public void run() {
                NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( mGoogleApiClient ).await();
                for(Node node : nodes.getNodes()) {
                   Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), REQUEST_PET_RETRIEVAL_PATH, null).await();
                }
            }
        }).start();
    }
}

在电话上,我收到此消息并发送带有对象的消息。

On the phone, I am receiving this message and sending a message back with an object.

public void onMessageReceived(MessageEvent messageEvent) {
    super.onMessageReceived(messageEvent);

    if (messageEvent.getPath().equals(REQUEST_PET_RETRIEVAL_PATH)) {


        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle connectionHint) {
                        final PutDataMapRequest putRequest = PutDataMapRequest.create("/send-pets");
                        final DataMap map = putRequest.getDataMap();

                        File imgFile = new File(obj.getAvatar());

                        Bitmap avatar;
                        if(imgFile.exists()) {
                            avatar = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                        } else {
                            avatar = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                        }

                        Asset asset = createAssetFromBitmap(avatar);
                        map.putAsset("avatar", asset);
                        map.putString("name", obj.getName());
                        map.putString("age", obj.getDateOfBirth());
                        Wearable.DataApi.putDataItem(mGoogleApiClient, putRequest.asPutDataRequest());
                    }

                    @Override
                    public void onConnectionSuspended(int cause) {
                    }
                })
                .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult result) {
                    }
                })
                .addApi(Wearable.API)
                .build();
        mGoogleApiClient.connect();
    }

在手表上,我正在检索对象。

On the watch, I am then retrieving the object.

public void onDataChanged(DataEventBuffer dataEvents) {
    final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
    for(DataEvent event : events) {
        final Uri uri = event.getDataItem().getUri();
        final String path = uri!=null ? uri.getPath() : null;
        if("/send-pets".equals(path)) {
            final DataMap map = DataMapItem.fromDataItem(event.getDataItem()).getDataMap();
            String name = map.getString("name");
            String age = map.getString("age");

            Asset avatar = map.getAsset("avatar");
            Bitmap bitmap = loadBitmapFromAsset(avatar);
        }
    }
}

现在我被困2问题:

1)这是要走的路还是我应该采用不同的解决办法?

1) Is this the way to go or should I solve it differently?

2)是可以一次发送多个对象,还是只需在onConnected方法中绕一个部分循环并分别发送每个对象?

2) Is it possible to sent multiple objects at once or do I just have to put a loop around the part in the "onConnected" method and sent each object separatly?

推荐答案

是的,这种做法是正确的。

Yes, this approach is good and correct one.

是的,可以发送多个,但你应该知道它们不是发送 它们更像是手机和Wear之间的共享或同步,并且可以在任何其他时间点进行修改(但我建议将它们保存到 SharedPreferences 上磨损到能够在离线模式下访问它们。

Yes it is possible to send multiple but you should be aware that they are not "send" they are more something like shared or synchronized between phone and Wear, and can be modified in any further point in time (however I would recommend to save them to SharedPreferences on Wear to be able to access them in offline mode.

所以Message API发送对象(快速,简单),DataItem API更复杂,但用于更大的数据和在手表和手机之间分享东西。

So Message API sends objects (fast, and simple), and DataItem API is more complex but is used for bigger data and to share things between watch and phone.

这篇关于从手机到机器人分享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆