Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.os.Bundle;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.wearable.Wearable;
- /**
- * Created by Kathrin on 26.01.2015.
- */
- public class HandheldConnectionCallbacks implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
- /**
- * The singleton instance of this listener class.
- */
- private static HandheldConnectionCallbacks listener;
- /**
- * Private Constructor so that no multiple instances can be created.
- */
- private HandheldConnectionCallbacks(){}
- /**
- * This method checks whether this listener was already instantiated or not, creates a new
- * instance if not or returns the existing one if it was already instantiated.
- * @return Returns a new instance of this listener if there is none or the already stored one.
- */
- public static HandheldConnectionCallbacks getInstance() {
- if (listener == null) {
- listener = new HandheldConnectionCallbacks();
- }
- return listener;
- }
- /**
- * This method gets call each time a Wear was connected with the application and adds then
- * the listener for messages from the mobile phone.
- * @param bundle A bundle with eventual extra information.
- */
- @Override
- public void onConnected(Bundle bundle) {
- Wearable.DataApi.addListener(Main.mGoogleApiClient, new WearListenerService());
- }
- /**
- * This method gets call each time a Wear connection was suspended.
- * @param i The cause of the disconnection.
- */
- @Override
- public void onConnectionSuspended(int i) {
- }
- /**
- * This method gets call each time a Wear connection failed.
- * @param connectionResult The result object that contains information why the connection failed.
- */
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- }
- }
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.graphics.BitmapFactory;
- import android.support.v4.app.NotificationCompat;
- import android.support.v4.app.NotificationManagerCompat;
- import com.google.android.gms.wearable.MessageEvent;
- import com.google.android.gms.wearable.WearableListenerService;
- /**
- * Created by Kathrin on 13.01.2015.
- */
- public class WearListenerService extends WearableListenerService {
- private final int NOTIFICATION_ID = 123456;
- NotificationManagerCompat notificationManager;
- /**
- * The services @method{onCreate} method where the @method{super.onCreate} method gets called and the
- * notification manager instance gets initialized.
- */
- @Override
- public void onCreate() {
- super.onCreate();
- notificationManager =
- NotificationManagerCompat.from(this);
- }
- /**
- * This method gets called each time this listener service receives a message sent by
- * the connected Android Smartphone, builds up and updates the persistent notification
- * and calls either @method{startScanning} or @method{stopScanning} in the @class{PersistentNotification} class
- * according to the data as message.
- *
- * @param event The message event holding the data sent from the Android Smartphone.
- */
- @Override
- public void onMessageReceived(MessageEvent event) {
- if ((event.getData()[0] != 0)) {
- //TRUE (in meinem fall sensor listening starten)
- } else {
- //FALSE (in meinem fall sensor listening stoppen)
- }
- }
- }
- //Methode zum zurückschicken der Sensordaten an das mobile phone
- /**
- * This method builds up a message containing the start/stop information for the sensor and
- * sends it to the Android Wear. Additionally, a result listener is instantiated that receives the
- * sending result.
- * @param _data The byte array that should be sent to the smartphone.
- */
- public static void sendDataToHandheld(final byte[] _data) {
- // Send the RPC
- PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
- nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
- @Override
- public void onResult(NodeApi.GetConnectedNodesResult result) {
- for (int i = 0; i < result.getNodes().size(); i++) {
- Node node = result.getNodes().get(i);
- PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(),
- PATH, _data);
- messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
- @Override
- public void onResult(MessageApi.SendMessageResult sendMessageResult) {
- Status status = sendMessageResult.getStatus();
- if (status.getStatusCode() != WearableStatusCodes.SUCCESS) {
- //Log.d(TAG, "SENDING NOT SUCCESSFUL");
- }
- }
- });
- }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement