1. Download our Official Android App: Forums for Android!

Apps Receiving Data From a Watch to a Phone

Discussion in 'Android Development' started by haroldjclements, Sep 6, 2016.

  1. haroldjclements

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Sep 6, 2016

    Sep 6, 2016
    1
    0
    5
    Male
    Hello forum, I am struggling with this for a day. I have tried all I can find on the internet, but obviously I am not getting it.

    I have a watch app that once you press a button should sent a "pice of text" to the phone. However I can not get it to work.

    All the code and log statements return good from the watch - it finds the mobile and does not return an error. I dont get any of the log statements from the mobile code and nothing happens (no toast).

    Any help will be very gratefully received as I am at the end of my tether !!!!

    Kind Regards,
    Harold Clements

    Listener Class (mobile)

    Code (Text):
    1. import android.util.Log;
    2. import android.widget.Toast;
    3.  
    4. import com.google.android.gms.wearable.MessageEvent;
    5. import com.google.android.gms.wearable.WearableListenerService;
    6.  
    7. /**
    8. * Created by haroldjclements on 05/09/2016.
    9. */
    10.  
    11. public class ListenerServiceFromWear extends WearableListenerService {
    12.     private static final String CLASS_NAME = "ListenerServiceFromWear";
    13.     private final static String W_PATH = "/from-ware";
    14.  
    15.     @Override
    16.     public void onMessageReceived(MessageEvent messageEvent) {
    17.         Log.e(CLASS_NAME, "onMessageReceived");
    18.         super.onMessageReceived(messageEvent);
    19.  
    20.         if(messageEvent.getPath().equals(W_PATH)) {
    21.             Log.e(CLASS_NAME, "onMessageReceived : " + W_PATH);
    22.             String msg = new String(messageEvent.getData());
    23.             Log.e(CLASS_NAME, "msg : " + msg);
    24.  
    25.  
    26.             String toastMsg = msg;
    27.             Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
    28.         }
    29.     }
    30. }
    31.  
    Manifest.xml
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.     package="innovation.sita.aero.metime">
    4.  
    5.     <uses-permission android:name="android.permission.INTERNET" />
    6.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    7.     <uses-permission android:name="android.permission.VIBRATE"/>
    8.     <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
    9.  
    10.     <supports-screens
    11.         android:largeScreens="true"
    12.         android:normalScreens="true"
    13.         android:smallScreens="true"
    14.         android:xlargeScreens="true" />
    15.  
    16.     <application
    17.         android:allowBackup="true"
    18.         android:icon="@mipmap/ic_launcher"
    19.         android:label="@string/app_name"
    20.         android:supportsRtl="true"
    21.         android:theme="@style/AppTheme">
    22.         <activity android:name=".MainActivity">
    23.             <intent-filter>
    24.                 <action android:name="android.intent.action.MAIN" />
    25.  
    26.                 <category android:name="android.intent.category.LAUNCHER" />
    27.             </intent-filter>
    28.         </activity>
    29.         <activity android:name=".SelectionActivity" />
    30.         <activity android:name=".DetailsActivity" />
    31.         <activity android:name=".DetailsWaitActivity"></activity>
    32.  
    33.         <service android:name=".ListenerServiceFromWear">
    34.             <intent-filter>
    35.                 <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
    36.                 <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
    37.                 <action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
    38.                 <action android:name="com.google.android.gms.wearable.CHANNEL_EVENT" />
    39.                 <data android:scheme="wear" android:host="*" android:pathPrefix=".*" />
    40.             </intent-filter>
    41.         </service>
    42.     </application>
    43.  
    44. </manifest>
    Button Press Code on the Watch
    Code (Text):
    1. public class OfferViewActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    2.  
    3.     private static final String CLASS_NAME = "W_OfferViewActivity";
    4.     private TextView mTextView;
    5.  
    6.     private Node node;
    7.     private GoogleApiClient api;
    8.     private final static String W_PATH = "/from-ware";
    9.  
    10.     @Override
    11.     protected void onCreate(Bundle savedInstanceState) {
    12.         Log.d(CLASS_NAME, "onCreate");
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.activity_offer_view);
    15.  
    16.         final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    17.         stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
    18.             @Override
    19.             public void onLayoutInflated(WatchViewStub stub) {
    20.                 mTextView = (TextView) stub.findViewById(R.id.text);
    21.             }
    22.         });
    23.  
    24.         api = new GoogleApiClient.Builder(this)
    25.                 .addApi(Wearable.API)
    26.                 .addConnectionCallbacks(this)
    27.                 .addOnConnectionFailedListener(this)
    28.                 .build();
    29.  
    30.     }
    31.  
    32.     @Override
    33.     public void onConnected(@Nullable Bundle bundle) {
    34.         Log.d(CLASS_NAME, "onConnected");
    35.         Wearable.NodeApi.getConnectedNodes(api)
    36.                 .setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
    37.                     @Override
    38.                     public void onResult(@NonNull NodeApi.GetConnectedNodesResult nodesResult) {
    39.                         Log.d(CLASS_NAME, "onConnected : onResult");
    40.                         for (Node n : nodesResult.getNodes()) {
    41.                             if (n != null && n.isNearby()) {
    42.                                 Log.d(CLASS_NAME, "onConnected : onResult : " + n.getDisplayName());
    43.                                 node = n;
    44.                             }
    45.                             if (n == null) {
    46.                                 Log.d(CLASS_NAME, "onConnected : onResult : cannot connect");
    47.                             }
    48.                         }
    49.                     }
    50.                 });
    51.     }
    52.  
    53.  
    54.  
    55.     @Override
    56.     public void onConnectionSuspended(int i) {
    57.         Log.d(CLASS_NAME, "onConnectionSuspended");
    58.  
    59.     }
    60.  
    61.     @Override
    62.     public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    63.         Log.d(CLASS_NAME, "onConnectionFailed");
    64.  
    65.     }
    66.  
    67.     @Override
    68.     protected void onStart() {
    69.         Log.d(CLASS_NAME, "onStart");
    70.         super.onStart();
    71.         api.connect();
    72.     }
    73.  
    74.     @Override
    75.     protected void onStop() {
    76.         Log.d(CLASS_NAME, "onStop");
    77.         super.onStop();
    78.         api.disconnect();
    79.     }
    80.  
    81.     private void sendMessageToNode(String msg, Node node) {
    82.         Log.d(CLASS_NAME, "sendMessageToNode : " + msg);
    83.         if(node != null && api !=null) {
    84.             Log.d(CLASS_NAME, "sendMessageToNode : " + node.getDisplayName());
    85.             Wearable.MessageApi.sendMessage(api, node.getId(), W_PATH, msg.getBytes())
    86.                     .setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
    87.                         @Override
    88.                         public void onResult(@NonNull MessageApi.SendMessageResult sendMessageResult) {
    89.                             Log.d(CLASS_NAME, "sendMessageToNode : sendMessage :: onResult");
    90.                             if(!sendMessageResult.getStatus().isSuccess()) {
    91.                                 Log.d(CLASS_NAME, "sendMessageToNode : sendMessage :: onResult - Failed - " + sendMessageResult.getStatus().getStatusMessage());
    92.                             } else {
    93.                                 Log.d(CLASS_NAME, "sendMessageToNode : sendMessage :: onResult - Success");
    94.                             }
    95.                         }
    96.                     });
    97.  
    98.         }
    99.     }
    100.  
    101.     public void openOnDevice(View view) {
    102.         sendMessageToNode("getQRCodes", node);
    103.     }
    104. }
     

    Advertisement

Share This Page

Loading...