Advertisement
Guest User

WearToMobile

a guest
May 26th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. import android.os.Bundle;
  2.  
  3. import com.google.android.gms.common.ConnectionResult;
  4. import com.google.android.gms.common.api.GoogleApiClient;
  5. import com.google.android.gms.wearable.Wearable;
  6.  
  7. /**
  8.  * Created by Kathrin on 26.01.2015.
  9.  */
  10. public class HandheldConnectionCallbacks implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  11.  
  12.     /**
  13.      * The singleton instance of this listener class.
  14.      */
  15.     private static HandheldConnectionCallbacks listener;
  16.  
  17.     /**
  18.      * Private Constructor so that no multiple instances can be created.
  19.      */
  20.     private HandheldConnectionCallbacks(){}
  21.  
  22.     /**
  23.      * This method checks whether this listener was already instantiated or not, creates a new
  24.      * instance if not or returns the existing one if it was already instantiated.
  25.      * @return Returns a new instance of this listener if there is none or the already stored one.
  26.      */
  27.     public static HandheldConnectionCallbacks getInstance() {
  28.         if (listener == null) {
  29.             listener = new HandheldConnectionCallbacks();
  30.         }
  31.         return listener;
  32.     }
  33.  
  34.     /**
  35.      * This method gets call each time a Wear was connected with the application and adds then
  36.      * the listener for messages from the mobile phone.
  37.      * @param bundle A bundle with eventual extra information.
  38.      */
  39.     @Override
  40.     public void onConnected(Bundle bundle) {
  41.         Wearable.DataApi.addListener(Main.mGoogleApiClient, new WearListenerService());
  42.     }
  43.  
  44.     /**
  45.      * This method gets call each time a Wear connection was suspended.
  46.      * @param i The cause of the disconnection.
  47.      */
  48.     @Override
  49.     public void onConnectionSuspended(int i) {
  50.  
  51.     }
  52.  
  53.     /**
  54.      * This method gets call each time a Wear connection failed.
  55.      * @param connectionResult The result object that contains information why the connection failed.
  56.      */
  57.     @Override
  58.     public void onConnectionFailed(ConnectionResult connectionResult) {
  59.  
  60.     }
  61. }
  62.  
  63. import android.app.PendingIntent;
  64. import android.content.Intent;
  65. import android.graphics.BitmapFactory;
  66. import android.support.v4.app.NotificationCompat;
  67. import android.support.v4.app.NotificationManagerCompat;
  68.  
  69. import com.google.android.gms.wearable.MessageEvent;
  70. import com.google.android.gms.wearable.WearableListenerService;
  71.  
  72. /**
  73.  * Created by Kathrin on 13.01.2015.
  74.  */
  75. public class WearListenerService extends WearableListenerService {
  76.  
  77.     private final int NOTIFICATION_ID = 123456;
  78.  
  79.     NotificationManagerCompat notificationManager;
  80.  
  81.     /**
  82.      * The services @method{onCreate} method where the @method{super.onCreate} method gets called and the
  83.      * notification manager instance gets initialized.
  84.      */
  85.     @Override
  86.     public void onCreate() {
  87.         super.onCreate();
  88.         notificationManager =
  89.                 NotificationManagerCompat.from(this);
  90.     }
  91.  
  92.     /**
  93.      * This method gets called each time this listener service receives a message sent by
  94.      * the connected Android Smartphone, builds up and updates the persistent notification
  95.      * and calls either @method{startScanning} or @method{stopScanning} in the @class{PersistentNotification} class
  96.      * according to the data as message.
  97.      *
  98.      * @param event The message event holding the data sent from the Android Smartphone.
  99.      */
  100.     @Override
  101.     public void onMessageReceived(MessageEvent event) {
  102.  
  103.         if ((event.getData()[0] != 0)) {
  104.                 //TRUE (in meinem fall sensor listening starten)
  105.         } else {
  106.                 //FALSE (in meinem fall sensor listening stoppen)
  107.         }
  108.     }
  109. }
  110.  
  111.  
  112. //Methode zum zurückschicken der Sensordaten an das mobile phone
  113. /**
  114.      * This method builds up a message containing the start/stop information for the sensor and
  115.      * sends it to the Android Wear. Additionally, a result listener is instantiated that receives the
  116.      * sending result.
  117.      * @param _data The byte array that should be sent to the smartphone.
  118.      */
  119.     public static void sendDataToHandheld(final byte[] _data) {
  120.  
  121.         // Send the RPC
  122.         PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
  123.         nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
  124.             @Override
  125.             public void onResult(NodeApi.GetConnectedNodesResult result) {
  126.                 for (int i = 0; i < result.getNodes().size(); i++) {
  127.                     Node node = result.getNodes().get(i);
  128.                     PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(),
  129.                             PATH, _data);
  130.                     messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
  131.                         @Override
  132.                         public void onResult(MessageApi.SendMessageResult sendMessageResult) {
  133.                             Status status = sendMessageResult.getStatus();
  134.                             if (status.getStatusCode() != WearableStatusCodes.SUCCESS) {
  135.  
  136.                                 //Log.d(TAG, "SENDING NOT SUCCESSFUL");
  137.  
  138.                             }
  139.                         }
  140.                     });
  141.                 }
  142.             }
  143.         });
  144.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement