Share objects from phone to android wear

I have created an application. In this application, you have objects that contain 2 strings (name and age) and a bitmap (avatar). Everything is stored in the sqlite database.

Now I want these objects to be available on my smartwatch. So what I want to achieve is that you can start, launch the application, and scroll left and right to see these objects.

This means I need to recover items from my phone and get them on the watch.

I am currently wondering if I did everything right, or what should I do differently. Whenever you run an app on your watch, I send a request to the phone that I want 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 receive this message and send the message using the 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 get 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);
        }
    }
}

      

Now I am stuck with two questions:

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

2) Is it possible to send multiple objects at once or do you just need to loop around the part in the "onConnected" method and send each object separately?

+3


source to share


1 answer


Yes, this approach is good and correct.

Yes, you can send several, but you should know that they do not "send", they are more like shared or synced between the phone and Wear and can be changed at any time (however, I would recommend to save them SharedPreferences

to Wear. to be able to access them offline.



So the messaging API sends objects (fast and simple), while the DataItem API is more complex, but used for big data and exchanging things between watch and phone.

+2


source







All Articles